Merge commit '9e4a5eb51b9f3b2bff0ef08e0074b7fe4893075d'
authorJames Almer <jamrial@gmail.com>
Sat, 28 Oct 2017 17:01:25 +0000 (14:01 -0300)
committerJames Almer <jamrial@gmail.com>
Sat, 28 Oct 2017 17:01:25 +0000 (14:01 -0300)
* commit '9e4a5eb51b9f3b2bff0ef08e0074b7fe4893075d':
  avformat: Free the internal codec context at the end

Merged-by: James Almer <jamrial@gmail.com>
1  2 
libavformat/utils.c

@@@ -3824,65 -2466,6 +3824,59 @@@ FF_ENABLE_DEPRECATION_WARNING
          count++;
      }
  
-     // close codecs which were opened in try_decode_frame()
-     for (i = 0; i < ic->nb_streams; i++) {
-         st = ic->streams[i];
-         avcodec_close(st->internal->avctx);
-     }
 +    if (eof_reached) {
 +        int stream_index;
 +        for (stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
 +            st = ic->streams[stream_index];
 +            avctx = st->internal->avctx;
 +            if (!has_codec_parameters(st, NULL)) {
 +                const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
 +                if (codec && !avctx->codec) {
 +                    AVDictionary *opts = NULL;
 +                    if (ic->codec_whitelist)
 +                        av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
 +                    if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
 +                        av_log(ic, AV_LOG_WARNING,
 +                               "Failed to open codec in %s\n",__FUNCTION__);
 +                    av_dict_free(&opts);
 +                }
 +            }
 +
 +            // EOF already reached while reading the stream above.
 +            // So continue with reoordering DTS with whatever delay we have.
 +            if (ic->internal->packet_buffer && !has_decode_delay_been_guessed(st)) {
 +                update_dts_from_pts(ic, stream_index, ic->internal->packet_buffer);
 +            }
 +        }
 +    }
 +
 +    if (flush_codecs) {
 +        AVPacket empty_pkt = { 0 };
 +        int err = 0;
 +        av_init_packet(&empty_pkt);
 +
 +        for (i = 0; i < ic->nb_streams; i++) {
 +
 +            st = ic->streams[i];
 +
 +            /* flush the decoders */
 +            if (st->info->found_decoder == 1) {
 +                do {
 +                    err = try_decode_frame(ic, st, &empty_pkt,
 +                                            (options && i < orig_nb_streams)
 +                                            ? &options[i] : NULL);
 +                } while (err > 0 && !has_codec_parameters(st, NULL));
 +
 +                if (err < 0) {
 +                    av_log(ic, AV_LOG_INFO,
 +                        "decoding for stream %d failed\n", st->index);
 +                }
 +            }
 +        }
 +    }
 +
 +    ff_rfps_calculate(ic);
 +
      for (i = 0; i < ic->nb_streams; i++) {
          st = ic->streams[i];
          avctx = st->internal->avctx;
@@@ -4066,11 -2561,11 +4060,12 @@@ FF_ENABLE_DEPRECATION_WARNING
          st->internal->avctx_inited = 0;
      }
  
 -    estimate_timings(ic, old_offset);
 -
  find_stream_info_err:
      for (i = 0; i < ic->nb_streams; i++) {
 +        st = ic->streams[i];
 +        if (st->info)
 +            av_freep(&st->info->duration_error);
+         avcodec_close(ic->streams[i]->internal->avctx);
          av_freep(&ic->streams[i]->info);
          av_bsf_free(&ic->streams[i]->internal->extract_extradata.bsf);
          av_packet_free(&ic->streams[i]->internal->extract_extradata.pkt);