@@ -39,7 +39,6 @@ static StreamContext *stream_ctx;
3939static int open_input_file (const char *filename)
4040{
4141 int ret;
42- unsigned int i;
4342 ifmt_ctx = nullptr ;
4443
4544 if ((ret = avformat_open_input (&ifmt_ctx, filename, nullptr , nullptr )) < 0 ) {
@@ -57,7 +56,7 @@ static int open_input_file(const char *filename)
5756 if (!stream_ctx)
5857 return AVERROR (ENOMEM );
5958
60- for (i = 0 ; i < ifmt_ctx->nb_streams ; i++) {
59+ for (int i = 0 ; i < ifmt_ctx->nb_streams ; i++) {
6160 AVStream *stream = ifmt_ctx->streams [i];
6261 AVCodec *dec = avcodec_find_decoder (stream->codecpar ->codec_id );
6362 AVCodecContext *codec_ctx;
@@ -76,7 +75,7 @@ static int open_input_file(const char *filename)
7675 " for stream #%u\n " , i);
7776 return ret;
7877 }
79- /* Reencode video & audio and remux subtitles etc. */
78+ /* encode video & audio and remux subtitles etc. */
8079 if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
8180 || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO ) {
8281 if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO )
@@ -102,7 +101,6 @@ static int open_output_file(const char *filename)
102101 AVCodecContext *dec_ctx, *enc_ctx;
103102 AVCodec *encoder;
104103 int ret;
105- unsigned int i;
106104
107105 ofmt_ctx = nullptr ;
108106 avformat_alloc_output_context2 (&ofmt_ctx, nullptr , nullptr , filename);
@@ -111,7 +109,7 @@ static int open_output_file(const char *filename)
111109 return AVERROR_UNKNOWN ;
112110 }
113111
114- for (i = 0 ; i < ifmt_ctx->nb_streams ; i++) {
112+ for (int i = 0 ; i < ifmt_ctx->nb_streams ; i++) {
115113 out_stream = avformat_new_stream (ofmt_ctx, nullptr );
116114 if (!out_stream) {
117115 ALOGE (" Failed allocating output stream\n " );
@@ -135,9 +133,8 @@ static int open_output_file(const char *filename)
135133 return AVERROR (ENOMEM );
136134 }
137135
138- /* In this example, we transcode to same properties (picture size,
139- * sample rate etc.). These properties can be changed for output
140- * streams easily using filters */
136+ /* we transcode to same properties (picture size, sample rate etc.).
137+ * These properties can be changed for output streams using filters */
141138 if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ) {
142139 enc_ctx->height = dec_ctx->height ;
143140 enc_ctx->width = dec_ctx->width ;
@@ -179,7 +176,6 @@ static int open_output_file(const char *filename)
179176 ALOGE (" Elementary stream #%d is of unknown type, cannot proceed\n " , i);
180177 return AVERROR_INVALIDDATA ;
181178 } else {
182- /* if this stream must be remuxed */
183179 ret = avcodec_parameters_copy (out_stream->codecpar , in_stream->codecpar );
184180 if (ret < 0 ) {
185181 ALOGE (" Copying parameters for stream #%u failed\n " , i);
@@ -389,7 +385,7 @@ static int init_filters()
389385 return 0 ;
390386}
391387
392- static int encode_write_frame (AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
388+ static int encode_write_frame (AVFrame *filt_frame, int stream_index, int *got_frame) {
393389 int ret;
394390 int got_frame_local;
395391 AVPacket enc_pkt;
@@ -400,13 +396,10 @@ static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, in
400396 if (!got_frame)
401397 got_frame = &got_frame_local;
402398
403- ALOGE (" Encoding frame\n " );
404- /* encode filtered frame */
405399 enc_pkt.data = nullptr ;
406400 enc_pkt.size = 0 ;
407401 av_init_packet (&enc_pkt);
408- ret = enc_func (stream_ctx[stream_index].enc_ctx , &enc_pkt,
409- filt_frame, got_frame);
402+ ret = enc_func (stream_ctx[stream_index].enc_ctx , &enc_pkt, filt_frame, got_frame);
410403 av_frame_free (&filt_frame);
411404 if (ret < 0 )
412405 return ret;
@@ -419,18 +412,16 @@ static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, in
419412 stream_ctx[stream_index].enc_ctx ->time_base ,
420413 ofmt_ctx->streams [stream_index]->time_base );
421414
422- ALOGE (" Muxing frame\n " );
423415 /* mux encoded frame */
424416 ret = av_interleaved_write_frame (ofmt_ctx, &enc_pkt);
425417 return ret;
426418}
427419
428- static int filter_encode_write_frame (AVFrame *frame, unsigned int stream_index)
420+ static int filter_encode_write_frame (AVFrame *frame, int stream_index)
429421{
430422 int ret;
431- AVFrame *filt_frame ;
423+ AVFrame *filter_frame ;
432424
433- ALOGE (" Pushing decoded frame to filters\n " );
434425 /* push the decoded frame into the filtergraph */
435426 ret = av_buffersrc_add_frame_flags (filter_ctx[stream_index].buffersrc_ctx ,
436427 frame, 0 );
@@ -441,35 +432,29 @@ static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
441432
442433 /* pull filtered frames from the filtergraph */
443434 while (1 ) {
444- filt_frame = av_frame_alloc ();
445- if (!filt_frame ) {
435+ filter_frame = av_frame_alloc ();
436+ if (!filter_frame ) {
446437 ret = AVERROR (ENOMEM );
447438 break ;
448439 }
449- ALOGE (" Pulling filtered frame from filters\n " );
450- ret = av_buffersink_get_frame (filter_ctx[stream_index].buffersink_ctx ,
451- filt_frame);
440+ ret = av_buffersink_get_frame (filter_ctx[stream_index].buffersink_ctx , filter_frame);
452441 if (ret < 0 ) {
453- /* if no more frames for output - returns AVERROR(EAGAIN)
454- * if flushed and no more frames for output - returns AVERROR_EOF
455- * rewrite retcode to 0 to show it as normal procedure completion
456- */
457442 if (ret == AVERROR (EAGAIN ) || ret == AVERROR_EOF )
458443 ret = 0 ;
459- av_frame_free (&filt_frame );
444+ av_frame_free (&filter_frame );
460445 break ;
461446 }
462447
463- filt_frame ->pict_type = AV_PICTURE_TYPE_NONE ;
464- ret = encode_write_frame (filt_frame , stream_index, nullptr );
448+ filter_frame ->pict_type = AV_PICTURE_TYPE_NONE ;
449+ ret = encode_write_frame (filter_frame , stream_index, nullptr );
465450 if (ret < 0 )
466451 break ;
467452 }
468453
469454 return ret;
470455}
471456
472- static int flush_encoder (unsigned int stream_index)
457+ static int flush_encoder (int stream_index)
473458{
474459 int ret;
475460 int got_frame;
@@ -479,7 +464,7 @@ static int flush_encoder(unsigned int stream_index)
479464 return 0 ;
480465
481466 while (1 ) {
482- ALOGE (" Flushing stream #%u encoder\n " , stream_index);
467+ ALOGE (" Flushing stream #%d encoder\n " , stream_index);
483468 ret = encode_write_frame (nullptr , stream_index, &got_frame);
484469 if (ret < 0 )
485470 break ;
@@ -495,8 +480,8 @@ int transcode(const char *input_file, const char *output_file)
495480 AVPacket packet = { .data = nullptr , .size = 0 };
496481 AVFrame *frame = nullptr ;
497482 enum AVMediaType type;
498- unsigned int stream_index;
499- unsigned int i;
483+ int stream_index;
484+ int i;
500485 int got_frame;
501486 int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
502487
@@ -513,11 +498,8 @@ int transcode(const char *input_file, const char *output_file)
513498 break ;
514499 stream_index = packet.stream_index ;
515500 type = ifmt_ctx->streams [packet.stream_index ]->codecpar ->codec_type ;
516- ALOGE (" Demuxer gave frame of stream_index %u\n " ,
517- stream_index);
518501
519- if (filter_ctx[stream_index].filter_graph ) {
520- ALOGE (" Going to re-encode&filter the frame\n " );
502+ if (filter_ctx && filter_ctx[stream_index].filter_graph ) {
521503 frame = av_frame_alloc ();
522504 if (!frame) {
523505 ret = AVERROR (ENOMEM );
@@ -559,16 +541,14 @@ int transcode(const char *input_file, const char *output_file)
559541
560542 /* flush filters and encoders */
561543 for (i = 0 ; i < ifmt_ctx->nb_streams ; i++) {
562- /* flush filter */
563- if (!filter_ctx[i].filter_graph )
544+ if (!filter_ctx || !filter_ctx[i].filter_graph )
564545 continue ;
565546 ret = filter_encode_write_frame (nullptr , i);
566547 if (ret < 0 ) {
567548 ALOGE (" Flushing filter failed\n " );
568549 goto end;
569550 }
570551
571- /* flush encoder */
572552 ret = flush_encoder (i);
573553 if (ret < 0 ) {
574554 ALOGE (" Flushing encoder failed\n " );
@@ -587,7 +567,8 @@ int transcode(const char *input_file, const char *output_file)
587567 if (filter_ctx && filter_ctx[i].filter_graph )
588568 avfilter_graph_free (&filter_ctx[i].filter_graph );
589569 }
590- av_free (filter_ctx);
570+ if (filter_ctx)
571+ av_free (filter_ctx);
591572 av_free (stream_ctx);
592573 avformat_close_input (&ifmt_ctx);
593574 if (ofmt_ctx && !(ofmt_ctx->oformat ->flags & AVFMT_NOFILE ))
0 commit comments