LCOV - code coverage report
Current view: top level - libavcodec - decode.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 713 1126 63.3 %
Date: 2017-12-14 19:11:59 Functions: 39 44 88.6 %

          Line data    Source code
       1             : /*
       2             :  * generic decoding-related code
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include <stdint.h>
      22             : #include <string.h>
      23             : 
      24             : #include "config.h"
      25             : 
      26             : #if CONFIG_ICONV
      27             : # include <iconv.h>
      28             : #endif
      29             : 
      30             : #include "libavutil/avassert.h"
      31             : #include "libavutil/avstring.h"
      32             : #include "libavutil/bprint.h"
      33             : #include "libavutil/common.h"
      34             : #include "libavutil/frame.h"
      35             : #include "libavutil/hwcontext.h"
      36             : #include "libavutil/imgutils.h"
      37             : #include "libavutil/internal.h"
      38             : #include "libavutil/intmath.h"
      39             : 
      40             : #include "avcodec.h"
      41             : #include "bytestream.h"
      42             : #include "decode.h"
      43             : #include "hwaccel.h"
      44             : #include "internal.h"
      45             : #include "thread.h"
      46             : 
      47      373346 : static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt)
      48             : {
      49      373346 :     int size = 0, ret;
      50             :     const uint8_t *data;
      51             :     uint32_t flags;
      52             :     int64_t val;
      53             : 
      54      373346 :     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
      55      373346 :     if (!data)
      56      373344 :         return 0;
      57             : 
      58           2 :     if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
      59           0 :         av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
      60             :                "changes, but PARAM_CHANGE side data was sent to it.\n");
      61           0 :         ret = AVERROR(EINVAL);
      62           0 :         goto fail2;
      63             :     }
      64             : 
      65           2 :     if (size < 4)
      66           0 :         goto fail;
      67             : 
      68           2 :     flags = bytestream_get_le32(&data);
      69           2 :     size -= 4;
      70             : 
      71           2 :     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
      72           0 :         if (size < 4)
      73           0 :             goto fail;
      74           0 :         val = bytestream_get_le32(&data);
      75           0 :         if (val <= 0 || val > INT_MAX) {
      76           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
      77           0 :             ret = AVERROR_INVALIDDATA;
      78           0 :             goto fail2;
      79             :         }
      80           0 :         avctx->channels = val;
      81           0 :         size -= 4;
      82             :     }
      83           2 :     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
      84           0 :         if (size < 8)
      85           0 :             goto fail;
      86           0 :         avctx->channel_layout = bytestream_get_le64(&data);
      87           0 :         size -= 8;
      88             :     }
      89           2 :     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
      90           0 :         if (size < 4)
      91           0 :             goto fail;
      92           0 :         val = bytestream_get_le32(&data);
      93           0 :         if (val <= 0 || val > INT_MAX) {
      94           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
      95           0 :             ret = AVERROR_INVALIDDATA;
      96           0 :             goto fail2;
      97             :         }
      98           0 :         avctx->sample_rate = val;
      99           0 :         size -= 4;
     100             :     }
     101           2 :     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
     102           2 :         if (size < 8)
     103           0 :             goto fail;
     104           2 :         avctx->width  = bytestream_get_le32(&data);
     105           2 :         avctx->height = bytestream_get_le32(&data);
     106           2 :         size -= 8;
     107           2 :         ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
     108           2 :         if (ret < 0)
     109           0 :             goto fail2;
     110             :     }
     111             : 
     112           2 :     return 0;
     113           0 : fail:
     114           0 :     av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
     115           0 :     ret = AVERROR_INVALIDDATA;
     116           0 : fail2:
     117           0 :     if (ret < 0) {
     118           0 :         av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
     119           0 :         if (avctx->err_recognition & AV_EF_EXPLODE)
     120           0 :             return ret;
     121             :     }
     122           0 :     return 0;
     123             : }
     124             : 
     125      374482 : static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
     126             : {
     127      374482 :     int ret = 0;
     128             : 
     129      374482 :     av_packet_unref(avci->last_pkt_props);
     130      374482 :     if (pkt) {
     131      374482 :         ret = av_packet_copy_props(avci->last_pkt_props, pkt);
     132      374482 :         if (!ret)
     133      374482 :             avci->last_pkt_props->size = pkt->size; // HACK: Needed for ff_init_buffer_info().
     134             :     }
     135      374482 :     return ret;
     136             : }
     137             : 
     138        3522 : static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
     139             : {
     140             :     int ret;
     141             : 
     142             :     /* move the original frame to our backup */
     143        3522 :     av_frame_unref(avci->to_free);
     144        3522 :     av_frame_move_ref(avci->to_free, frame);
     145             : 
     146             :     /* now copy everything except the AVBufferRefs back
     147             :      * note that we make a COPY of the side data, so calling av_frame_free() on
     148             :      * the caller's frame will work properly */
     149        3522 :     ret = av_frame_copy_props(frame, avci->to_free);
     150        3522 :     if (ret < 0)
     151           0 :         return ret;
     152             : 
     153        3522 :     memcpy(frame->data,     avci->to_free->data,     sizeof(frame->data));
     154        3522 :     memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
     155        3522 :     if (avci->to_free->extended_data != avci->to_free->data) {
     156           0 :         int planes = avci->to_free->channels;
     157           0 :         int size   = planes * sizeof(*frame->extended_data);
     158             : 
     159           0 :         if (!size) {
     160           0 :             av_frame_unref(frame);
     161           0 :             return AVERROR_BUG;
     162             :         }
     163             : 
     164           0 :         frame->extended_data = av_malloc(size);
     165           0 :         if (!frame->extended_data) {
     166           0 :             av_frame_unref(frame);
     167           0 :             return AVERROR(ENOMEM);
     168             :         }
     169           0 :         memcpy(frame->extended_data, avci->to_free->extended_data,
     170             :                size);
     171             :     } else
     172        3522 :         frame->extended_data = frame->data;
     173             : 
     174        3522 :     frame->format         = avci->to_free->format;
     175        3522 :     frame->width          = avci->to_free->width;
     176        3522 :     frame->height         = avci->to_free->height;
     177        3522 :     frame->channel_layout = avci->to_free->channel_layout;
     178        3522 :     frame->nb_samples     = avci->to_free->nb_samples;
     179        3522 :     frame->channels       = avci->to_free->channels;
     180             : 
     181        3522 :     return 0;
     182             : }
     183             : 
     184     1146788 : static int bsfs_init(AVCodecContext *avctx)
     185             : {
     186     1146788 :     AVCodecInternal *avci = avctx->internal;
     187     1146788 :     DecodeFilterContext *s = &avci->filter;
     188             :     const char *bsfs_str;
     189             :     int ret;
     190             : 
     191     1146788 :     if (s->nb_bsfs)
     192     1137477 :         return 0;
     193             : 
     194        9311 :     bsfs_str = avctx->codec->bsfs ? avctx->codec->bsfs : "null";
     195       27933 :     while (bsfs_str && *bsfs_str) {
     196             :         AVBSFContext **tmp;
     197             :         const AVBitStreamFilter *filter;
     198             :         char *bsf;
     199             : 
     200        9311 :         bsf = av_get_token(&bsfs_str, ",");
     201        9311 :         if (!bsf) {
     202           0 :             ret = AVERROR(ENOMEM);
     203           0 :             goto fail;
     204             :         }
     205             : 
     206        9311 :         filter = av_bsf_get_by_name(bsf);
     207        9311 :         if (!filter) {
     208           0 :             av_log(avctx, AV_LOG_ERROR, "A non-existing bitstream filter %s "
     209             :                    "requested by a decoder. This is a bug, please report it.\n",
     210             :                    bsf);
     211           0 :             ret = AVERROR_BUG;
     212           0 :             av_freep(&bsf);
     213           0 :             goto fail;
     214             :         }
     215        9311 :         av_freep(&bsf);
     216             : 
     217        9311 :         tmp = av_realloc_array(s->bsfs, s->nb_bsfs + 1, sizeof(*s->bsfs));
     218        9311 :         if (!tmp) {
     219           0 :             ret = AVERROR(ENOMEM);
     220           0 :             goto fail;
     221             :         }
     222        9311 :         s->bsfs = tmp;
     223        9311 :         s->nb_bsfs++;
     224             : 
     225        9311 :         ret = av_bsf_alloc(filter, &s->bsfs[s->nb_bsfs - 1]);
     226        9311 :         if (ret < 0)
     227           0 :             goto fail;
     228             : 
     229        9311 :         if (s->nb_bsfs == 1) {
     230             :             /* We do not currently have an API for passing the input timebase into decoders,
     231             :              * but no filters used here should actually need it.
     232             :              * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
     233        9311 :             s->bsfs[s->nb_bsfs - 1]->time_base_in = (AVRational){ 1, 90000 };
     234        9311 :             ret = avcodec_parameters_from_context(s->bsfs[s->nb_bsfs - 1]->par_in,
     235             :                                                   avctx);
     236             :         } else {
     237           0 :             s->bsfs[s->nb_bsfs - 1]->time_base_in = s->bsfs[s->nb_bsfs - 2]->time_base_out;
     238           0 :             ret = avcodec_parameters_copy(s->bsfs[s->nb_bsfs - 1]->par_in,
     239           0 :                                           s->bsfs[s->nb_bsfs - 2]->par_out);
     240             :         }
     241        9311 :         if (ret < 0)
     242           0 :             goto fail;
     243             : 
     244        9311 :         ret = av_bsf_init(s->bsfs[s->nb_bsfs - 1]);
     245        9311 :         if (ret < 0)
     246           0 :             goto fail;
     247             :     }
     248             : 
     249        9311 :     return 0;
     250           0 : fail:
     251           0 :     ff_decode_bsfs_uninit(avctx);
     252           0 :     return ret;
     253             : }
     254             : 
     255             : /* try to get one output packet from the filter chain */
     256      752012 : static int bsfs_poll(AVCodecContext *avctx, AVPacket *pkt)
     257             : {
     258      752012 :     DecodeFilterContext *s = &avctx->internal->filter;
     259             :     int idx, ret;
     260             : 
     261             :     /* start with the last filter in the chain */
     262      752012 :     idx = s->nb_bsfs - 1;
     263     1877325 :     while (idx >= 0) {
     264             :         /* request a packet from the currently selected filter */
     265      752012 :         ret = av_bsf_receive_packet(s->bsfs[idx], pkt);
     266      752012 :         if (ret == AVERROR(EAGAIN)) {
     267             :             /* no packets available, try the next filter up the chain */
     268      373301 :             ret = 0;
     269      373301 :             idx--;
     270      373301 :             continue;
     271      378711 :         } else if (ret < 0 && ret != AVERROR_EOF) {
     272           0 :             return ret;
     273             :         }
     274             : 
     275             :         /* got a packet or EOF -- pass it to the caller or to the next filter
     276             :          * down the chain */
     277      378711 :         if (idx == s->nb_bsfs - 1) {
     278      378711 :             return ret;
     279             :         } else {
     280           0 :             idx++;
     281           0 :             ret = av_bsf_send_packet(s->bsfs[idx], ret < 0 ? NULL : pkt);
     282           0 :             if (ret < 0) {
     283           0 :                 av_log(avctx, AV_LOG_ERROR,
     284             :                        "Error pre-processing a packet before decoding\n");
     285           0 :                 av_packet_unref(pkt);
     286           0 :                 return ret;
     287             :             }
     288             :         }
     289             :     }
     290             : 
     291      373301 :     return AVERROR(EAGAIN);
     292             : }
     293             : 
     294      752014 : int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
     295             : {
     296      752014 :     AVCodecInternal *avci = avctx->internal;
     297             :     int ret;
     298             : 
     299      752014 :     if (avci->draining)
     300           2 :         return AVERROR_EOF;
     301             : 
     302      752012 :     ret = bsfs_poll(avctx, pkt);
     303      752012 :     if (ret == AVERROR_EOF)
     304        5365 :         avci->draining = 1;
     305      752012 :     if (ret < 0)
     306      378666 :         return ret;
     307             : 
     308      373346 :     ret = extract_packet_props(avctx->internal, pkt);
     309      373346 :     if (ret < 0)
     310           0 :         goto finish;
     311             : 
     312      373346 :     ret = apply_param_change(avctx, pkt);
     313      373346 :     if (ret < 0)
     314           0 :         goto finish;
     315             : 
     316      373346 :     if (avctx->codec->receive_frame)
     317          92 :         avci->compat_decode_consumed += pkt->size;
     318             : 
     319      373346 :     return 0;
     320           0 : finish:
     321           0 :     av_packet_unref(pkt);
     322           0 :     return ret;
     323             : }
     324             : 
     325             : /**
     326             :  * Attempt to guess proper monotonic timestamps for decoded video frames
     327             :  * which might have incorrect times. Input timestamps may wrap around, in
     328             :  * which case the output will as well.
     329             :  *
     330             :  * @param pts the pts field of the decoded AVPacket, as passed through
     331             :  * AVFrame.pts
     332             :  * @param dts the dts field of the decoded AVPacket
     333             :  * @return one of the input values, may be AV_NOPTS_VALUE
     334             :  */
     335      394908 : static int64_t guess_correct_pts(AVCodecContext *ctx,
     336             :                                  int64_t reordered_pts, int64_t dts)
     337             : {
     338      394908 :     int64_t pts = AV_NOPTS_VALUE;
     339             : 
     340      394908 :     if (dts != AV_NOPTS_VALUE) {
     341      361148 :         ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
     342      361148 :         ctx->pts_correction_last_dts = dts;
     343       33760 :     } else if (reordered_pts != AV_NOPTS_VALUE)
     344         132 :         ctx->pts_correction_last_dts = reordered_pts;
     345             : 
     346      394908 :     if (reordered_pts != AV_NOPTS_VALUE) {
     347      332715 :         ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
     348      332715 :         ctx->pts_correction_last_pts = reordered_pts;
     349       62193 :     } else if(dts != AV_NOPTS_VALUE)
     350       28565 :         ctx->pts_correction_last_pts = dts;
     351             : 
     352      394908 :     if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
     353      394092 :        && reordered_pts != AV_NOPTS_VALUE)
     354      331900 :         pts = reordered_pts;
     355             :     else
     356       63008 :         pts = dts;
     357             : 
     358      394908 :     return pts;
     359             : }
     360             : 
     361             : /*
     362             :  * The core of the receive_frame_wrapper for the decoders implementing
     363             :  * the simple API. Certain decoders might consume partial packets without
     364             :  * returning any output, so this function needs to be called in a loop until it
     365             :  * returns EAGAIN.
     366             :  **/
     367      786328 : static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
     368             : {
     369      786328 :     AVCodecInternal   *avci = avctx->internal;
     370      786328 :     DecodeSimpleContext *ds = &avci->ds;
     371      786328 :     AVPacket           *pkt = ds->in_pkt;
     372             :     // copy to ensure we do not change pkt
     373             :     int got_frame, actual_got_frame;
     374             :     int ret;
     375             : 
     376      786328 :     if (!pkt->data && !avci->draining) {
     377      751826 :         av_packet_unref(pkt);
     378      751826 :         ret = ff_decode_get_packet(avctx, pkt);
     379      751826 :         if (ret < 0 && ret != AVERROR_EOF)
     380      373209 :             return ret;
     381             :     }
     382             : 
     383             :     // Some codecs (at least wma lossless) will crash when feeding drain packets
     384             :     // after EOF was signaled.
     385      413119 :     if (avci->draining_done)
     386        5648 :         return AVERROR_EOF;
     387             : 
     388      413453 :     if (!pkt->data &&
     389       10708 :         !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
     390        4726 :           avctx->active_thread_type & FF_THREAD_FRAME))
     391        4716 :         return AVERROR_EOF;
     392             : 
     393      402755 :     got_frame = 0;
     394             : 
     395      402755 :     if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
     396         142 :         ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
     397             :     } else {
     398      402613 :         ret = avctx->codec->decode(avctx, frame, &got_frame, pkt);
     399             : 
     400      402613 :         if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
     401      402613 :             frame->pkt_dts = pkt->dts;
     402      402613 :         if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
     403      109844 :             if(!avctx->has_b_frames)
     404       78990 :                 frame->pkt_pos = pkt->pos;
     405             :             //FIXME these should be under if(!avctx->has_b_frames)
     406             :             /* get_buffer is supposed to set frame parameters */
     407      109844 :             if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
     408       20289 :                 if (!frame->sample_aspect_ratio.num)  frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
     409       20289 :                 if (!frame->width)                    frame->width               = avctx->width;
     410       20289 :                 if (!frame->height)                   frame->height              = avctx->height;
     411       20289 :                 if (frame->format == AV_PIX_FMT_NONE) frame->format              = avctx->pix_fmt;
     412             :             }
     413             :         }
     414             :     }
     415      402755 :     emms_c();
     416      402755 :     actual_got_frame = got_frame;
     417             : 
     418      402755 :     if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
     419      109856 :         if (frame->flags & AV_FRAME_FLAG_DISCARD)
     420         139 :             got_frame = 0;
     421      109856 :         if (got_frame)
     422      103382 :             frame->best_effort_timestamp = guess_correct_pts(avctx,
     423             :                                                              frame->pts,
     424             :                                                              frame->pkt_dts);
     425      292899 :     } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
     426             :         uint8_t *side;
     427             :         int side_size;
     428      292899 :         uint32_t discard_padding = 0;
     429      292899 :         uint8_t skip_reason = 0;
     430      292899 :         uint8_t discard_reason = 0;
     431             : 
     432      292899 :         if (ret >= 0 && got_frame) {
     433      291526 :             frame->best_effort_timestamp = guess_correct_pts(avctx,
     434             :                                                              frame->pts,
     435             :                                                              frame->pkt_dts);
     436      291526 :             if (frame->format == AV_SAMPLE_FMT_NONE)
     437           0 :                 frame->format = avctx->sample_fmt;
     438      291526 :             if (!frame->channel_layout)
     439        1253 :                 frame->channel_layout = avctx->channel_layout;
     440      291526 :             if (!frame->channels)
     441           0 :                 frame->channels = avctx->channels;
     442      291526 :             if (!frame->sample_rate)
     443           2 :                 frame->sample_rate = avctx->sample_rate;
     444             :         }
     445             : 
     446      292899 :         side= av_packet_get_side_data(avci->last_pkt_props, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
     447      292899 :         if(side && side_size>=10) {
     448          91 :             avctx->internal->skip_samples = AV_RL32(side) * avctx->internal->skip_samples_multiplier;
     449          91 :             discard_padding = AV_RL32(side + 4);
     450         182 :             av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
     451          91 :                    avctx->internal->skip_samples, (int)discard_padding);
     452          91 :             skip_reason = AV_RL8(side + 8);
     453          91 :             discard_reason = AV_RL8(side + 9);
     454             :         }
     455             : 
     456      292913 :         if ((frame->flags & AV_FRAME_FLAG_DISCARD) && got_frame &&
     457          14 :             !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
     458          14 :             avctx->internal->skip_samples = FFMAX(0, avctx->internal->skip_samples - frame->nb_samples);
     459          14 :             got_frame = 0;
     460             :         }
     461             : 
     462      293041 :         if (avctx->internal->skip_samples > 0 && got_frame &&
     463         142 :             !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
     464         142 :             if(frame->nb_samples <= avctx->internal->skip_samples){
     465         100 :                 got_frame = 0;
     466         100 :                 avctx->internal->skip_samples -= frame->nb_samples;
     467         100 :                 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
     468         100 :                        avctx->internal->skip_samples);
     469             :             } else {
     470          84 :                 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
     471          84 :                                 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
     472          84 :                 if(avctx->pkt_timebase.num && avctx->sample_rate) {
     473          42 :                     int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
     474          42 :                                                    (AVRational){1, avctx->sample_rate},
     475             :                                                    avctx->pkt_timebase);
     476          42 :                     if(frame->pts!=AV_NOPTS_VALUE)
     477          42 :                         frame->pts += diff_ts;
     478             : #if FF_API_PKT_PTS
     479             : FF_DISABLE_DEPRECATION_WARNINGS
     480          42 :                     if(frame->pkt_pts!=AV_NOPTS_VALUE)
     481          42 :                         frame->pkt_pts += diff_ts;
     482             : FF_ENABLE_DEPRECATION_WARNINGS
     483             : #endif
     484          42 :                     if(frame->pkt_dts!=AV_NOPTS_VALUE)
     485          42 :                         frame->pkt_dts += diff_ts;
     486          42 :                     if (frame->pkt_duration >= diff_ts)
     487          42 :                         frame->pkt_duration -= diff_ts;
     488             :                 } else {
     489           0 :                     av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
     490             :                 }
     491          84 :                 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
     492          42 :                        avctx->internal->skip_samples, frame->nb_samples);
     493          42 :                 frame->nb_samples -= avctx->internal->skip_samples;
     494          42 :                 avctx->internal->skip_samples = 0;
     495             :             }
     496             :         }
     497             : 
     498      292904 :         if (discard_padding > 0 && discard_padding <= frame->nb_samples && got_frame &&
     499           5 :             !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
     500           5 :             if (discard_padding == frame->nb_samples) {
     501           2 :                 got_frame = 0;
     502             :             } else {
     503           6 :                 if(avctx->pkt_timebase.num && avctx->sample_rate) {
     504           3 :                     int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
     505           3 :                                                    (AVRational){1, avctx->sample_rate},
     506             :                                                    avctx->pkt_timebase);
     507           3 :                     frame->pkt_duration = diff_ts;
     508             :                 } else {
     509           0 :                     av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
     510             :                 }
     511           3 :                 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
     512             :                        (int)discard_padding, frame->nb_samples);
     513           3 :                 frame->nb_samples -= discard_padding;
     514             :             }
     515             :         }
     516             : 
     517      292899 :         if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && got_frame) {
     518           0 :             AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
     519           0 :             if (fside) {
     520           0 :                 AV_WL32(fside->data, avctx->internal->skip_samples);
     521           0 :                 AV_WL32(fside->data + 4, discard_padding);
     522           0 :                 AV_WL8(fside->data + 8, skip_reason);
     523           0 :                 AV_WL8(fside->data + 9, discard_reason);
     524           0 :                 avctx->internal->skip_samples = 0;
     525             :             }
     526             :         }
     527             :     }
     528             : 
     529      695654 :     if (avctx->codec->type == AVMEDIA_TYPE_AUDIO &&
     530      560933 :         !avci->showed_multi_packet_warning &&
     531      268022 :         ret >= 0 && ret != pkt->size && !(avctx->codec->capabilities & AV_CODEC_CAP_SUBFRAMES)) {
     532          11 :         av_log(avctx, AV_LOG_WARNING, "Multiple frames in a packet.\n");
     533          11 :         avci->showed_multi_packet_warning = 1;
     534             :     }
     535             : 
     536      402755 :     if (!got_frame)
     537        7963 :         av_frame_unref(frame);
     538             : 
     539      402755 :     if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO && !(avctx->flags & AV_CODEC_FLAG_TRUNCATED))
     540      109300 :         ret = pkt->size;
     541             : 
     542             : #if FF_API_AVCTX_TIMEBASE
     543      402755 :     if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
     544       81902 :         avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
     545             : #endif
     546             : 
     547             :     /* do not stop draining when actual_got_frame != 0 or ret < 0 */
     548             :     /* got_frame == 0 but actual_got_frame != 0 when frame is discarded */
     549      402755 :     if (avctx->internal->draining && !actual_got_frame) {
     550         637 :         if (ret < 0) {
     551             :             /* prevent infinite loop if a decoder wrongly always return error on draining */
     552             :             /* reasonable nb_errors_max = maximum b frames + thread count */
     553           1 :             int nb_errors_max = 20 + (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME ?
     554           0 :                                 avctx->thread_count : 1);
     555             : 
     556           1 :             if (avci->nb_draining_errors++ >= nb_errors_max) {
     557           0 :                 av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. "
     558             :                        "Stop draining and force EOF.\n");
     559           0 :                 avci->draining_done = 1;
     560           0 :                 ret = AVERROR_BUG;
     561             :             }
     562             :         } else {
     563         636 :             avci->draining_done = 1;
     564             :         }
     565             :     }
     566             : 
     567      402755 :     avci->compat_decode_consumed += ret;
     568             : 
     569      402755 :     if (ret >= pkt->size || ret < 0) {
     570      374520 :         av_packet_unref(pkt);
     571             :     } else {
     572       28235 :         int consumed = ret;
     573             : 
     574       28235 :         pkt->data                += consumed;
     575       28235 :         pkt->size                -= consumed;
     576       28235 :         avci->last_pkt_props->size -= consumed; // See extract_packet_props() comment.
     577       28235 :         pkt->pts                  = AV_NOPTS_VALUE;
     578       28235 :         pkt->dts                  = AV_NOPTS_VALUE;
     579       28235 :         avci->last_pkt_props->pts = AV_NOPTS_VALUE;
     580       28235 :         avci->last_pkt_props->dts = AV_NOPTS_VALUE;
     581             :     }
     582             : 
     583      402755 :     if (got_frame)
     584      394792 :         av_assert0(frame->buf[0]);
     585             : 
     586      402755 :     return ret < 0 ? ret : 0;
     587             : }
     588             : 
     589      778884 : static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
     590             : {
     591             :     int ret;
     592             : 
     593     1960004 :     while (!frame->buf[0]) {
     594      786328 :         ret = decode_simple_internal(avctx, frame);
     595      786328 :         if (ret < 0)
     596      384092 :             return ret;
     597             :     }
     598             : 
     599      394792 :     return 0;
     600             : }
     601             : 
     602      779152 : static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
     603             : {
     604      779152 :     AVCodecInternal *avci = avctx->internal;
     605             :     int ret;
     606             : 
     607      779152 :     av_assert0(!frame->buf[0]);
     608             : 
     609      779152 :     if (avctx->codec->receive_frame)
     610         268 :         ret = avctx->codec->receive_frame(avctx, frame);
     611             :     else
     612      778884 :         ret = decode_simple_receive_frame(avctx, frame);
     613             : 
     614      779152 :     if (ret == AVERROR_EOF)
     615       10368 :         avci->draining_done = 1;
     616             : 
     617      779152 :     if (!ret) {
     618             :         /* the only case where decode data is not set should be decoders
     619             :          * that do not call ff_get_buffer() */
     620      394964 :         av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) ||
     621             :                    !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
     622             : 
     623      394964 :         if (frame->private_ref) {
     624      374884 :             FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
     625             : 
     626      374884 :             if (fdd->post_process) {
     627           0 :                 ret = fdd->post_process(avctx, frame);
     628           0 :                 if (ret < 0) {
     629           0 :                     av_frame_unref(frame);
     630           0 :                     return ret;
     631             :                 }
     632             :             }
     633             :         }
     634             :     }
     635             : 
     636             :     /* free the per-frame decode data */
     637      779152 :     av_buffer_unref(&frame->private_ref);
     638             : 
     639      779152 :     return ret;
     640             : }
     641             : 
     642      379266 : int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
     643             : {
     644      379266 :     AVCodecInternal *avci = avctx->internal;
     645             :     int ret;
     646             : 
     647      379266 :     if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
     648           0 :         return AVERROR(EINVAL);
     649             : 
     650      379266 :     if (avctx->internal->draining)
     651         617 :         return AVERROR_EOF;
     652             : 
     653      378649 :     if (avpkt && !avpkt->size && avpkt->data)
     654           0 :         return AVERROR(EINVAL);
     655             : 
     656      378649 :     ret = bsfs_init(avctx);
     657      378649 :     if (ret < 0)
     658           0 :         return ret;
     659             : 
     660      378649 :     av_packet_unref(avci->buffer_pkt);
     661      378649 :     if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
     662      373284 :         ret = av_packet_ref(avci->buffer_pkt, avpkt);
     663      373284 :         if (ret < 0)
     664           0 :             return ret;
     665             :     }
     666             : 
     667      378649 :     ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
     668      378649 :     if (ret < 0) {
     669           0 :         av_packet_unref(avci->buffer_pkt);
     670           0 :         return ret;
     671             :     }
     672             : 
     673      378649 :     if (!avci->buffer_frame->buf[0]) {
     674      378649 :         ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
     675      378649 :         if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
     676         514 :             return ret;
     677             :     }
     678             : 
     679      378135 :     return 0;
     680             : }
     681             : 
     682      103382 : static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
     683             : {
     684             :     /* make sure we are noisy about decoders returning invalid cropping data */
     685      206764 :     if (frame->crop_left >= INT_MAX - frame->crop_right        ||
     686      206764 :         frame->crop_top  >= INT_MAX - frame->crop_bottom       ||
     687      206764 :         (frame->crop_left + frame->crop_right) >= frame->width ||
     688      103382 :         (frame->crop_top + frame->crop_bottom) >= frame->height) {
     689           0 :         av_log(avctx, AV_LOG_WARNING,
     690             :                "Invalid cropping information set by a decoder: "
     691             :                "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
     692             :                "(frame size %dx%d). This is a bug, please report it\n",
     693             :                frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
     694             :                frame->width, frame->height);
     695           0 :         frame->crop_left   = 0;
     696           0 :         frame->crop_right  = 0;
     697           0 :         frame->crop_top    = 0;
     698           0 :         frame->crop_bottom = 0;
     699           0 :         return 0;
     700             :     }
     701             : 
     702      103382 :     if (!avctx->apply_cropping)
     703           0 :         return 0;
     704             : 
     705      103382 :     return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
     706             :                                           AV_FRAME_CROP_UNALIGNED : 0);
     707             : }
     708             : 
     709      768139 : int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
     710             : {
     711      768139 :     AVCodecInternal *avci = avctx->internal;
     712             :     int ret;
     713             : 
     714      768139 :     av_frame_unref(frame);
     715             : 
     716      768139 :     if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
     717           0 :         return AVERROR(EINVAL);
     718             : 
     719      768139 :     ret = bsfs_init(avctx);
     720      768139 :     if (ret < 0)
     721           0 :         return ret;
     722             : 
     723      768139 :     if (avci->buffer_frame->buf[0]) {
     724      367636 :         av_frame_move_ref(frame, avci->buffer_frame);
     725             :     } else {
     726      400503 :         ret = decode_receive_frame_internal(avctx, frame);
     727      400503 :         if (ret < 0)
     728      373175 :             return ret;
     729             :     }
     730             : 
     731      394964 :     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
     732      103382 :         ret = apply_cropping(avctx, frame);
     733      103382 :         if (ret < 0) {
     734           0 :             av_frame_unref(frame);
     735           0 :             return ret;
     736             :         }
     737             :     }
     738             : 
     739      394964 :     avctx->frame_number++;
     740             : 
     741      394964 :     return 0;
     742             : }
     743             : 
     744        6189 : static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
     745             :                          int *got_frame, const AVPacket *pkt)
     746             : {
     747        6189 :     AVCodecInternal *avci = avctx->internal;
     748        6189 :     int ret = 0;
     749             : 
     750        6189 :     av_assert0(avci->compat_decode_consumed == 0);
     751             : 
     752        6189 :     if (avci->draining_done && pkt && pkt->size != 0) {
     753           0 :         av_log(avctx, AV_LOG_WARNING, "Got unexpected packet after EOF\n");
     754           0 :         avcodec_flush_buffers(avctx);
     755             :     }
     756             : 
     757        6189 :     *got_frame = 0;
     758        6189 :     avci->compat_decode = 1;
     759             : 
     760        6189 :     if (avci->compat_decode_partial_size > 0 &&
     761           0 :         avci->compat_decode_partial_size != pkt->size) {
     762           0 :         av_log(avctx, AV_LOG_ERROR,
     763             :                "Got unexpected packet size after a partial decode\n");
     764           0 :         ret = AVERROR(EINVAL);
     765           0 :         goto finish;
     766             :     }
     767             : 
     768        6189 :     if (!avci->compat_decode_partial_size) {
     769        6189 :         ret = avcodec_send_packet(avctx, pkt);
     770        6189 :         if (ret == AVERROR_EOF)
     771           6 :             ret = 0;
     772        6183 :         else if (ret == AVERROR(EAGAIN)) {
     773             :             /* we fully drain all the output in each decode call, so this should not
     774             :              * ever happen */
     775           0 :             ret = AVERROR_BUG;
     776           0 :             goto finish;
     777        6183 :         } else if (ret < 0)
     778           0 :             goto finish;
     779             :     }
     780             : 
     781       18534 :     while (ret >= 0) {
     782       12345 :         ret = avcodec_receive_frame(avctx, frame);
     783       12345 :         if (ret < 0) {
     784        6183 :             if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
     785        6183 :                 ret = 0;
     786        6183 :             goto finish;
     787             :         }
     788             : 
     789        6162 :         if (frame != avci->compat_decode_frame) {
     790        6162 :             if (!avctx->refcounted_frames) {
     791        3522 :                 ret = unrefcount_frame(avci, frame);
     792        3522 :                 if (ret < 0)
     793           0 :                     goto finish;
     794             :             }
     795             : 
     796        6162 :             *got_frame = 1;
     797        6162 :             frame = avci->compat_decode_frame;
     798             :         } else {
     799           0 :             if (!avci->compat_decode_warned) {
     800           0 :                 av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
     801             :                        "API cannot return all the frames for this decoder. "
     802             :                        "Some frames will be dropped. Update your code to the "
     803             :                        "new decoding API to fix this.\n");
     804           0 :                 avci->compat_decode_warned = 1;
     805             :             }
     806             :         }
     807             : 
     808       12318 :         if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
     809             :             break;
     810             :     }
     811             : 
     812           6 : finish:
     813        6189 :     if (ret == 0) {
     814             :         /* if there are any bsfs then assume full packet is always consumed */
     815        6189 :         if (avctx->codec->bsfs)
     816           0 :             ret = pkt->size;
     817             :         else
     818        6189 :             ret = FFMIN(avci->compat_decode_consumed, pkt->size);
     819             :     }
     820        6189 :     avci->compat_decode_consumed = 0;
     821        6189 :     avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
     822             : 
     823        6189 :     return ret;
     824             : }
     825             : 
     826        2171 : int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
     827             :                                               int *got_picture_ptr,
     828             :                                               const AVPacket *avpkt)
     829             : {
     830        2171 :     return compat_decode(avctx, picture, got_picture_ptr, avpkt);
     831             : }
     832             : 
     833        4018 : int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
     834             :                                               AVFrame *frame,
     835             :                                               int *got_frame_ptr,
     836             :                                               const AVPacket *avpkt)
     837             : {
     838        4018 :     return compat_decode(avctx, frame, got_frame_ptr, avpkt);
     839             : }
     840             : 
     841        1167 : static void get_subtitle_defaults(AVSubtitle *sub)
     842             : {
     843        1167 :     memset(sub, 0, sizeof(*sub));
     844        1167 :     sub->pts = AV_NOPTS_VALUE;
     845        1167 : }
     846             : 
     847             : #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
     848        1136 : static int recode_subtitle(AVCodecContext *avctx,
     849             :                            AVPacket *outpkt, const AVPacket *inpkt)
     850             : {
     851             : #if CONFIG_ICONV
     852        1136 :     iconv_t cd = (iconv_t)-1;
     853        1136 :     int ret = 0;
     854             :     char *inb, *outb;
     855             :     size_t inl, outl;
     856             :     AVPacket tmp;
     857             : #endif
     858             : 
     859        1136 :     if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
     860        1043 :         return 0;
     861             : 
     862             : #if CONFIG_ICONV
     863          93 :     cd = iconv_open("UTF-8", avctx->sub_charenc);
     864          93 :     av_assert0(cd != (iconv_t)-1);
     865             : 
     866          93 :     inb = inpkt->data;
     867          93 :     inl = inpkt->size;
     868             : 
     869          93 :     if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
     870           0 :         av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
     871           0 :         ret = AVERROR(ENOMEM);
     872           0 :         goto end;
     873             :     }
     874             : 
     875          93 :     ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
     876          93 :     if (ret < 0)
     877           0 :         goto end;
     878          93 :     outpkt->buf  = tmp.buf;
     879          93 :     outpkt->data = tmp.data;
     880          93 :     outpkt->size = tmp.size;
     881          93 :     outb = outpkt->data;
     882          93 :     outl = outpkt->size;
     883             : 
     884         186 :     if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
     885         186 :         iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
     886         186 :         outl >= outpkt->size || inl != 0) {
     887           0 :         ret = FFMIN(AVERROR(errno), -1);
     888           0 :         av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
     889             :                "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
     890           0 :         av_packet_unref(&tmp);
     891           0 :         goto end;
     892             :     }
     893          93 :     outpkt->size -= outl;
     894          93 :     memset(outpkt->data + outpkt->size, 0, outl);
     895             : 
     896          93 : end:
     897          93 :     if (cd != (iconv_t)-1)
     898          93 :         iconv_close(cd);
     899          93 :     return ret;
     900             : #else
     901             :     av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
     902             :     return AVERROR(EINVAL);
     903             : #endif
     904             : }
     905             : 
     906         552 : static int utf8_check(const uint8_t *str)
     907             : {
     908             :     const uint8_t *byte;
     909             :     uint32_t codepoint, min;
     910             : 
     911       46797 :     while (*str) {
     912       45693 :         byte = str;
     913       45693 :         GET_UTF8(codepoint, *(byte++), return 0;);
     914       46389 :         min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
     915         696 :               1 << (5 * (byte - str) - 4);
     916       45693 :         if (codepoint < min || codepoint >= 0x110000 ||
     917       45693 :             codepoint == 0xFFFE /* BOM */ ||
     918           0 :             codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
     919           0 :             return 0;
     920       45693 :         str = byte;
     921             :     }
     922         552 :     return 1;
     923             : }
     924             : 
     925             : #if FF_API_ASS_TIMING
     926           0 : static void insert_ts(AVBPrint *buf, int ts)
     927             : {
     928           0 :     if (ts == -1) {
     929           0 :         av_bprintf(buf, "9:59:59.99,");
     930             :     } else {
     931             :         int h, m, s;
     932             : 
     933           0 :         h = ts/360000;  ts -= 360000*h;
     934           0 :         m = ts/  6000;  ts -=   6000*m;
     935           0 :         s = ts/   100;  ts -=    100*s;
     936           0 :         av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
     937             :     }
     938           0 : }
     939             : 
     940           0 : static int convert_sub_to_old_ass_form(AVSubtitle *sub, const AVPacket *pkt, AVRational tb)
     941             : {
     942             :     int i;
     943             :     AVBPrint buf;
     944             : 
     945           0 :     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
     946             : 
     947           0 :     for (i = 0; i < sub->num_rects; i++) {
     948             :         char *final_dialog;
     949             :         const char *dialog;
     950           0 :         AVSubtitleRect *rect = sub->rects[i];
     951           0 :         int ts_start, ts_duration = -1;
     952             :         long int layer;
     953             : 
     954           0 :         if (rect->type != SUBTITLE_ASS || !strncmp(rect->ass, "Dialogue: ", 10))
     955           0 :             continue;
     956             : 
     957           0 :         av_bprint_clear(&buf);
     958             : 
     959             :         /* skip ReadOrder */
     960           0 :         dialog = strchr(rect->ass, ',');
     961           0 :         if (!dialog)
     962           0 :             continue;
     963           0 :         dialog++;
     964             : 
     965             :         /* extract Layer or Marked */
     966           0 :         layer = strtol(dialog, (char**)&dialog, 10);
     967           0 :         if (*dialog != ',')
     968           0 :             continue;
     969           0 :         dialog++;
     970             : 
     971             :         /* rescale timing to ASS time base (ms) */
     972           0 :         ts_start = av_rescale_q(pkt->pts, tb, av_make_q(1, 100));
     973           0 :         if (pkt->duration != -1)
     974           0 :             ts_duration = av_rescale_q(pkt->duration, tb, av_make_q(1, 100));
     975           0 :         sub->end_display_time = FFMAX(sub->end_display_time, 10 * ts_duration);
     976             : 
     977             :         /* construct ASS (standalone file form with timestamps) string */
     978           0 :         av_bprintf(&buf, "Dialogue: %ld,", layer);
     979           0 :         insert_ts(&buf, ts_start);
     980           0 :         insert_ts(&buf, ts_duration == -1 ? -1 : ts_start + ts_duration);
     981           0 :         av_bprintf(&buf, "%s\r\n", dialog);
     982             : 
     983           0 :         final_dialog = av_strdup(buf.str);
     984           0 :         if (!av_bprint_is_complete(&buf) || !final_dialog) {
     985           0 :             av_freep(&final_dialog);
     986           0 :             av_bprint_finalize(&buf, NULL);
     987           0 :             return AVERROR(ENOMEM);
     988             :         }
     989           0 :         av_freep(&rect->ass);
     990           0 :         rect->ass = final_dialog;
     991             :     }
     992             : 
     993           0 :     av_bprint_finalize(&buf, NULL);
     994           0 :     return 0;
     995             : }
     996             : #endif
     997             : 
     998        1167 : int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
     999             :                              int *got_sub_ptr,
    1000             :                              AVPacket *avpkt)
    1001             : {
    1002        1167 :     int i, ret = 0;
    1003             : 
    1004        1167 :     if (!avpkt->data && avpkt->size) {
    1005           0 :         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
    1006           0 :         return AVERROR(EINVAL);
    1007             :     }
    1008        1167 :     if (!avctx->codec)
    1009           0 :         return AVERROR(EINVAL);
    1010        1167 :     if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
    1011           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
    1012           0 :         return AVERROR(EINVAL);
    1013             :     }
    1014             : 
    1015        1167 :     *got_sub_ptr = 0;
    1016        1167 :     get_subtitle_defaults(sub);
    1017             : 
    1018        1167 :     if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
    1019        1136 :         AVPacket pkt_recoded = *avpkt;
    1020             : 
    1021        1136 :         ret = recode_subtitle(avctx, &pkt_recoded, avpkt);
    1022        1136 :         if (ret < 0) {
    1023           0 :             *got_sub_ptr = 0;
    1024             :         } else {
    1025        1136 :              ret = extract_packet_props(avctx->internal, &pkt_recoded);
    1026        1136 :              if (ret < 0)
    1027           0 :                 return ret;
    1028             : 
    1029        1136 :             if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
    1030        1134 :                 sub->pts = av_rescale_q(avpkt->pts,
    1031        1134 :                                         avctx->pkt_timebase, AV_TIME_BASE_Q);
    1032        1136 :             ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
    1033             :             av_assert1((ret >= 0) >= !!*got_sub_ptr &&
    1034             :                        !!*got_sub_ptr >= !!sub->num_rects);
    1035             : 
    1036             : #if FF_API_ASS_TIMING
    1037        1136 :             if (avctx->sub_text_format == FF_SUB_TEXT_FMT_ASS_WITH_TIMINGS
    1038           0 :                 && *got_sub_ptr && sub->num_rects) {
    1039           0 :                 const AVRational tb = avctx->pkt_timebase.num ? avctx->pkt_timebase
    1040             :                                                               : avctx->time_base;
    1041           0 :                 int err = convert_sub_to_old_ass_form(sub, avpkt, tb);
    1042           0 :                 if (err < 0)
    1043           0 :                     ret = err;
    1044             :             }
    1045             : #endif
    1046             : 
    1047        1516 :             if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
    1048         380 :                 avctx->pkt_timebase.num) {
    1049         380 :                 AVRational ms = { 1, 1000 };
    1050         380 :                 sub->end_display_time = av_rescale_q(avpkt->duration,
    1051             :                                                      avctx->pkt_timebase, ms);
    1052             :             }
    1053             : 
    1054        1136 :             if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
    1055          47 :                 sub->format = 0;
    1056        1089 :             else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
    1057        1089 :                 sub->format = 1;
    1058             : 
    1059        1732 :             for (i = 0; i < sub->num_rects; i++) {
    1060         596 :                 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
    1061           0 :                     av_log(avctx, AV_LOG_ERROR,
    1062             :                            "Invalid UTF-8 in decoded subtitles text; "
    1063             :                            "maybe missing -sub_charenc option\n");
    1064           0 :                     avsubtitle_free(sub);
    1065           0 :                     ret = AVERROR_INVALIDDATA;
    1066           0 :                     break;
    1067             :                 }
    1068             :             }
    1069             : 
    1070        1136 :             if (avpkt->data != pkt_recoded.data) { // did we recode?
    1071             :                 /* prevent from destroying side data from original packet */
    1072          93 :                 pkt_recoded.side_data = NULL;
    1073          93 :                 pkt_recoded.side_data_elems = 0;
    1074             : 
    1075          93 :                 av_packet_unref(&pkt_recoded);
    1076             :             }
    1077             :         }
    1078             : 
    1079        1136 :         if (*got_sub_ptr)
    1080         596 :             avctx->frame_number++;
    1081             :     }
    1082             : 
    1083        1167 :     return ret;
    1084             : }
    1085             : 
    1086        1042 : enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
    1087             :                                               const enum AVPixelFormat *fmt)
    1088             : {
    1089             :     const AVPixFmtDescriptor *desc;
    1090             :     const AVCodecHWConfig *config;
    1091             :     int i, n;
    1092             : 
    1093             :     // If a device was supplied when the codec was opened, assume that the
    1094             :     // user wants to use it.
    1095        1042 :     if (avctx->hw_device_ctx && avctx->codec->hw_configs) {
    1096           0 :         AVHWDeviceContext *device_ctx =
    1097           0 :             (AVHWDeviceContext*)avctx->hw_device_ctx->data;
    1098           0 :         for (i = 0;; i++) {
    1099           0 :             config = &avctx->codec->hw_configs[i]->public;
    1100           0 :             if (!config)
    1101           0 :                 break;
    1102           0 :             if (!(config->methods &
    1103             :                   AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
    1104           0 :                 continue;
    1105           0 :             if (device_ctx->type != config->device_type)
    1106           0 :                 continue;
    1107           0 :             for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
    1108           0 :                 if (config->pix_fmt == fmt[n])
    1109           0 :                     return fmt[n];
    1110             :             }
    1111             :         }
    1112             :     }
    1113             :     // No device or other setup, so we have to choose from things which
    1114             :     // don't any other external information.
    1115             : 
    1116             :     // If the last element of the list is a software format, choose it
    1117             :     // (this should be best software format if any exist).
    1118        1042 :     for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
    1119        1042 :     desc = av_pix_fmt_desc_get(fmt[n - 1]);
    1120        1042 :     if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
    1121        1042 :         return fmt[n - 1];
    1122             : 
    1123             :     // Finally, traverse the list in order and choose the first entry
    1124             :     // with no external dependencies (if there is no hardware configuration
    1125             :     // information available then this just picks the first entry).
    1126           0 :     for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
    1127           0 :         for (i = 0;; i++) {
    1128           0 :             config = avcodec_get_hw_config(avctx->codec, i);
    1129           0 :             if (!config)
    1130           0 :                 break;
    1131           0 :             if (config->pix_fmt == fmt[n])
    1132           0 :                 break;
    1133             :         }
    1134           0 :         if (!config) {
    1135             :             // No specific config available, so the decoder must be able
    1136             :             // to handle this format without any additional setup.
    1137           0 :             return fmt[n];
    1138             :         }
    1139           0 :         if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
    1140             :             // Usable with only internal setup.
    1141           0 :             return fmt[n];
    1142             :         }
    1143             :     }
    1144             : 
    1145             :     // Nothing is usable, give up.
    1146           0 :     return AV_PIX_FMT_NONE;
    1147             : }
    1148             : 
    1149           0 : int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
    1150             :                                 enum AVHWDeviceType dev_type)
    1151             : {
    1152             :     AVHWDeviceContext *device_ctx;
    1153             :     AVHWFramesContext *frames_ctx;
    1154             :     int ret;
    1155             : 
    1156           0 :     if (!avctx->hwaccel)
    1157           0 :         return AVERROR(ENOSYS);
    1158             : 
    1159           0 :     if (avctx->hw_frames_ctx)
    1160           0 :         return 0;
    1161           0 :     if (!avctx->hw_device_ctx) {
    1162           0 :         av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
    1163             :                 "required for hardware accelerated decoding.\n");
    1164           0 :         return AVERROR(EINVAL);
    1165             :     }
    1166             : 
    1167           0 :     device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
    1168           0 :     if (device_ctx->type != dev_type) {
    1169           0 :         av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
    1170             :                "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
    1171             :                av_hwdevice_get_type_name(device_ctx->type));
    1172           0 :         return AVERROR(EINVAL);
    1173             :     }
    1174             : 
    1175           0 :     ret = avcodec_get_hw_frames_parameters(avctx,
    1176             :                                            avctx->hw_device_ctx,
    1177           0 :                                            avctx->hwaccel->pix_fmt,
    1178             :                                            &avctx->hw_frames_ctx);
    1179           0 :     if (ret < 0)
    1180           0 :         return ret;
    1181             : 
    1182           0 :     frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
    1183             : 
    1184             : 
    1185           0 :     if (frames_ctx->initial_pool_size) {
    1186             :         // We guarantee 4 base work surfaces. The function above guarantees 1
    1187             :         // (the absolute minimum), so add the missing count.
    1188           0 :         frames_ctx->initial_pool_size += 3;
    1189             : 
    1190             :         // Add an additional surface per thread is frame threading is enabled.
    1191           0 :         if (avctx->active_thread_type & FF_THREAD_FRAME)
    1192           0 :             frames_ctx->initial_pool_size += avctx->thread_count;
    1193             :     }
    1194             : 
    1195           0 :     ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
    1196           0 :     if (ret < 0) {
    1197           0 :         av_buffer_unref(&avctx->hw_frames_ctx);
    1198           0 :         return ret;
    1199             :     }
    1200             : 
    1201           0 :     return 0;
    1202             : }
    1203             : 
    1204           0 : int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
    1205             :                                      AVBufferRef *device_ref,
    1206             :                                      enum AVPixelFormat hw_pix_fmt,
    1207             :                                      AVBufferRef **out_frames_ref)
    1208             : {
    1209           0 :     AVBufferRef *frames_ref = NULL;
    1210             :     const AVCodecHWConfigInternal *hw_config;
    1211             :     const AVHWAccel *hwa;
    1212             :     int i, ret;
    1213             : 
    1214           0 :     for (i = 0;; i++) {
    1215           0 :         hw_config = avctx->codec->hw_configs[i];
    1216           0 :         if (!hw_config)
    1217           0 :             return AVERROR(ENOENT);
    1218           0 :         if (hw_config->public.pix_fmt == hw_pix_fmt)
    1219           0 :             break;
    1220             :     }
    1221             : 
    1222           0 :     hwa = hw_config->hwaccel;
    1223           0 :     if (!hwa || !hwa->frame_params)
    1224           0 :         return AVERROR(ENOENT);
    1225             : 
    1226           0 :     frames_ref = av_hwframe_ctx_alloc(device_ref);
    1227           0 :     if (!frames_ref)
    1228           0 :         return AVERROR(ENOMEM);
    1229             : 
    1230           0 :     ret = hwa->frame_params(avctx, frames_ref);
    1231           0 :     if (ret >= 0) {
    1232           0 :         *out_frames_ref = frames_ref;
    1233             :     } else {
    1234           0 :         av_buffer_unref(&frames_ref);
    1235             :     }
    1236           0 :     return ret;
    1237             : }
    1238             : 
    1239           0 : static int hwaccel_init(AVCodecContext *avctx,
    1240             :                         const AVCodecHWConfigInternal *hw_config)
    1241             : {
    1242             :     const AVHWAccel *hwaccel;
    1243             :     int err;
    1244             : 
    1245           0 :     hwaccel = hw_config->hwaccel;
    1246           0 :     if (hwaccel->capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL &&
    1247           0 :         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
    1248           0 :         av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
    1249             :                hwaccel->name);
    1250           0 :         return AVERROR_PATCHWELCOME;
    1251             :     }
    1252             : 
    1253           0 :     if (hwaccel->priv_data_size) {
    1254           0 :         avctx->internal->hwaccel_priv_data =
    1255           0 :             av_mallocz(hwaccel->priv_data_size);
    1256           0 :         if (!avctx->internal->hwaccel_priv_data)
    1257           0 :             return AVERROR(ENOMEM);
    1258             :     }
    1259             : 
    1260           0 :     avctx->hwaccel = hwaccel;
    1261           0 :     if (hwaccel->init) {
    1262           0 :         err = hwaccel->init(avctx);
    1263           0 :         if (err < 0) {
    1264           0 :             av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
    1265             :                    "hwaccel initialisation returned error.\n",
    1266             :                    av_get_pix_fmt_name(hw_config->public.pix_fmt));
    1267           0 :             av_freep(&avctx->internal->hwaccel_priv_data);
    1268           0 :             avctx->hwaccel = NULL;
    1269           0 :             return err;
    1270             :         }
    1271             :     }
    1272             : 
    1273           0 :     return 0;
    1274             : }
    1275             : 
    1276        1897 : static void hwaccel_uninit(AVCodecContext *avctx)
    1277             : {
    1278        1897 :     if (avctx->hwaccel && avctx->hwaccel->uninit)
    1279           0 :         avctx->hwaccel->uninit(avctx);
    1280             : 
    1281        1897 :     av_freep(&avctx->internal->hwaccel_priv_data);
    1282             : 
    1283        1897 :     avctx->hwaccel = NULL;
    1284             : 
    1285        1897 :     av_buffer_unref(&avctx->hw_frames_ctx);
    1286        1897 : }
    1287             : 
    1288        1897 : int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
    1289             : {
    1290             :     const AVPixFmtDescriptor *desc;
    1291             :     enum AVPixelFormat *choices;
    1292             :     enum AVPixelFormat ret, user_choice;
    1293             :     const AVCodecHWConfigInternal *hw_config;
    1294             :     const AVCodecHWConfig *config;
    1295             :     int i, n, err;
    1296             : 
    1297             :     // Find end of list.
    1298        1897 :     for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
    1299             :     // Must contain at least one entry.
    1300        1897 :     av_assert0(n >= 1);
    1301             :     // If a software format is available, it must be the last entry.
    1302        1897 :     desc = av_pix_fmt_desc_get(fmt[n - 1]);
    1303        1897 :     if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
    1304             :         // No software format is available.
    1305             :     } else {
    1306        1897 :         avctx->sw_pix_fmt = fmt[n - 1];
    1307             :     }
    1308             : 
    1309        1897 :     choices = av_malloc_array(n + 1, sizeof(*choices));
    1310        1897 :     if (!choices)
    1311           0 :         return AV_PIX_FMT_NONE;
    1312             : 
    1313        1897 :     memcpy(choices, fmt, (n + 1) * sizeof(*choices));
    1314             : 
    1315             :     for (;;) {
    1316             :         // Remove the previous hwaccel, if there was one.
    1317        1897 :         hwaccel_uninit(avctx);
    1318             : 
    1319        1897 :         user_choice = avctx->get_format(avctx, choices);
    1320        1897 :         if (user_choice == AV_PIX_FMT_NONE) {
    1321             :             // Explicitly chose nothing, give up.
    1322           0 :             ret = AV_PIX_FMT_NONE;
    1323           0 :             break;
    1324             :         }
    1325             : 
    1326        1897 :         desc = av_pix_fmt_desc_get(user_choice);
    1327        1897 :         if (!desc) {
    1328           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
    1329             :                    "get_format() callback.\n");
    1330           0 :             ret = AV_PIX_FMT_NONE;
    1331           0 :             break;
    1332             :         }
    1333        1897 :         av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
    1334             :                desc->name);
    1335             : 
    1336        3586 :         for (i = 0; i < n; i++) {
    1337        3586 :             if (choices[i] == user_choice)
    1338        1897 :                 break;
    1339             :         }
    1340        1897 :         if (i == n) {
    1341           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
    1342             :                    "%s not in possible list.\n", desc->name);
    1343           0 :             break;
    1344             :         }
    1345             : 
    1346        1897 :         if (avctx->codec->hw_configs) {
    1347        3606 :             for (i = 0;; i++) {
    1348        5409 :                 hw_config = avctx->codec->hw_configs[i];
    1349        3606 :                 if (!hw_config)
    1350        1803 :                     break;
    1351        1803 :                 if (hw_config->public.pix_fmt == user_choice)
    1352           0 :                     break;
    1353             :             }
    1354             :         } else {
    1355          94 :             hw_config = NULL;
    1356             :         }
    1357             : 
    1358        1897 :         if (!hw_config) {
    1359             :             // No config available, so no extra setup required.
    1360        1897 :             ret = user_choice;
    1361        1897 :             break;
    1362             :         }
    1363           0 :         config = &hw_config->public;
    1364             : 
    1365           0 :         if (config->methods &
    1366           0 :             AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
    1367           0 :             avctx->hw_frames_ctx) {
    1368           0 :             const AVHWFramesContext *frames_ctx =
    1369           0 :                 (AVHWFramesContext*)avctx->hw_frames_ctx->data;
    1370           0 :             if (frames_ctx->format != user_choice) {
    1371           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
    1372             :                        "does not match the format of the provided frames "
    1373             :                        "context.\n", desc->name);
    1374           0 :                 goto try_again;
    1375             :             }
    1376           0 :         } else if (config->methods &
    1377           0 :                    AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
    1378           0 :                    avctx->hw_device_ctx) {
    1379           0 :             const AVHWDeviceContext *device_ctx =
    1380           0 :                 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
    1381           0 :             if (device_ctx->type != config->device_type) {
    1382           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
    1383             :                        "does not match the type of the provided device "
    1384             :                        "context.\n", desc->name);
    1385           0 :                 goto try_again;
    1386             :             }
    1387           0 :         } else if (config->methods &
    1388             :                    AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
    1389             :             // Internal-only setup, no additional configuration.
    1390           0 :         } else if (config->methods &
    1391             :                    AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
    1392             :             // Some ad-hoc configuration we can't see and can't check.
    1393             :         } else {
    1394           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
    1395             :                    "missing configuration.\n", desc->name);
    1396           0 :             goto try_again;
    1397             :         }
    1398           0 :         if (hw_config->hwaccel) {
    1399           0 :             av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel "
    1400             :                    "initialisation.\n", desc->name);
    1401           0 :             err = hwaccel_init(avctx, hw_config);
    1402           0 :             if (err < 0)
    1403           0 :                 goto try_again;
    1404             :         }
    1405           0 :         ret = user_choice;
    1406           0 :         break;
    1407             : 
    1408           0 :     try_again:
    1409           0 :         av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
    1410             :                "get_format() without it.\n", desc->name);
    1411           0 :         for (i = 0; i < n; i++) {
    1412           0 :             if (choices[i] == user_choice)
    1413           0 :                 break;
    1414             :         }
    1415           0 :         for (; i + 1 < n; i++)
    1416           0 :             choices[i] = choices[i + 1];
    1417           0 :         --n;
    1418             :     }
    1419             : 
    1420        1897 :     av_freep(&choices);
    1421        1897 :     return ret;
    1422             : }
    1423             : 
    1424      380248 : static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
    1425             : {
    1426      380248 :     FramePool *pool = avctx->internal->pool;
    1427             :     int i, ret;
    1428             : 
    1429      380248 :     switch (avctx->codec_type) {
    1430       88486 :     case AVMEDIA_TYPE_VIDEO: {
    1431             :         uint8_t *data[4];
    1432             :         int linesize[4];
    1433       88486 :         int size[4] = { 0 };
    1434       88486 :         int w = frame->width;
    1435       88486 :         int h = frame->height;
    1436             :         int tmpsize, unaligned;
    1437             : 
    1438      175251 :         if (pool->format == frame->format &&
    1439      167620 :             pool->width == frame->width && pool->height == frame->height)
    1440      161688 :             return 0;
    1441             : 
    1442        7642 :         avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
    1443             : 
    1444             :         do {
    1445             :             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
    1446             :             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
    1447       14039 :             ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
    1448       14039 :             if (ret < 0)
    1449           0 :                 return ret;
    1450             :             // increase alignment of w for next try (rhs gives the lowest bit set in w)
    1451       14039 :             w += w & ~(w - 1);
    1452             : 
    1453       14039 :             unaligned = 0;
    1454       70195 :             for (i = 0; i < 4; i++)
    1455       56156 :                 unaligned |= linesize[i] % pool->stride_align[i];
    1456       14039 :         } while (unaligned);
    1457             : 
    1458        7642 :         tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
    1459             :                                          NULL, linesize);
    1460        7642 :         if (tmpsize < 0)
    1461           0 :             return -1;
    1462             : 
    1463       21272 :         for (i = 0; i < 3 && data[i + 1]; i++)
    1464       13630 :             size[i] = data[i + 1] - data[i];
    1465        7642 :         size[i] = tmpsize - (data[i] - data[0]);
    1466             : 
    1467       38210 :         for (i = 0; i < 4; i++) {
    1468       30568 :             av_buffer_pool_uninit(&pool->pools[i]);
    1469       30568 :             pool->linesize[i] = linesize[i];
    1470       30568 :             if (size[i]) {
    1471       21272 :                 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
    1472             :                                                      CONFIG_MEMORY_POISONING ?
    1473             :                                                         NULL :
    1474             :                                                         av_buffer_allocz);
    1475       21272 :                 if (!pool->pools[i]) {
    1476           0 :                     ret = AVERROR(ENOMEM);
    1477           0 :                     goto fail;
    1478             :                 }
    1479             :             }
    1480             :         }
    1481        7642 :         pool->format = frame->format;
    1482        7642 :         pool->width  = frame->width;
    1483        7642 :         pool->height = frame->height;
    1484             : 
    1485        7642 :         break;
    1486             :         }
    1487      291762 :     case AVMEDIA_TYPE_AUDIO: {
    1488      291762 :         int ch     = frame->channels; //av_get_channel_layout_nb_channels(frame->channel_layout);
    1489      291762 :         int planar = av_sample_fmt_is_planar(frame->format);
    1490      291762 :         int planes = planar ? ch : 1;
    1491             : 
    1492      582101 :         if (pool->format == frame->format && pool->planes == planes &&
    1493      580678 :             pool->channels == ch && frame->nb_samples == pool->samples)
    1494      284019 :             return 0;
    1495             : 
    1496        7743 :         av_buffer_pool_uninit(&pool->pools[0]);
    1497        7743 :         ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
    1498        7743 :                                          frame->nb_samples, frame->format, 0);
    1499        7743 :         if (ret < 0)
    1500           0 :             goto fail;
    1501             : 
    1502        7743 :         pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
    1503        7743 :         if (!pool->pools[0]) {
    1504           0 :             ret = AVERROR(ENOMEM);
    1505           0 :             goto fail;
    1506             :         }
    1507             : 
    1508        7743 :         pool->format     = frame->format;
    1509        7743 :         pool->planes     = planes;
    1510        7743 :         pool->channels   = ch;
    1511        7743 :         pool->samples = frame->nb_samples;
    1512        7743 :         break;
    1513             :         }
    1514           0 :     default: av_assert0(0);
    1515             :     }
    1516       15385 :     return 0;
    1517           0 : fail:
    1518           0 :     for (i = 0; i < 4; i++)
    1519           0 :         av_buffer_pool_uninit(&pool->pools[i]);
    1520           0 :     pool->format = -1;
    1521           0 :     pool->planes = pool->channels = pool->samples = 0;
    1522           0 :     pool->width  = pool->height = 0;
    1523           0 :     return ret;
    1524             : }
    1525             : 
    1526      291762 : static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
    1527             : {
    1528      291762 :     FramePool *pool = avctx->internal->pool;
    1529      291762 :     int planes = pool->planes;
    1530             :     int i;
    1531             : 
    1532      291762 :     frame->linesize[0] = pool->linesize[0];
    1533             : 
    1534      291762 :     if (planes > AV_NUM_DATA_POINTERS) {
    1535           0 :         frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
    1536           0 :         frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
    1537           0 :         frame->extended_buf  = av_mallocz_array(frame->nb_extended_buf,
    1538             :                                           sizeof(*frame->extended_buf));
    1539           0 :         if (!frame->extended_data || !frame->extended_buf) {
    1540           0 :             av_freep(&frame->extended_data);
    1541           0 :             av_freep(&frame->extended_buf);
    1542           0 :             return AVERROR(ENOMEM);
    1543             :         }
    1544             :     } else {
    1545      291762 :         frame->extended_data = frame->data;
    1546      291762 :         av_assert0(frame->nb_extended_buf == 0);
    1547             :     }
    1548             : 
    1549      739059 :     for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
    1550      447297 :         frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
    1551      447297 :         if (!frame->buf[i])
    1552           0 :             goto fail;
    1553      447297 :         frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
    1554             :     }
    1555      291762 :     for (i = 0; i < frame->nb_extended_buf; i++) {
    1556           0 :         frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
    1557           0 :         if (!frame->extended_buf[i])
    1558           0 :             goto fail;
    1559           0 :         frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
    1560             :     }
    1561             : 
    1562      291762 :     if (avctx->debug & FF_DEBUG_BUFFERS)
    1563           0 :         av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
    1564             : 
    1565      291762 :     return 0;
    1566           0 : fail:
    1567           0 :     av_frame_unref(frame);
    1568           0 :     return AVERROR(ENOMEM);
    1569             : }
    1570             : 
    1571       88486 : static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
    1572             : {
    1573       88486 :     FramePool *pool = s->internal->pool;
    1574       88486 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
    1575             :     int i;
    1576             : 
    1577       88486 :     if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
    1578           0 :         av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
    1579           0 :         return -1;
    1580             :     }
    1581             : 
    1582       88486 :     if (!desc) {
    1583           0 :         av_log(s, AV_LOG_ERROR,
    1584             :             "Unable to get pixel format descriptor for format %s\n",
    1585           0 :             av_get_pix_fmt_name(pic->format));
    1586           0 :         return AVERROR(EINVAL);
    1587             :     }
    1588             : 
    1589       88486 :     memset(pic->data, 0, sizeof(pic->data));
    1590       88486 :     pic->extended_data = pic->data;
    1591             : 
    1592      340657 :     for (i = 0; i < 4 && pool->pools[i]; i++) {
    1593      252171 :         pic->linesize[i] = pool->linesize[i];
    1594             : 
    1595      252171 :         pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
    1596      252171 :         if (!pic->buf[i])
    1597           0 :             goto fail;
    1598             : 
    1599      252171 :         pic->data[i] = pic->buf[i]->data;
    1600             :     }
    1601      544203 :     for (; i < AV_NUM_DATA_POINTERS; i++) {
    1602      455717 :         pic->data[i] = NULL;
    1603      455717 :         pic->linesize[i] = 0;
    1604             :     }
    1605      174839 :     if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
    1606       86353 :         desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
    1607        2432 :         avpriv_set_systematic_pal2((uint32_t *)pic->data[1], pic->format);
    1608             : 
    1609       88486 :     if (s->debug & FF_DEBUG_BUFFERS)
    1610           0 :         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
    1611             : 
    1612       88486 :     return 0;
    1613           0 : fail:
    1614           0 :     av_frame_unref(pic);
    1615           0 :     return AVERROR(ENOMEM);
    1616             : }
    1617             : 
    1618      380248 : int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
    1619             : {
    1620             :     int ret;
    1621             : 
    1622      380248 :     if (avctx->hw_frames_ctx) {
    1623           0 :         ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
    1624           0 :         frame->width  = avctx->coded_width;
    1625           0 :         frame->height = avctx->coded_height;
    1626           0 :         return ret;
    1627             :     }
    1628             : 
    1629      380248 :     if ((ret = update_frame_pool(avctx, frame)) < 0)
    1630           0 :         return ret;
    1631             : 
    1632      380248 :     switch (avctx->codec_type) {
    1633       88486 :     case AVMEDIA_TYPE_VIDEO:
    1634       88486 :         return video_get_buffer(avctx, frame);
    1635      291762 :     case AVMEDIA_TYPE_AUDIO:
    1636      291762 :         return audio_get_buffer(avctx, frame);
    1637           0 :     default:
    1638           0 :         return -1;
    1639             :     }
    1640             : }
    1641             : 
    1642      489109 : static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
    1643             : {
    1644             :     int size;
    1645             :     const uint8_t *side_metadata;
    1646             : 
    1647      489109 :     AVDictionary **frame_md = &frame->metadata;
    1648             : 
    1649      489109 :     side_metadata = av_packet_get_side_data(avpkt,
    1650             :                                             AV_PKT_DATA_STRINGS_METADATA, &size);
    1651      489109 :     return av_packet_unpack_dictionary(side_metadata, size, frame_md);
    1652             : }
    1653             : 
    1654      489109 : int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
    1655             : {
    1656      489109 :     const AVPacket *pkt = avctx->internal->last_pkt_props;
    1657             :     int i;
    1658             :     static const struct {
    1659             :         enum AVPacketSideDataType packet;
    1660             :         enum AVFrameSideDataType frame;
    1661             :     } sd[] = {
    1662             :         { AV_PKT_DATA_REPLAYGAIN ,                AV_FRAME_DATA_REPLAYGAIN },
    1663             :         { AV_PKT_DATA_DISPLAYMATRIX,              AV_FRAME_DATA_DISPLAYMATRIX },
    1664             :         { AV_PKT_DATA_SPHERICAL,                  AV_FRAME_DATA_SPHERICAL },
    1665             :         { AV_PKT_DATA_STEREO3D,                   AV_FRAME_DATA_STEREO3D },
    1666             :         { AV_PKT_DATA_AUDIO_SERVICE_TYPE,         AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
    1667             :         { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
    1668             :         { AV_PKT_DATA_CONTENT_LIGHT_LEVEL,        AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
    1669             :         { AV_PKT_DATA_A53_CC,                     AV_FRAME_DATA_A53_CC },
    1670             :     };
    1671             : 
    1672      489109 :     if (pkt) {
    1673      489109 :         frame->pts = pkt->pts;
    1674             : #if FF_API_PKT_PTS
    1675             : FF_DISABLE_DEPRECATION_WARNINGS
    1676      489109 :         frame->pkt_pts = pkt->pts;
    1677             : FF_ENABLE_DEPRECATION_WARNINGS
    1678             : #endif
    1679      489109 :         frame->pkt_pos      = pkt->pos;
    1680      489109 :         frame->pkt_duration = pkt->duration;
    1681      489109 :         frame->pkt_size     = pkt->size;
    1682             : 
    1683     4401981 :         for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
    1684             :             int size;
    1685     3912872 :             uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
    1686     3912872 :             if (packet_sd) {
    1687         418 :                 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
    1688             :                                                                    sd[i].frame,
    1689             :                                                                    size);
    1690         418 :                 if (!frame_sd)
    1691           0 :                     return AVERROR(ENOMEM);
    1692             : 
    1693         418 :                 memcpy(frame_sd->data, packet_sd, size);
    1694             :             }
    1695             :         }
    1696      489109 :         add_metadata_from_side_data(pkt, frame);
    1697             : 
    1698      489109 :         if (pkt->flags & AV_PKT_FLAG_DISCARD) {
    1699         292 :             frame->flags |= AV_FRAME_FLAG_DISCARD;
    1700             :         } else {
    1701      488817 :             frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
    1702             :         }
    1703             :     }
    1704      489109 :     frame->reordered_opaque = avctx->reordered_opaque;
    1705             : 
    1706      489109 :     if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
    1707      472667 :         frame->color_primaries = avctx->color_primaries;
    1708      489109 :     if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
    1709      472667 :         frame->color_trc = avctx->color_trc;
    1710      489109 :     if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
    1711      471044 :         frame->colorspace = avctx->colorspace;
    1712      489109 :     if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
    1713      469281 :         frame->color_range = avctx->color_range;
    1714      489109 :     if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
    1715      458638 :         frame->chroma_location = avctx->chroma_sample_location;
    1716             : 
    1717      489109 :     switch (avctx->codec->type) {
    1718      181458 :     case AVMEDIA_TYPE_VIDEO:
    1719      181458 :         frame->format              = avctx->pix_fmt;
    1720      181458 :         if (!frame->sample_aspect_ratio.num)
    1721      172055 :             frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
    1722             : 
    1723      290953 :         if (frame->width && frame->height &&
    1724      109495 :             av_image_check_sar(frame->width, frame->height,
    1725             :                                frame->sample_aspect_ratio) < 0) {
    1726           0 :             av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
    1727             :                    frame->sample_aspect_ratio.num,
    1728             :                    frame->sample_aspect_ratio.den);
    1729           0 :             frame->sample_aspect_ratio = (AVRational){ 0, 1 };
    1730             :         }
    1731             : 
    1732      181458 :         break;
    1733      307651 :     case AVMEDIA_TYPE_AUDIO:
    1734      307651 :         if (!frame->sample_rate)
    1735      291762 :             frame->sample_rate    = avctx->sample_rate;
    1736      307651 :         if (frame->format < 0)
    1737      291762 :             frame->format         = avctx->sample_fmt;
    1738      307651 :         if (!frame->channel_layout) {
    1739      291762 :             if (avctx->channel_layout) {
    1740      581018 :                  if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
    1741      290509 :                      avctx->channels) {
    1742           0 :                      av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
    1743             :                             "configuration.\n");
    1744           0 :                      return AVERROR(EINVAL);
    1745             :                  }
    1746             : 
    1747      290509 :                 frame->channel_layout = avctx->channel_layout;
    1748             :             } else {
    1749        1253 :                 if (avctx->channels > FF_SANE_NB_CHANNELS) {
    1750           0 :                     av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
    1751             :                            avctx->channels);
    1752           0 :                     return AVERROR(ENOSYS);
    1753             :                 }
    1754             :             }
    1755             :         }
    1756      307651 :         frame->channels = avctx->channels;
    1757      307651 :         break;
    1758             :     }
    1759      489109 :     return 0;
    1760             : }
    1761             : 
    1762      405816 : int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
    1763             : {
    1764      405816 :     return ff_init_buffer_info(avctx, frame);
    1765             : }
    1766             : 
    1767      380169 : static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
    1768             : {
    1769      380169 :     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
    1770             :         int i;
    1771       88407 :         int num_planes = av_pix_fmt_count_planes(frame->format);
    1772       88407 :         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
    1773       88407 :         int flags = desc ? desc->flags : 0;
    1774       88407 :         if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
    1775        2133 :             num_planes = 2;
    1776      340042 :         for (i = 0; i < num_planes; i++) {
    1777      251635 :             av_assert0(frame->data[i]);
    1778             :         }
    1779             :         // For now do not enforce anything for palette of pseudopal formats
    1780       88407 :         if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PSEUDOPAL))
    1781         299 :             num_planes = 2;
    1782             :         // For formats without data like hwaccel allow unused pointers to be non-NULL.
    1783      543729 :         for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
    1784      455322 :             if (frame->data[i])
    1785           0 :                 av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
    1786      455322 :             frame->data[i] = NULL;
    1787             :         }
    1788             :     }
    1789      380169 : }
    1790             : 
    1791      380169 : static void decode_data_free(void *opaque, uint8_t *data)
    1792             : {
    1793      380169 :     FrameDecodeData *fdd = (FrameDecodeData*)data;
    1794             : 
    1795      380169 :     if (fdd->post_process_opaque_free)
    1796           0 :         fdd->post_process_opaque_free(fdd->post_process_opaque);
    1797             : 
    1798      380169 :     if (fdd->hwaccel_priv_free)
    1799           0 :         fdd->hwaccel_priv_free(fdd->hwaccel_priv);
    1800             : 
    1801      380169 :     av_freep(&fdd);
    1802      380169 : }
    1803             : 
    1804      380169 : int ff_attach_decode_data(AVFrame *frame)
    1805             : {
    1806             :     AVBufferRef *fdd_buf;
    1807             :     FrameDecodeData *fdd;
    1808             : 
    1809             :     av_assert1(!frame->private_ref);
    1810      380169 :     av_buffer_unref(&frame->private_ref);
    1811             : 
    1812      380169 :     fdd = av_mallocz(sizeof(*fdd));
    1813      380169 :     if (!fdd)
    1814           0 :         return AVERROR(ENOMEM);
    1815             : 
    1816      380169 :     fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
    1817             :                                NULL, AV_BUFFER_FLAG_READONLY);
    1818      380169 :     if (!fdd_buf) {
    1819           0 :         av_freep(&fdd);
    1820           0 :         return AVERROR(ENOMEM);
    1821             :     }
    1822             : 
    1823      380169 :     frame->private_ref = fdd_buf;
    1824             : 
    1825      380169 :     return 0;
    1826             : }
    1827             : 
    1828      380169 : static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
    1829             : {
    1830      380169 :     const AVHWAccel *hwaccel = avctx->hwaccel;
    1831      380169 :     int override_dimensions = 1;
    1832             :     int ret;
    1833             : 
    1834      380169 :     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
    1835       88407 :         if ((ret = av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) {
    1836           0 :             av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
    1837           0 :             return AVERROR(EINVAL);
    1838             :         }
    1839             : 
    1840       88407 :         if (frame->width <= 0 || frame->height <= 0) {
    1841       78800 :             frame->width  = FFMAX(avctx->width,  AV_CEIL_RSHIFT(avctx->coded_width,  avctx->lowres));
    1842       78800 :             frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
    1843       78800 :             override_dimensions = 0;
    1844             :         }
    1845             : 
    1846       88407 :         if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
    1847           0 :             av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
    1848           0 :             return AVERROR(EINVAL);
    1849             :         }
    1850             :     }
    1851      380169 :     ret = ff_decode_frame_props(avctx, frame);
    1852      380169 :     if (ret < 0)
    1853           0 :         return ret;
    1854             : 
    1855      380169 :     if (hwaccel) {
    1856           0 :         if (hwaccel->alloc_frame) {
    1857           0 :             ret = hwaccel->alloc_frame(avctx, frame);
    1858           0 :             goto end;
    1859             :         }
    1860             :     } else
    1861      380169 :         avctx->sw_pix_fmt = avctx->pix_fmt;
    1862             : 
    1863      380169 :     ret = avctx->get_buffer2(avctx, frame, flags);
    1864      380169 :     if (ret < 0)
    1865           0 :         goto end;
    1866             : 
    1867      380169 :     validate_avframe_allocation(avctx, frame);
    1868             : 
    1869      380169 :     ret = ff_attach_decode_data(frame);
    1870      380169 :     if (ret < 0)
    1871           0 :         goto end;
    1872             : 
    1873      760338 : end:
    1874      458969 :     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
    1875       78800 :         !(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
    1876       46804 :         frame->width  = avctx->width;
    1877       46804 :         frame->height = avctx->height;
    1878             :     }
    1879             : 
    1880      380169 :     if (ret < 0)
    1881           0 :         av_frame_unref(frame);
    1882             : 
    1883      380169 :     return ret;
    1884             : }
    1885             : 
    1886      380169 : int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
    1887             : {
    1888      380169 :     int ret = get_buffer_internal(avctx, frame, flags);
    1889      380169 :     if (ret < 0) {
    1890           0 :         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
    1891           0 :         frame->width = frame->height = 0;
    1892             :     }
    1893      380169 :     return ret;
    1894             : }
    1895             : 
    1896        7238 : static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
    1897             : {
    1898             :     AVFrame *tmp;
    1899             :     int ret;
    1900             : 
    1901        7238 :     av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
    1902             : 
    1903        7238 :     if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
    1904           0 :         av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
    1905           0 :                frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
    1906           0 :         av_frame_unref(frame);
    1907             :     }
    1908             : 
    1909        7238 :     ff_init_buffer_info(avctx, frame);
    1910             : 
    1911        7238 :     if (!frame->data[0])
    1912         121 :         return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
    1913             : 
    1914        7117 :     if (av_frame_is_writable(frame))
    1915        5580 :         return ff_decode_frame_props(avctx, frame);
    1916             : 
    1917        1537 :     tmp = av_frame_alloc();
    1918        1537 :     if (!tmp)
    1919           0 :         return AVERROR(ENOMEM);
    1920             : 
    1921        1537 :     av_frame_move_ref(tmp, frame);
    1922             : 
    1923        1537 :     ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
    1924        1537 :     if (ret < 0) {
    1925           0 :         av_frame_free(&tmp);
    1926           0 :         return ret;
    1927             :     }
    1928             : 
    1929        1537 :     av_frame_copy(frame, tmp);
    1930        1537 :     av_frame_free(&tmp);
    1931             : 
    1932        1537 :     return 0;
    1933             : }
    1934             : 
    1935        7238 : int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
    1936             : {
    1937        7238 :     int ret = reget_buffer_internal(avctx, frame);
    1938        7238 :     if (ret < 0)
    1939           0 :         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
    1940        7238 :     return ret;
    1941             : }
    1942             : 
    1943          28 : void avcodec_flush_buffers(AVCodecContext *avctx)
    1944             : {
    1945          28 :     avctx->internal->draining      = 0;
    1946          28 :     avctx->internal->draining_done = 0;
    1947          28 :     avctx->internal->nb_draining_errors = 0;
    1948          28 :     av_frame_unref(avctx->internal->buffer_frame);
    1949          28 :     av_frame_unref(avctx->internal->compat_decode_frame);
    1950          28 :     av_packet_unref(avctx->internal->buffer_pkt);
    1951          28 :     avctx->internal->buffer_pkt_valid = 0;
    1952             : 
    1953          28 :     av_packet_unref(avctx->internal->ds.in_pkt);
    1954             : 
    1955          28 :     if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
    1956           0 :         ff_thread_flush(avctx);
    1957          28 :     else if (avctx->codec->flush)
    1958           0 :         avctx->codec->flush(avctx);
    1959             : 
    1960          28 :     avctx->pts_correction_last_pts =
    1961          28 :     avctx->pts_correction_last_dts = INT64_MIN;
    1962             : 
    1963          28 :     ff_decode_bsfs_uninit(avctx);
    1964             : 
    1965          28 :     if (!avctx->refcounted_frames)
    1966          28 :         av_frame_unref(avctx->internal->to_free);
    1967          28 : }
    1968             : 
    1969       17065 : void ff_decode_bsfs_uninit(AVCodecContext *avctx)
    1970             : {
    1971       17065 :     DecodeFilterContext *s = &avctx->internal->filter;
    1972             :     int i;
    1973             : 
    1974       26376 :     for (i = 0; i < s->nb_bsfs; i++)
    1975        9311 :         av_bsf_free(&s->bsfs[i]);
    1976       17065 :     av_freep(&s->bsfs);
    1977       17065 :     s->nb_bsfs = 0;
    1978       17065 : }

Generated by: LCOV version 1.13