Advertisement
Guest User

codecpar

a guest
Aug 9th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 33.51 KB | None | 0 0
  1. diff --git a/avconv.c b/avconv.c
  2. index a058b1d..3a2e21d 100644
  3. --- a/avconv.c
  4. +++ b/avconv.c
  5. @@ -175,16 +175,16 @@ static void avconv_cleanup(int ret)
  6.      }
  7.      for (i = 0; i < nb_output_streams; i++) {
  8.          OutputStream *ost = output_streams[i];
  9. -        AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
  10. -        while (bsfc) {
  11. -            AVBitStreamFilterContext *next = bsfc->next;
  12. -            av_bitstream_filter_close(bsfc);
  13. -            bsfc = next;
  14. -        }
  15. -        ost->bitstream_filters = NULL;
  16. +
  17. +        for (j = 0; j < ost->nb_bitstream_filters; j++)
  18. +            av_bsf_free(&ost->bsf_ctx[j]);
  19. +        av_freep(&ost->bsf_ctx);
  20. +        av_freep(&ost->bitstream_filters);
  21. +
  22.          av_frame_free(&ost->filtered_frame);
  23.  
  24.          av_parser_close(ost->parser);
  25. +        avcodec_free_context(&ost->parser_avctx);
  26.  
  27.          av_freep(&ost->forced_keyframes);
  28.          av_freep(&ost->avfilter);
  29. @@ -255,10 +255,9 @@ static void abort_codec_experimental(AVCodec *c, int encoder)
  30.      exit_program(1);
  31.  }
  32.  
  33. -static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  34. +static void write_packet(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  35.  {
  36. -    AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
  37. -    AVCodecContext          *avctx = ost->encoding_needed ? ost->enc_ctx : ost->st->codec;
  38. +    AVStream *st = ost->st;
  39.      int ret;
  40.  
  41.      /*
  42. @@ -268,14 +267,14 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  43.       * Counting encoded video frames needs to be done separately because of
  44.       * reordering, see do_video_out()
  45.       */
  46. -    if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO && avctx->codec)) {
  47. +    if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) {
  48.          if (ost->frame_number >= ost->max_frames) {
  49.              av_packet_unref(pkt);
  50.              return;
  51.          }
  52.          ost->frame_number++;
  53.      }
  54. -    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  55. +    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  56.          uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR,
  57.                                                NULL);
  58.          ost->quality = sd ? *(int *)sd : -1;
  59. @@ -286,31 +285,6 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  60.          }
  61.      }
  62.  
  63. -    while (bsfc) {
  64. -        AVPacket new_pkt = *pkt;
  65. -        int a = av_bitstream_filter_filter(bsfc, avctx, NULL,
  66. -                                           &new_pkt.data, &new_pkt.size,
  67. -                                           pkt->data, pkt->size,
  68. -                                           pkt->flags & AV_PKT_FLAG_KEY);
  69. -        if (a > 0) {
  70. -            av_packet_unref(pkt);
  71. -            new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
  72. -                                           av_buffer_default_free, NULL, 0);
  73. -            if (!new_pkt.buf)
  74. -                exit_program(1);
  75. -        } else if (a < 0) {
  76. -            av_log(NULL, AV_LOG_ERROR, "%s failed for stream %d, codec %s",
  77. -                   bsfc->filter->name, pkt->stream_index,
  78. -                   avctx->codec ? avctx->codec->name : "copy");
  79. -            print_error("", a);
  80. -            if (exit_on_error)
  81. -                exit_program(1);
  82. -        }
  83. -        *pkt = new_pkt;
  84. -
  85. -        bsfc = bsfc->next;
  86. -    }
  87. -
  88.      if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
  89.          ost->last_mux_dts != AV_NOPTS_VALUE &&
  90.          pkt->dts < ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT)) {
  91. @@ -341,6 +315,49 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  92.      }
  93.  }
  94.  
  95. +static void output_packet(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  96. +{
  97. +    int ret = 0;
  98. +
  99. +    /* apply the output bitstream filters, if any */
  100. +    if (ost->nb_bitstream_filters) {
  101. +        int idx;
  102. +
  103. +        ret = av_bsf_send_packet(ost->bsf_ctx[0], pkt);
  104. +        if (ret < 0)
  105. +            goto finish;
  106. +
  107. +        idx = 1;
  108. +        while (idx) {
  109. +            /* get a packet from the previous filter up the chain */
  110. +            ret = av_bsf_receive_packet(ost->bsf_ctx[idx - 1], pkt);
  111. +            if (ret == AVERROR(EAGAIN)) {
  112. +                ret = 0;
  113. +                idx--;
  114. +                continue;
  115. +            } else if (ret < 0)
  116. +                goto finish;
  117. +
  118. +            /* send it to the next filter down the chain or to the muxer */
  119. +            if (idx < ost->nb_bitstream_filters) {
  120. +                ret = av_bsf_send_packet(ost->bsf_ctx[idx], pkt);
  121. +                if (ret < 0)
  122. +                    goto finish;
  123. +                idx++;
  124. +            } else
  125. +                write_packet(s, pkt, ost);
  126. +        }
  127. +    } else
  128. +        write_packet(s, pkt, ost);
  129. +
  130. +finish:
  131. +    if (ret < 0 && ret != AVERROR_EOF) {
  132. +        av_log(NULL, AV_LOG_FATAL, "Error applying bitstream filters to an output "
  133. +               "packet for stream #%d:%d.\n", ost->file_index, ost->index);
  134. +        exit_program(1);
  135. +    }
  136. +}
  137. +
  138.  static int check_recording_time(OutputStream *ost)
  139.  {
  140.      OutputFile *of = output_files[ost->file_index];
  141. @@ -379,7 +396,7 @@ static void do_audio_out(AVFormatContext *s, OutputStream *ost,
  142.  
  143.      if (got_packet) {
  144.          av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base);
  145. -        write_frame(s, &pkt, ost);
  146. +        output_packet(s, &pkt, ost);
  147.      }
  148.  }
  149.  
  150. @@ -448,7 +465,7 @@ static void do_subtitle_out(AVFormatContext *s,
  151.              else
  152.                  pkt.pts += 90 * sub->end_display_time;
  153.          }
  154. -        write_frame(s, &pkt, ost);
  155. +        output_packet(s, &pkt, ost);
  156.      }
  157.  }
  158.  
  159. @@ -515,7 +532,7 @@ static void do_video_out(AVFormatContext *s,
  160.  
  161.      if (got_packet) {
  162.          av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base);
  163. -        write_frame(s, &pkt, ost);
  164. +        output_packet(s, &pkt, ost);
  165.          *frame_size = pkt.size;
  166.  
  167.          /* if two pass, output log */
  168. @@ -982,7 +999,7 @@ static void flush_encoders(void)
  169.                      break;
  170.                  }
  171.                  av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base);
  172. -                write_frame(os, &pkt, ost);
  173. +                output_packet(os, &pkt, ost);
  174.              }
  175.  
  176.              if (stop_encoding)
  177. @@ -1062,7 +1079,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
  178.         && ost->enc_ctx->codec_id != AV_CODEC_ID_MPEG2VIDEO
  179.         && ost->enc_ctx->codec_id != AV_CODEC_ID_VC1
  180.         ) {
  181. -        if (av_parser_change(ost->parser, ost->st->codec,
  182. +        if (av_parser_change(ost->parser, ost->parser_avctx,
  183.                               &opkt.data, &opkt.size,
  184.                               pkt->data, pkt->size,
  185.                               pkt->flags & AV_PKT_FLAG_KEY)) {
  186. @@ -1075,7 +1092,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
  187.          opkt.size = pkt->size;
  188.      }
  189.  
  190. -    write_frame(of->ctx, &opkt, ost);
  191. +    output_packet(of->ctx, &opkt, ost);
  192.  }
  193.  
  194.  int guess_input_channel_layout(InputStream *ist)
  195. @@ -1560,6 +1577,51 @@ static InputStream *get_input_stream(OutputStream *ost)
  196.      return NULL;
  197.  }
  198.  
  199. +static int init_output_bsfs(OutputStream *ost)
  200. +{
  201. +    AVBSFContext *ctx;
  202. +    int i, ret;
  203. +
  204. +    if (!ost->nb_bitstream_filters)
  205. +        return 0;
  206. +
  207. +    ost->bsf_ctx = av_mallocz_array(ost->nb_bitstream_filters, sizeof(*ost->bsf_ctx));
  208. +    if (!ost->bsf_ctx)
  209. +        return AVERROR(ENOMEM);
  210. +
  211. +    for (i = 0; i < ost->nb_bitstream_filters; i++) {
  212. +        ret = av_bsf_alloc(ost->bitstream_filters[i], &ctx);
  213. +        if (ret < 0) {
  214. +            av_log(NULL, AV_LOG_ERROR, "Error allocating a bistream filter context\n");
  215. +            return ret;
  216. +        }
  217. +        ost->bsf_ctx[i] = ctx;
  218. +
  219. +        ret = avcodec_parameters_copy(ctx->par_in,
  220. +                                      i ? ost->bsf_ctx[i - 1]->par_out : ost->st->codecpar);
  221. +        if (ret < 0)
  222. +            return ret;
  223. +
  224. +        ctx->time_base_in = i ? ost->bsf_ctx[i - 1]->time_base_out : ost->st->time_base;
  225. +
  226. +        ret = av_bsf_init(ctx);
  227. +        if (ret < 0) {
  228. +            av_log(NULL, AV_LOG_ERROR, "Error initializing bistream filter: %s\n",
  229. +                   ost->bitstream_filters[i]->name);
  230. +            return ret;
  231. +        }
  232. +    }
  233. +
  234. +    ctx = ost->bsf_ctx[ost->nb_bitstream_filters - 1];
  235. +    ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
  236. +    if (ret < 0)
  237. +        return ret;
  238. +
  239. +    ost->st->time_base = ctx->time_base_out;
  240. +
  241. +    return 0;
  242. +}
  243. +
  244.  static int init_output_stream(OutputStream *ost, char *error, int error_len)
  245.  {
  246.      int ret = 0;
  247. @@ -1601,7 +1663,7 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len)
  248.              av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
  249.                                           "It takes bits/s as argument, not kbits/s\n");
  250.  
  251. -        ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
  252. +        ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);
  253.          if (ret < 0) {
  254.              av_log(NULL, AV_LOG_FATAL,
  255.                     "Error initializing the output stream codec context.\n");
  256. @@ -1635,9 +1697,23 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len)
  257.          ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
  258.          if (ret < 0)
  259.              return ret;
  260. -        ost->st->time_base = ost->st->codec->time_base;
  261. +
  262. +        /*
  263. +         * FIXME: will the codec context used by the parser during streamcopy
  264. +         * This should go away with the new parser API.
  265. +         */
  266. +        ret = avcodec_parameters_to_context(ost->parser_avctx, ost->st->codecpar);
  267. +        if (ret < 0)
  268. +            return ret;
  269.      }
  270.  
  271. +    /* initialize bitstream filters for the output stream
  272. +     * needs to be done here, because the codec id for streamcopy is not
  273. +     * known until now */
  274. +    ret = init_output_bsfs(ost);
  275. +    if (ret < 0)
  276. +        return ret;
  277. +
  278.      return ret;
  279.  }
  280.  
  281. @@ -1719,8 +1795,6 @@ static int transcode_init(void)
  282.  
  283.      /* for each output stream, we compute the right encoding parameters */
  284.      for (i = 0; i < nb_output_streams; i++) {
  285. -        AVCodecContext *enc_ctx;
  286. -        AVCodecContext *dec_ctx = NULL;
  287.          ost = output_streams[i];
  288.          oc  = output_files[ost->file_index]->ctx;
  289.          ist = get_input_stream(ost);
  290. @@ -1728,56 +1802,46 @@ static int transcode_init(void)
  291.          if (ost->attachment_filename)
  292.              continue;
  293.  
  294. -        enc_ctx = ost->stream_copy ? ost->st->codec : ost->enc_ctx;
  295. -
  296.          if (ist) {
  297. -            dec_ctx = ist->dec_ctx;
  298. -
  299.              ost->st->disposition          = ist->st->disposition;
  300. -            enc_ctx->bits_per_raw_sample    = dec_ctx->bits_per_raw_sample;
  301. -            enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
  302.          }
  303.  
  304.          if (ost->stream_copy) {
  305. +            AVCodecParameters *par_dst = ost->st->codecpar;
  306. +            AVCodecParameters *par_src = ist->st->codecpar;
  307.              AVRational sar;
  308.              uint64_t extra_size;
  309.  
  310.              av_assert0(ist && !ost->filter);
  311.  
  312. -            extra_size = (uint64_t)dec_ctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE;
  313. +            extra_size = (uint64_t)par_src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE;
  314.  
  315.              if (extra_size > INT_MAX) {
  316.                  return AVERROR(EINVAL);
  317.              }
  318.  
  319.              /* if stream_copy is selected, no need to decode or encode */
  320. -            enc_ctx->codec_id   = dec_ctx->codec_id;
  321. -            enc_ctx->codec_type = dec_ctx->codec_type;
  322. +            par_dst->codec_id   = par_src->codec_id;
  323. +            par_dst->codec_type = par_src->codec_type;
  324.  
  325. -            if (!enc_ctx->codec_tag) {
  326. +            if (!par_dst->codec_tag) {
  327.                  if (!oc->oformat->codec_tag ||
  328. -                     av_codec_get_id (oc->oformat->codec_tag, dec_ctx->codec_tag) == enc_ctx->codec_id ||
  329. -                     av_codec_get_tag(oc->oformat->codec_tag, dec_ctx->codec_id) <= 0)
  330. -                    enc_ctx->codec_tag = dec_ctx->codec_tag;
  331. +                     av_codec_get_id (oc->oformat->codec_tag, par_src->codec_tag) == par_dst->codec_id ||
  332. +                     av_codec_get_tag(oc->oformat->codec_tag, par_src->codec_id) <= 0)
  333. +                    par_dst->codec_tag = par_src->codec_tag;
  334.              }
  335.  
  336. -            enc_ctx->bit_rate       = dec_ctx->bit_rate;
  337. -            enc_ctx->rc_max_rate    = dec_ctx->rc_max_rate;
  338. -            enc_ctx->rc_buffer_size = dec_ctx->rc_buffer_size;
  339. -            enc_ctx->field_order    = dec_ctx->field_order;
  340. -            enc_ctx->extradata      = av_mallocz(extra_size);
  341. -            if (!enc_ctx->extradata) {
  342. +            par_dst->bit_rate        = par_src->bit_rate;
  343. +            par_dst->field_order     = par_src->field_order;
  344. +            par_dst->chroma_location = par_src->chroma_location;
  345. +            par_dst->extradata       = av_mallocz(extra_size);
  346. +            if (!par_dst->extradata) {
  347.                  return AVERROR(ENOMEM);
  348.              }
  349. -            memcpy(enc_ctx->extradata, dec_ctx->extradata, dec_ctx->extradata_size);
  350. -            enc_ctx->extradata_size = dec_ctx->extradata_size;
  351. -            if (!copy_tb) {
  352. -                enc_ctx->time_base      = dec_ctx->time_base;
  353. -                enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
  354. -                av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
  355. -                          enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
  356. -            } else
  357. -                enc_ctx->time_base = ist->st->time_base;
  358. +            memcpy(par_dst->extradata, par_src->extradata, par_src->extradata_size);
  359. +            par_dst->extradata_size = par_src->extradata_size;
  360. +
  361. +            ost->st->time_base = ist->st->time_base;
  362.  
  363.              if (ist->st->nb_side_data) {
  364.                  ost->st->side_data = av_realloc_array(NULL, ist->st->nb_side_data,
  365. @@ -1799,37 +1863,37 @@ static int transcode_init(void)
  366.                  }
  367.              }
  368.  
  369. -            ost->parser = av_parser_init(enc_ctx->codec_id);
  370. +            ost->parser = av_parser_init(par_dst->codec_id);
  371. +            ost->parser_avctx = avcodec_alloc_context3(NULL);
  372. +            if (!ost->parser_avctx)
  373. +                return AVERROR(ENOMEM);
  374.  
  375. -            switch (enc_ctx->codec_type) {
  376. +            switch (par_dst->codec_type) {
  377.              case AVMEDIA_TYPE_AUDIO:
  378.                  if (audio_volume != 256) {
  379.                      av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
  380.                      exit_program(1);
  381.                  }
  382. -                enc_ctx->channel_layout     = dec_ctx->channel_layout;
  383. -                enc_ctx->sample_rate        = dec_ctx->sample_rate;
  384. -                enc_ctx->channels           = dec_ctx->channels;
  385. -                enc_ctx->frame_size         = dec_ctx->frame_size;
  386. -                enc_ctx->audio_service_type = dec_ctx->audio_service_type;
  387. -                enc_ctx->block_align        = dec_ctx->block_align;
  388. +                par_dst->channel_layout     = par_src->channel_layout;
  389. +                par_dst->sample_rate        = par_src->sample_rate;
  390. +                par_dst->channels           = par_src->channels;
  391. +                par_dst->block_align        = par_src->block_align;
  392.                  break;
  393.              case AVMEDIA_TYPE_VIDEO:
  394. -                enc_ctx->pix_fmt            = dec_ctx->pix_fmt;
  395. -                enc_ctx->width              = dec_ctx->width;
  396. -                enc_ctx->height             = dec_ctx->height;
  397. -                enc_ctx->has_b_frames       = dec_ctx->has_b_frames;
  398. +                par_dst->format             = par_src->format;
  399. +                par_dst->width              = par_src->width;
  400. +                par_dst->height             = par_src->height;
  401.                  if (ost->frame_aspect_ratio)
  402. -                    sar = av_d2q(ost->frame_aspect_ratio * enc_ctx->height / enc_ctx->width, 255);
  403. +                    sar = av_d2q(ost->frame_aspect_ratio * par_dst->height / par_dst->width, 255);
  404.                  else if (ist->st->sample_aspect_ratio.num)
  405.                      sar = ist->st->sample_aspect_ratio;
  406.                  else
  407. -                    sar = dec_ctx->sample_aspect_ratio;
  408. -                ost->st->sample_aspect_ratio = enc_ctx->sample_aspect_ratio = sar;
  409. +                    sar = par_src->sample_aspect_ratio;
  410. +                ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
  411.                  break;
  412.              case AVMEDIA_TYPE_SUBTITLE:
  413. -                enc_ctx->width  = dec_ctx->width;
  414. -                enc_ctx->height = dec_ctx->height;
  415. +                par_dst->width  = par_src->width;
  416. +                par_dst->height = par_src->height;
  417.                  break;
  418.              case AVMEDIA_TYPE_DATA:
  419.              case AVMEDIA_TYPE_ATTACHMENT:
  420. @@ -1838,6 +1902,9 @@ static int transcode_init(void)
  421.                  abort();
  422.              }
  423.          } else {
  424. +            AVCodecContext *enc_ctx = ost->enc_ctx;
  425. +            AVCodecContext *dec_ctx = NULL;
  426. +
  427.              if (!ost->enc) {
  428.                  /* should only happen when a default codec is not present. */
  429.                  snprintf(error, sizeof(error), "Automatic encoder selection "
  430. @@ -1851,6 +1918,13 @@ static int transcode_init(void)
  431.  
  432.              set_encoder_id(output_files[ost->file_index], ost);
  433.  
  434. +            if (ist) {
  435. +                dec_ctx = ist->dec_ctx;
  436. +
  437. +                enc_ctx->bits_per_raw_sample    = dec_ctx->bits_per_raw_sample;
  438. +                enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
  439. +            }
  440. +
  441.              /*
  442.               * We want CFR output if and only if one of those is true:
  443.               * 1) user specified output framerate with -r
  444. diff --git a/avconv.h b/avconv.h
  445. index f0a948f..dc31f00 100644
  446. --- a/avconv.h
  447. +++ b/avconv.h
  448. @@ -326,7 +326,11 @@ typedef struct OutputStream {
  449.      int64_t first_pts;
  450.      /* dts of the last packet sent to the muxer */
  451.      int64_t last_mux_dts;
  452. -    AVBitStreamFilterContext *bitstream_filters;
  453. +
  454. +    int                    nb_bitstream_filters;
  455. +    const AVBitStreamFilter **bitstream_filters;
  456. +    AVBSFContext            **bsf_ctx;
  457. +
  458.      AVCodecContext *enc_ctx;
  459.      AVCodec *enc;
  460.      int64_t max_frames;
  461. @@ -364,6 +368,7 @@ typedef struct OutputStream {
  462.      enum AVPixelFormat pix_fmts[2];
  463.  
  464.      AVCodecParserContext *parser;
  465. +    AVCodecContext       *parser_avctx;
  466.  
  467.      /* stats */
  468.      // combined size of all the packets written
  469. diff --git a/avconv_filter.c b/avconv_filter.c
  470. index aaf5851..3ecad56 100644
  471. --- a/avconv_filter.c
  472. +++ b/avconv_filter.c
  473. @@ -134,7 +134,7 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
  474.          s = input_files[file_idx]->ctx;
  475.  
  476.          for (i = 0; i < s->nb_streams; i++) {
  477. -            if (s->streams[i]->codec->codec_type != type)
  478. +            if (s->streams[i]->codecpar->codec_type != type)
  479.                  continue;
  480.              if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
  481.                  st = s->streams[i];
  482. diff --git a/avconv_opt.c b/avconv_opt.c
  483. index 8fe53e6..ffba54f 100644
  484. --- a/avconv_opt.c
  485. +++ b/avconv_opt.c
  486. @@ -470,11 +470,11 @@ static AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *
  487.  
  488.      MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
  489.      if (codec_name) {
  490. -        AVCodec *codec = find_codec_or_die(codec_name, st->codec->codec_type, 0);
  491. -        st->codec->codec_id = codec->id;
  492. +        AVCodec *codec = find_codec_or_die(codec_name, st->codecpar->codec_type, 0);
  493. +        st->codecpar->codec_id = codec->id;
  494.          return codec;
  495.      } else
  496. -        return avcodec_find_decoder(st->codec->codec_id);
  497. +        return avcodec_find_decoder(st->codecpar->codec_id);
  498.  }
  499.  
  500.  /* Add all the streams from the given input file to the global
  501. @@ -485,7 +485,7 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
  502.  
  503.      for (i = 0; i < ic->nb_streams; i++) {
  504.          AVStream *st = ic->streams[i];
  505. -        AVCodecContext *dec = st->codec;
  506. +        AVCodecParameters *par = st->codecpar;
  507.          InputStream *ist = av_mallocz(sizeof(*ist));
  508.          char *framerate = NULL, *hwaccel = NULL, *hwaccel_device = NULL;
  509.          char *codec_tag = NULL;
  510. @@ -516,11 +516,11 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
  511.              uint32_t tag = strtol(codec_tag, &next, 0);
  512.              if (*next)
  513.                  tag = AV_RL32(codec_tag);
  514. -            st->codec->codec_tag = tag;
  515. +            st->codecpar->codec_tag = tag;
  516.          }
  517.  
  518.          ist->dec = choose_decoder(o, ic, st);
  519. -        ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codec->codec_id, ic, st, ist->dec);
  520. +        ist->decoder_opts = filter_codec_opts(o->g->codec_opts, par->codec_id, ic, st, ist->dec);
  521.  
  522.          ist->dec_ctx = avcodec_alloc_context3(ist->dec);
  523.          if (!ist->dec_ctx) {
  524. @@ -528,13 +528,13 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
  525.              exit_program(1);
  526.          }
  527.  
  528. -        ret = avcodec_copy_context(ist->dec_ctx, dec);
  529. +        ret = avcodec_parameters_to_context(ist->dec_ctx, par);
  530.          if (ret < 0) {
  531.              av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  532.              exit_program(1);
  533.          }
  534.  
  535. -        switch (dec->codec_type) {
  536. +        switch (par->codec_type) {
  537.          case AVMEDIA_TYPE_VIDEO:
  538.              ist->resample_height  = ist->dec_ctx->height;
  539.              ist->resample_width   = ist->dec_ctx->width;
  540. @@ -637,7 +637,7 @@ static void dump_attachment(AVStream *st, const char *filename)
  541.      AVIOContext *out = NULL;
  542.      AVDictionaryEntry *e;
  543.  
  544. -    if (!st->codec->extradata_size) {
  545. +    if (!st->codecpar->extradata_size) {
  546.          av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
  547.                 nb_input_files - 1, st->index);
  548.          return;
  549. @@ -658,7 +658,7 @@ static void dump_attachment(AVStream *st, const char *filename)
  550.          exit_program(1);
  551.      }
  552.  
  553. -    avio_write(out, st->codec->extradata, st->codec->extradata_size);
  554. +    avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
  555.      avio_flush(out);
  556.      avio_close(out);
  557.  }
  558. @@ -895,14 +895,14 @@ static void choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *
  559.  
  560.      MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
  561.      if (!codec_name) {
  562. -        ost->st->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename,
  563. -                                                  NULL, ost->st->codec->codec_type);
  564. -        ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
  565. +        ost->st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->filename,
  566. +                                                     NULL, ost->st->codecpar->codec_type);
  567. +        ost->enc = avcodec_find_encoder(ost->st->codecpar->codec_id);
  568.      } else if (!strcmp(codec_name, "copy"))
  569.          ost->stream_copy = 1;
  570.      else {
  571. -        ost->enc = find_codec_or_die(codec_name, ost->st->codec->codec_type, 1);
  572. -        ost->st->codec->codec_id = ost->enc->id;
  573. +        ost->enc = find_codec_or_die(codec_name, ost->st->codecpar->codec_type, 1);
  574. +        ost->st->codecpar->codec_id = ost->enc->id;
  575.      }
  576.  }
  577.  
  578. @@ -911,8 +911,8 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
  579.      OutputStream *ost;
  580.      AVStream *st = avformat_new_stream(oc, NULL);
  581.      int idx      = oc->nb_streams - 1, ret = 0;
  582. -    char *bsf = NULL, *next, *codec_tag = NULL;
  583. -    AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL;
  584. +    const char *bsfs = NULL;
  585. +    char *next, *codec_tag = NULL;
  586.      double qscale = -1;
  587.  
  588.      if (!st) {
  589. @@ -931,7 +931,7 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
  590.      ost->file_index = nb_output_files - 1;
  591.      ost->index      = idx;
  592.      ost->st         = st;
  593. -    st->codec->codec_type = type;
  594. +    st->codecpar->codec_type = type;
  595.      choose_encoder(o, oc, ost);
  596.  
  597.      ost->enc_ctx = avcodec_alloc_context3(ost->enc);
  598. @@ -978,21 +978,31 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
  599.      ost->max_frames = INT64_MAX;
  600.      MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);
  601.  
  602. -    MATCH_PER_STREAM_OPT(bitstream_filters, str, bsf, oc, st);
  603. -    while (bsf) {
  604. -        if (next = strchr(bsf, ','))
  605. -            *next++ = 0;
  606. -        if (!(bsfc = av_bitstream_filter_init(bsf))) {
  607. +    MATCH_PER_STREAM_OPT(bitstream_filters, str, bsfs, oc, st);
  608. +    while (bsfs && *bsfs) {
  609. +        const AVBitStreamFilter *filter;
  610. +        char *bsf;
  611. +
  612. +        bsf = av_get_token(&bsfs, ",");
  613. +        if (!bsf)
  614. +            exit_program(1);
  615. +
  616. +        filter = av_bsf_get_by_name(bsf);
  617. +        if (!filter) {
  618.              av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\n", bsf);
  619.              exit_program(1);
  620.          }
  621. -        if (bsfc_prev)
  622. -            bsfc_prev->next = bsfc;
  623. -        else
  624. -            ost->bitstream_filters = bsfc;
  625. +        av_freep(&bsf);
  626. +
  627. +        ost->bitstream_filters = av_realloc_array(ost->bitstream_filters,
  628. +                                                  ost->nb_bitstream_filters + 1,
  629. +                                                  sizeof(*ost->bitstream_filters));
  630. +        if (!ost->bitstream_filters)
  631. +            exit_program(1);
  632.  
  633. -        bsfc_prev = bsfc;
  634. -        bsf       = next;
  635. +        ost->bitstream_filters[ost->nb_bitstream_filters++] = filter;
  636. +        if (*bsfs)
  637. +            bsfs++;
  638.      }
  639.  
  640.      MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
  641. @@ -1088,7 +1098,7 @@ static char *get_ost_filters(OptionsContext *o, AVFormatContext *oc,
  642.      else if (filter)
  643.          return av_strdup(filter);
  644.  
  645. -    return av_strdup(st->codec->codec_type == AVMEDIA_TYPE_VIDEO ?
  646. +    return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
  647.                       "null" : "anull");
  648.  }
  649.  
  650. @@ -1524,9 +1534,9 @@ static int open_output_file(OptionsContext *o, const char *filename)
  651.              int area = 0, idx = -1;
  652.              for (i = 0; i < nb_input_streams; i++) {
  653.                  ist = input_streams[i];
  654. -                if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  655. -                    ist->st->codec->width * ist->st->codec->height > area) {
  656. -                    area = ist->st->codec->width * ist->st->codec->height;
  657. +                if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  658. +                    ist->st->codecpar->width * ist->st->codecpar->height > area) {
  659. +                    area = ist->st->codecpar->width * ist->st->codecpar->height;
  660.                      idx = i;
  661.                  }
  662.              }
  663. @@ -1538,9 +1548,9 @@ static int open_output_file(OptionsContext *o, const char *filename)
  664.              int channels = 0, idx = -1;
  665.              for (i = 0; i < nb_input_streams; i++) {
  666.                  ist = input_streams[i];
  667. -                if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  668. -                    ist->st->codec->channels > channels) {
  669. -                    channels = ist->st->codec->channels;
  670. +                if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  671. +                    ist->st->codecpar->channels > channels) {
  672. +                    channels = ist->st->codecpar->channels;
  673.                      idx = i;
  674.                  }
  675.              }
  676. @@ -1550,7 +1560,7 @@ static int open_output_file(OptionsContext *o, const char *filename)
  677.          /* subtitles: pick first */
  678.          if (!o->subtitle_disable && oc->oformat->subtitle_codec != AV_CODEC_ID_NONE) {
  679.              for (i = 0; i < nb_input_streams; i++)
  680. -                if (input_streams[i]->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  681. +                if (input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  682.                      NEW_STREAM(subtitle, i);
  683.                      break;
  684.                  }
  685. @@ -1587,7 +1597,7 @@ loop_end:
  686.                  init_output_filter(ofilter, o, oc);
  687.              } else {
  688.                  ist = input_streams[input_files[map->file_index]->ist_index + map->stream_index];
  689. -                switch (ist->st->codec->codec_type) {
  690. +                switch (ist->st->codecpar->codec_type) {
  691.                  case AVMEDIA_TYPE_VIDEO:    ost = new_video_stream(o, oc);    break;
  692.                  case AVMEDIA_TYPE_AUDIO:    ost = new_audio_stream(o, oc);    break;
  693.                  case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream(o, oc); break;
  694. @@ -1636,8 +1646,8 @@ loop_end:
  695.          ost->stream_copy               = 0;
  696.          ost->source_index              = -1;
  697.          ost->attachment_filename       = o->attachments[i];
  698. -        ost->st->codec->extradata      = attachment;
  699. -        ost->st->codec->extradata_size = len;
  700. +        ost->st->codecpar->extradata      = attachment;
  701. +        ost->st->codecpar->extradata_size = len;
  702.  
  703.          p = strrchr(o->attachments[i], '/');
  704.          av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
  705. @@ -1838,11 +1848,10 @@ static int opt_target(void *optctx, const char *opt, const char *arg)
  706.              int i, j, fr;
  707.              for (j = 0; j < nb_input_files; j++) {
  708.                  for (i = 0; i < input_files[j]->nb_streams; i++) {
  709. -                    AVCodecContext *c = input_files[j]->ctx->streams[i]->codec;
  710. -                    if (c->codec_type != AVMEDIA_TYPE_VIDEO ||
  711. -                        !c->time_base.num)
  712. +                    AVStream *st = input_files[j]->ctx->streams[i];
  713. +                    if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
  714.                          continue;
  715. -                    fr = c->time_base.den * 1000 / c->time_base.num;
  716. +                    fr = st->time_base.den * 1000 / st->time_base.num;
  717.                      if (fr == 25000) {
  718.                          norm = PAL;
  719.                          break;
  720. diff --git a/cmdutils.c b/cmdutils.c
  721. index e4bd74f..b3c4b62 100644
  722. --- a/cmdutils.c
  723. +++ b/cmdutils.c
  724. @@ -1173,10 +1173,11 @@ int show_encoders(void *optctx, const char *opt, const char *arg)
  725.  
  726.  int show_bsfs(void *optctx, const char *opt, const char *arg)
  727.  {
  728. -    AVBitStreamFilter *bsf = NULL;
  729. +    const AVBitStreamFilter *bsf = NULL;
  730. +    void *opaque = NULL;
  731.  
  732.      printf("Bitstream filters:\n");
  733. -    while ((bsf = av_bitstream_filter_next(bsf)))
  734. +    while ((bsf = av_bsf_next(&opaque)))
  735.          printf("%s\n", bsf->name);
  736.      printf("\n");
  737.      return 0;
  738. @@ -1504,12 +1505,12 @@ int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  739.          case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
  740.          default:  av_assert0(0);
  741.          }
  742. -        if (type != st->codec->codec_type)
  743. +        if (type != st->codecpar->codec_type)
  744.              return 0;
  745.          if (*spec++ == ':') { /* possibly followed by :index */
  746.              int i, index = strtol(spec, NULL, 0);
  747.              for (i = 0; i < s->nb_streams; i++)
  748. -                if (s->streams[i]->codec->codec_type == type && index-- == 0)
  749. +                if (s->streams[i]->codecpar->codec_type == type && index-- == 0)
  750.                     return i == st->index;
  751.              return 0;
  752.          }
  753. @@ -1565,17 +1566,17 @@ int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  754.          av_freep(&key);
  755.          return ret;
  756.      } else if (*spec == 'u') {
  757. -        AVCodecContext *avctx = st->codec;
  758. +        AVCodecParameters *par = st->codecpar;
  759.          int val;
  760. -        switch (avctx->codec_type) {
  761. +        switch (par->codec_type) {
  762.          case AVMEDIA_TYPE_AUDIO:
  763. -            val = avctx->sample_rate && avctx->channels;
  764. -            if (avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
  765. +            val = par->sample_rate && par->channels;
  766. +            if (par->format == AV_SAMPLE_FMT_NONE)
  767.                  return 0;
  768.              break;
  769.          case AVMEDIA_TYPE_VIDEO:
  770. -            val = avctx->width && avctx->height;
  771. -            if (avctx->pix_fmt == AV_PIX_FMT_NONE)
  772. +            val = par->width && par->height;
  773. +            if (par->format == AV_PIX_FMT_NONE)
  774.                  return 0;
  775.              break;
  776.          case AVMEDIA_TYPE_UNKNOWN:
  777. @@ -1585,7 +1586,7 @@ int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  778.              val = 1;
  779.              break;
  780.          }
  781. -        return avctx->codec_id != AV_CODEC_ID_NONE && val != 0;
  782. +        return par->codec_id != AV_CODEC_ID_NONE && val != 0;
  783.      } else if (!*spec) /* empty specifier, matches everything */
  784.          return 1;
  785.  
  786. @@ -1607,7 +1608,7 @@ AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  787.          codec            = s->oformat ? avcodec_find_encoder(codec_id)
  788.                                        : avcodec_find_decoder(codec_id);
  789.  
  790. -    switch (st->codec->codec_type) {
  791. +    switch (st->codecpar->codec_type) {
  792.      case AVMEDIA_TYPE_VIDEO:
  793.          prefix  = 'v';
  794.          flags  |= AV_OPT_FLAG_VIDEO_PARAM;
  795. @@ -1664,7 +1665,7 @@ AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  796.          return NULL;
  797.      }
  798.      for (i = 0; i < s->nb_streams; i++)
  799. -        opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
  800. +        opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
  801.                                      s, s->streams[i], NULL);
  802.      return opts;
  803.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement