@@ -25,8 +25,6 @@ static void logging(const char *fmt, ...)
25
25
// ------------------------------------------------------------------------------
26
26
Prober::~Prober ()
27
27
{
28
- avformat_close_input (&mFormatCtx );
29
- avcodec_free_context (&mCodecCtx );
30
28
}
31
29
32
30
// ------------------------------------------------------------------------------
@@ -48,15 +46,11 @@ bool Prober::open(const char *path)
48
46
std::cerr << " prober could not get stream info" ;
49
47
return false ;
50
48
}
51
- av_dump_format (mFormatCtx , 0 , path, 0 ); // [4]
52
49
53
- if (!findStreams ())
54
- {
55
- std::cerr << " prober could not find streams in input" ;
56
- return false ;
57
- }
50
+ // TODO:
51
+ av_dump_format (mFormatCtx , 0 , path, 0 );
58
52
59
- if (!openCodec ())
53
+ if (!openCodecs ())
60
54
{
61
55
std::cerr << " prober could not open codecs on input" ;
62
56
return false ;
@@ -68,121 +62,51 @@ bool Prober::open(const char *path)
68
62
// ------------------------------------------------------------------------------
69
63
//
70
64
// ------------------------------------------------------------------------------
71
- static int decode_packet2 (
72
- const AVStream *stream,
73
- AVCodecContext *codecContext,
74
- AVPacket *pkt,
75
- AVFrame *frame)
65
+ bool Prober::openCodecs ()
76
66
{
77
- // printf("stream_index %d\n", pkt->stream_index);
78
- // printf("pts %d\n", pkt->pts);
79
- // printf("dts %d\n", pkt->dts);
80
- // printf("dts_time", pkt->dts, &st->time_base);
81
- // printf("duration %d\n", pkt->duration);
82
- // printf("duration_time", pkt->duration, &st->time_base);
83
- // print_val("size", pkt->size, unit_byte_str);
84
-
85
- auto ret = avcodec_send_packet (codecContext, pkt);
86
- if (ret < 0 )
67
+ for (unsigned i = 0 ; i < mFormatCtx ->nb_streams ; i++)
87
68
{
88
- printf (" Error sending packet for decoding." );
89
- return -1 ;
90
- }
69
+ const auto stream = mFormatCtx ->streams [i];
91
70
92
- while (ret >= 0 )
93
- {
94
- ret = avcodec_receive_frame (codecContext, frame);
95
- // printf("receive frame: %d %d\n", frame->width, frame->height);
96
- if (ret == AVERROR (EAGAIN) || ret == AVERROR_EOF)
71
+ logging (" AVStream->time_base before open coded %d/%d" , stream->time_base .num , stream->time_base .den );
72
+ logging (" AVStream->r_frame_rate before open coded %d/%d" , stream->r_frame_rate .num , stream->r_frame_rate .den );
73
+
74
+ if (stream->codecpar ->codec_id == AV_CODEC_ID_PROBE)
97
75
{
98
- break ;
76
+ std::cerr << " Failed to probe codec for input stream " << stream->index << std::endl;
77
+ mStreams .emplace_back (std::make_shared<InputStream>(nullptr , nullptr ));
78
+ continue ;
99
79
}
100
- else if (ret < 0 )
80
+
81
+ auto codec = avcodec_find_decoder (stream->codecpar ->codec_id );
82
+ if (!codec)
101
83
{
102
- printf (" Error while decoding packet." );
103
- return -1 ;
84
+ std::cerr
85
+ << " Unsupported codec with id " << stream->codecpar ->codec_id
86
+ << " for input stream " << stream->index << std::endl;
87
+ return false ;
104
88
}
105
89
106
- logging (
107
- " Frame %d (type=%c, size=%d bytes, format=%d) pts %d key_frame %d [DTS %d]" ,
108
- codecContext->frame_number ,
109
- av_get_picture_type_char (frame->pict_type ),
110
- frame->pkt_size ,
111
- frame->format ,
112
- frame->pts ,
113
- frame->key_frame ,
114
- frame->coded_picture_number );
115
- }
116
-
117
- return 0 ;
118
- }
119
-
120
- // ------------------------------------------------------------------------------
121
- //
122
- // ------------------------------------------------------------------------------
123
- bool Prober::findStreams ()
124
- {
125
- for (unsigned i = 0 ; i < mFormatCtx ->nb_streams ; i++)
126
- {
127
- auto codecParam = mFormatCtx ->streams [i]->codecpar ;
128
- logging (" AVStream->time_base before open coded %d/%d" , mFormatCtx ->streams [i]->time_base .num , mFormatCtx ->streams [i]->time_base .den );
129
- logging (" AVStream->r_frame_rate before open coded %d/%d" , mFormatCtx ->streams [i]->r_frame_rate .num , mFormatCtx ->streams [i]->r_frame_rate .den );
130
-
131
- if (codecParam->codec_type == AVMEDIA_TYPE_VIDEO)
132
- {
133
- mVideoStream = i;
134
- mCodecParameters = mFormatCtx ->streams [mVideoStream ]->codecpar ;
135
- }
136
- else if (codecParam->codec_type == AVMEDIA_TYPE_AUDIO)
90
+ auto codecCtx = avcodec_alloc_context3 (codec);
91
+ if (!codecCtx)
137
92
{
138
- logging (" Audio Codec: %d channels, sample rate %d" , codecParam->channels , codecParam->sample_rate );
139
- mAudioStream = i;
140
- mAudioCodecParameters = mFormatCtx ->streams [mAudioStream ]->codecpar ;
93
+ std::cerr << " could not allocate memory for Codec Context" ;
94
+ return false ;
141
95
}
142
- }
143
-
144
- return mAudioStream != -1 || mVideoStream != -1 ;
145
- }
146
96
147
- // ------------------------------------------------------------------------------
148
- //
149
- // ------------------------------------------------------------------------------
150
- bool Prober::openCodec ()
151
- {
152
- auto codec = avcodec_find_decoder (mCodecParameters ->codec_id );
153
- if (!codec)
154
- {
155
- std::cerr << " unsupported codec" ;
156
- return false ;
157
- }
158
-
159
- /* *
160
- * Note that we must not use the AVCodecContext from the video stream
161
- * directly! So we have to use avcodec_copy_context() to copy the
162
- * context to a new location (after allocating memory for it, of
163
- * course).
164
- */
165
-
166
- // Copy context
167
- mCodecCtx = avcodec_alloc_context3 (codec);
168
- if (!mCodecCtx )
169
- {
170
- std::cerr << " could not allocate memory for Codec Context" ;
171
- return false ;
172
- }
97
+ if (avcodec_parameters_to_context (codecCtx, stream->codecpar ) < 0 )
98
+ {
99
+ printf (" Could not copy codec context.\n " );
100
+ return false ;
101
+ }
173
102
174
- auto ret = avcodec_parameters_to_context (mCodecCtx , mCodecParameters );
175
- if (ret != 0 )
176
- {
177
- printf (" Could not copy codec context.\n " );
178
- return false ;
179
- }
103
+ if (avcodec_open2 (codecCtx, codec, nullptr ) < 0 )
104
+ {
105
+ std::cerr << " Failed to open codec" ;
106
+ return false ;
107
+ }
180
108
181
- // Open codec
182
- if (avcodec_open2 (mCodecCtx , codec, nullptr ) < 0 )
183
- {
184
- std::cerr << " Failed to open Codec" ;
185
- return false ;
109
+ mStreams .emplace_back (std::make_shared<InputStream>(stream, codecCtx));
186
110
}
187
111
188
112
return true ;
0 commit comments