LCOV - code coverage report
Current view: top level - libavutil - frame.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 356 516 69.0 %
Date: 2017-12-14 19:11:59 Functions: 27 52 51.9 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include "channel_layout.h"
      20             : #include "avassert.h"
      21             : #include "buffer.h"
      22             : #include "common.h"
      23             : #include "dict.h"
      24             : #include "frame.h"
      25             : #include "imgutils.h"
      26             : #include "mem.h"
      27             : #include "samplefmt.h"
      28             : 
      29             : 
      30             : static AVFrameSideData *frame_new_side_data(AVFrame *frame,
      31             :                                             enum AVFrameSideDataType type,
      32             :                                             AVBufferRef *buf);
      33             : 
      34             : #if FF_API_FRAME_GET_SET
      35           0 : MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
      36           0 : MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
      37           0 : MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
      38           0 : MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
      39           0 : MAKE_ACCESSORS(AVFrame, frame, int,     channels)
      40           0 : MAKE_ACCESSORS(AVFrame, frame, int,     sample_rate)
      41           0 : MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
      42           0 : MAKE_ACCESSORS(AVFrame, frame, int,     decode_error_flags)
      43           0 : MAKE_ACCESSORS(AVFrame, frame, int,     pkt_size)
      44           0 : MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
      45           0 : MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
      46             : #endif
      47             : 
      48             : #define CHECK_CHANNELS_CONSISTENCY(frame) \
      49             :     av_assert2(!(frame)->channel_layout || \
      50             :                (frame)->channels == \
      51             :                av_get_channel_layout_nb_channels((frame)->channel_layout))
      52             : 
      53             : #if FF_API_FRAME_QP
      54       11967 : int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
      55             : {
      56       11967 :     av_buffer_unref(&f->qp_table_buf);
      57             : 
      58       11967 :     f->qp_table_buf = buf;
      59             : 
      60             : FF_DISABLE_DEPRECATION_WARNINGS
      61       11967 :     f->qscale_table = buf->data;
      62       11967 :     f->qstride      = stride;
      63       11967 :     f->qscale_type  = qp_type;
      64             : FF_ENABLE_DEPRECATION_WARNINGS
      65             : 
      66       11967 :     return 0;
      67             : }
      68             : 
      69          56 : int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
      70             : {
      71             : FF_DISABLE_DEPRECATION_WARNINGS
      72          56 :     *stride = f->qstride;
      73          56 :     *type   = f->qscale_type;
      74             : FF_ENABLE_DEPRECATION_WARNINGS
      75             : 
      76          56 :     if (!f->qp_table_buf)
      77          36 :         return NULL;
      78             : 
      79          20 :     return f->qp_table_buf->data;
      80             : }
      81             : #endif
      82             : 
      83          63 : const char *av_get_colorspace_name(enum AVColorSpace val)
      84             : {
      85             :     static const char * const name[] = {
      86             :         [AVCOL_SPC_RGB]       = "GBR",
      87             :         [AVCOL_SPC_BT709]     = "bt709",
      88             :         [AVCOL_SPC_FCC]       = "fcc",
      89             :         [AVCOL_SPC_BT470BG]   = "bt470bg",
      90             :         [AVCOL_SPC_SMPTE170M] = "smpte170m",
      91             :         [AVCOL_SPC_SMPTE240M] = "smpte240m",
      92             :         [AVCOL_SPC_YCOCG]     = "YCgCo",
      93             :     };
      94          63 :     if ((unsigned)val >= FF_ARRAY_ELEMS(name))
      95           0 :         return NULL;
      96          63 :     return name[val];
      97             : }
      98             : 
      99     5547073 : static void get_frame_defaults(AVFrame *frame)
     100             : {
     101     5547073 :     if (frame->extended_data != frame->data)
     102     2192305 :         av_freep(&frame->extended_data);
     103             : 
     104     5547073 :     memset(frame, 0, sizeof(*frame));
     105             : 
     106     5547073 :     frame->pts                   =
     107     5547073 :     frame->pkt_dts               = AV_NOPTS_VALUE;
     108             : #if FF_API_PKT_PTS
     109             : FF_DISABLE_DEPRECATION_WARNINGS
     110     5547073 :     frame->pkt_pts               = AV_NOPTS_VALUE;
     111             : FF_ENABLE_DEPRECATION_WARNINGS
     112             : #endif
     113     5547073 :     frame->best_effort_timestamp = AV_NOPTS_VALUE;
     114     5547073 :     frame->pkt_duration        = 0;
     115     5547073 :     frame->pkt_pos             = -1;
     116     5547073 :     frame->pkt_size            = -1;
     117     5547073 :     frame->key_frame           = 1;
     118     5547073 :     frame->sample_aspect_ratio = (AVRational){ 0, 1 };
     119     5547073 :     frame->format              = -1; /* unknown */
     120     5547073 :     frame->extended_data       = frame->data;
     121     5547073 :     frame->color_primaries     = AVCOL_PRI_UNSPECIFIED;
     122     5547073 :     frame->color_trc           = AVCOL_TRC_UNSPECIFIED;
     123     5547073 :     frame->colorspace          = AVCOL_SPC_UNSPECIFIED;
     124     5547073 :     frame->color_range         = AVCOL_RANGE_UNSPECIFIED;
     125     5547073 :     frame->chroma_location     = AVCHROMA_LOC_UNSPECIFIED;
     126     5547073 :     frame->flags               = 0;
     127     5547073 : }
     128             : 
     129      101224 : static void free_side_data(AVFrameSideData **ptr_sd)
     130             : {
     131      101224 :     AVFrameSideData *sd = *ptr_sd;
     132             : 
     133      101224 :     av_buffer_unref(&sd->buf);
     134      101224 :     av_dict_free(&sd->metadata);
     135      101224 :     av_freep(ptr_sd);
     136      101224 : }
     137             : 
     138     3354770 : static void wipe_side_data(AVFrame *frame)
     139             : {
     140             :     int i;
     141             : 
     142     3455994 :     for (i = 0; i < frame->nb_side_data; i++) {
     143      101224 :         free_side_data(&frame->side_data[i]);
     144             :     }
     145     3354770 :     frame->nb_side_data = 0;
     146             : 
     147     3354770 :     av_freep(&frame->side_data);
     148     3354770 : }
     149             : 
     150     1029930 : AVFrame *av_frame_alloc(void)
     151             : {
     152     1029930 :     AVFrame *frame = av_mallocz(sizeof(*frame));
     153             : 
     154     1029930 :     if (!frame)
     155           0 :         return NULL;
     156             : 
     157     1029930 :     frame->extended_data = NULL;
     158     1029930 :     get_frame_defaults(frame);
     159             : 
     160     1029930 :     return frame;
     161             : }
     162             : 
     163     1385287 : void av_frame_free(AVFrame **frame)
     164             : {
     165     1385287 :     if (!frame || !*frame)
     166      355312 :         return;
     167             : 
     168     1029975 :     av_frame_unref(*frame);
     169     1029975 :     av_freep(frame);
     170             : }
     171             : 
     172        1375 : static int get_video_buffer(AVFrame *frame, int align)
     173             : {
     174        1375 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
     175             :     int ret, i;
     176             : 
     177        1375 :     if (!desc)
     178           0 :         return AVERROR(EINVAL);
     179             : 
     180        1375 :     if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
     181           0 :         return ret;
     182             : 
     183        1375 :     if (!frame->linesize[0]) {
     184        1375 :         if (align <= 0)
     185           0 :             align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
     186             : 
     187        3690 :         for(i=1; i<=align; i+=i) {
     188        3690 :             ret = av_image_fill_linesizes(frame->linesize, frame->format,
     189        3690 :                                           FFALIGN(frame->width, i));
     190        3690 :             if (ret < 0)
     191           0 :                 return ret;
     192        3690 :             if (!(frame->linesize[0] & (align-1)))
     193        1375 :                 break;
     194             :         }
     195             : 
     196        4632 :         for (i = 0; i < 4 && frame->linesize[i]; i++)
     197        3257 :             frame->linesize[i] = FFALIGN(frame->linesize[i], align);
     198             :     }
     199             : 
     200        4632 :     for (i = 0; i < 4 && frame->linesize[i]; i++) {
     201        3257 :         int h = FFALIGN(frame->height, 32);
     202        3257 :         if (i == 1 || i == 2)
     203        1778 :             h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
     204             : 
     205        3257 :         frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
     206        3257 :         if (!frame->buf[i])
     207           0 :             goto fail;
     208             : 
     209        3257 :         frame->data[i] = frame->buf[i]->data;
     210             :     }
     211        1375 :     if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
     212           0 :         av_buffer_unref(&frame->buf[1]);
     213           0 :         frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
     214           0 :         if (!frame->buf[1])
     215           0 :             goto fail;
     216           0 :         frame->data[1] = frame->buf[1]->data;
     217             :     }
     218             : 
     219        1375 :     frame->extended_data = frame->data;
     220             : 
     221        1375 :     return 0;
     222           0 : fail:
     223           0 :     av_frame_unref(frame);
     224           0 :     return AVERROR(ENOMEM);
     225             : }
     226             : 
     227          69 : static int get_audio_buffer(AVFrame *frame, int align)
     228             : {
     229             :     int channels;
     230          69 :     int planar   = av_sample_fmt_is_planar(frame->format);
     231             :     int planes;
     232             :     int ret, i;
     233             : 
     234          69 :     if (!frame->channels)
     235          29 :         frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
     236             : 
     237          69 :     channels = frame->channels;
     238          69 :     planes = planar ? channels : 1;
     239             : 
     240             :     CHECK_CHANNELS_CONSISTENCY(frame);
     241          69 :     if (!frame->linesize[0]) {
     242          69 :         ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
     243          69 :                                          frame->nb_samples, frame->format,
     244             :                                          align);
     245          69 :         if (ret < 0)
     246           0 :             return ret;
     247             :     }
     248             : 
     249          69 :     if (planes > AV_NUM_DATA_POINTERS) {
     250           0 :         frame->extended_data = av_mallocz_array(planes,
     251             :                                           sizeof(*frame->extended_data));
     252           0 :         frame->extended_buf  = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
     253             :                                           sizeof(*frame->extended_buf));
     254           0 :         if (!frame->extended_data || !frame->extended_buf) {
     255           0 :             av_freep(&frame->extended_data);
     256           0 :             av_freep(&frame->extended_buf);
     257           0 :             return AVERROR(ENOMEM);
     258             :         }
     259           0 :         frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
     260             :     } else
     261          69 :         frame->extended_data = frame->data;
     262             : 
     263         161 :     for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
     264          92 :         frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
     265          92 :         if (!frame->buf[i]) {
     266           0 :             av_frame_unref(frame);
     267           0 :             return AVERROR(ENOMEM);
     268             :         }
     269          92 :         frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
     270             :     }
     271          69 :     for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
     272           0 :         frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
     273           0 :         if (!frame->extended_buf[i]) {
     274           0 :             av_frame_unref(frame);
     275           0 :             return AVERROR(ENOMEM);
     276             :         }
     277           0 :         frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
     278             :     }
     279          69 :     return 0;
     280             : 
     281             : }
     282             : 
     283        1444 : int av_frame_get_buffer(AVFrame *frame, int align)
     284             : {
     285        1444 :     if (frame->format < 0)
     286           0 :         return AVERROR(EINVAL);
     287             : 
     288        1444 :     if (frame->width > 0 && frame->height > 0)
     289        1375 :         return get_video_buffer(frame, align);
     290          69 :     else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
     291          69 :         return get_audio_buffer(frame, align);
     292             : 
     293           0 :     return AVERROR(EINVAL);
     294             : }
     295             : 
     296      661810 : static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
     297             : {
     298             :     int i;
     299             : 
     300      661810 :     dst->key_frame              = src->key_frame;
     301      661810 :     dst->pict_type              = src->pict_type;
     302      661810 :     dst->sample_aspect_ratio    = src->sample_aspect_ratio;
     303      661810 :     dst->crop_top               = src->crop_top;
     304      661810 :     dst->crop_bottom            = src->crop_bottom;
     305      661810 :     dst->crop_left              = src->crop_left;
     306      661810 :     dst->crop_right             = src->crop_right;
     307      661810 :     dst->pts                    = src->pts;
     308      661810 :     dst->repeat_pict            = src->repeat_pict;
     309      661810 :     dst->interlaced_frame       = src->interlaced_frame;
     310      661810 :     dst->top_field_first        = src->top_field_first;
     311      661810 :     dst->palette_has_changed    = src->palette_has_changed;
     312      661810 :     dst->sample_rate            = src->sample_rate;
     313      661810 :     dst->opaque                 = src->opaque;
     314             : #if FF_API_PKT_PTS
     315             : FF_DISABLE_DEPRECATION_WARNINGS
     316      661810 :     dst->pkt_pts                = src->pkt_pts;
     317             : FF_ENABLE_DEPRECATION_WARNINGS
     318             : #endif
     319      661810 :     dst->pkt_dts                = src->pkt_dts;
     320      661810 :     dst->pkt_pos                = src->pkt_pos;
     321      661810 :     dst->pkt_size               = src->pkt_size;
     322      661810 :     dst->pkt_duration           = src->pkt_duration;
     323      661810 :     dst->reordered_opaque       = src->reordered_opaque;
     324      661810 :     dst->quality                = src->quality;
     325      661810 :     dst->best_effort_timestamp  = src->best_effort_timestamp;
     326      661810 :     dst->coded_picture_number   = src->coded_picture_number;
     327      661810 :     dst->display_picture_number = src->display_picture_number;
     328      661810 :     dst->flags                  = src->flags;
     329      661810 :     dst->decode_error_flags     = src->decode_error_flags;
     330      661810 :     dst->color_primaries        = src->color_primaries;
     331      661810 :     dst->color_trc              = src->color_trc;
     332      661810 :     dst->colorspace             = src->colorspace;
     333      661810 :     dst->color_range            = src->color_range;
     334      661810 :     dst->chroma_location        = src->chroma_location;
     335             : 
     336      661810 :     av_dict_copy(&dst->metadata, src->metadata, 0);
     337             : 
     338             : #if FF_API_ERROR_FRAME
     339             : FF_DISABLE_DEPRECATION_WARNINGS
     340      661810 :     memcpy(dst->error, src->error, sizeof(dst->error));
     341             : FF_ENABLE_DEPRECATION_WARNINGS
     342             : #endif
     343             : 
     344      735442 :     for (i = 0; i < src->nb_side_data; i++) {
     345       73632 :         const AVFrameSideData *sd_src = src->side_data[i];
     346             :         AVFrameSideData *sd_dst;
     347       73632 :         if (   sd_src->type == AV_FRAME_DATA_PANSCAN
     348       12835 :             && (src->width != dst->width || src->height != dst->height))
     349          75 :             continue;
     350       73557 :         if (force_copy) {
     351        7340 :             sd_dst = av_frame_new_side_data(dst, sd_src->type,
     352             :                                             sd_src->size);
     353        7340 :             if (!sd_dst) {
     354           0 :                 wipe_side_data(dst);
     355           0 :                 return AVERROR(ENOMEM);
     356             :             }
     357        7340 :             memcpy(sd_dst->data, sd_src->data, sd_src->size);
     358             :         } else {
     359       66217 :             sd_dst = frame_new_side_data(dst, sd_src->type, av_buffer_ref(sd_src->buf));
     360       66217 :             if (!sd_dst) {
     361           0 :                 wipe_side_data(dst);
     362           0 :                 return AVERROR(ENOMEM);
     363             :             }
     364             :         }
     365       73557 :         av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
     366             :     }
     367             : 
     368             : #if FF_API_FRAME_QP
     369             : FF_DISABLE_DEPRECATION_WARNINGS
     370      661810 :     dst->qscale_table = NULL;
     371      661810 :     dst->qstride      = 0;
     372      661810 :     dst->qscale_type  = 0;
     373      661810 :     av_buffer_unref(&dst->qp_table_buf);
     374      661810 :     if (src->qp_table_buf) {
     375       14853 :         dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
     376       14853 :         if (dst->qp_table_buf) {
     377       14853 :             dst->qscale_table = dst->qp_table_buf->data;
     378       14853 :             dst->qstride      = src->qstride;
     379       14853 :             dst->qscale_type  = src->qscale_type;
     380             :         }
     381             :     }
     382             : FF_ENABLE_DEPRECATION_WARNINGS
     383             : #endif
     384             : 
     385      661810 :     av_buffer_unref(&dst->opaque_ref);
     386      661810 :     av_buffer_unref(&dst->private_ref);
     387      661810 :     if (src->opaque_ref) {
     388           0 :         dst->opaque_ref = av_buffer_ref(src->opaque_ref);
     389           0 :         if (!dst->opaque_ref)
     390           0 :             return AVERROR(ENOMEM);
     391             :     }
     392      661810 :     if (src->private_ref) {
     393      155437 :         dst->private_ref = av_buffer_ref(src->private_ref);
     394      155437 :         if (!dst->private_ref)
     395           0 :             return AVERROR(ENOMEM);
     396             :     }
     397      661810 :     return 0;
     398             : }
     399             : 
     400      315169 : int av_frame_ref(AVFrame *dst, const AVFrame *src)
     401             : {
     402      315169 :     int i, ret = 0;
     403             : 
     404             :     av_assert1(dst->width == 0 && dst->height == 0);
     405             :     av_assert1(dst->channels == 0);
     406             : 
     407      315169 :     dst->format         = src->format;
     408      315169 :     dst->width          = src->width;
     409      315169 :     dst->height         = src->height;
     410      315169 :     dst->channels       = src->channels;
     411      315169 :     dst->channel_layout = src->channel_layout;
     412      315169 :     dst->nb_samples     = src->nb_samples;
     413             : 
     414      315169 :     ret = frame_copy_props(dst, src, 0);
     415      315169 :     if (ret < 0)
     416           0 :         return ret;
     417             : 
     418             :     /* duplicate the frame data if it's not refcounted */
     419      315169 :     if (!src->buf[0]) {
     420          13 :         ret = av_frame_get_buffer(dst, 32);
     421          13 :         if (ret < 0)
     422           0 :             return ret;
     423             : 
     424          13 :         ret = av_frame_copy(dst, src);
     425          13 :         if (ret < 0)
     426           0 :             av_frame_unref(dst);
     427             : 
     428          13 :         return ret;
     429             :     }
     430             : 
     431             :     /* ref the buffers */
     432     2836404 :     for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
     433     2521248 :         if (!src->buf[i])
     434     1660174 :             continue;
     435      861074 :         dst->buf[i] = av_buffer_ref(src->buf[i]);
     436      861074 :         if (!dst->buf[i]) {
     437           0 :             ret = AVERROR(ENOMEM);
     438           0 :             goto fail;
     439             :         }
     440             :     }
     441             : 
     442      315156 :     if (src->extended_buf) {
     443           0 :         dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
     444           0 :                                        src->nb_extended_buf);
     445           0 :         if (!dst->extended_buf) {
     446           0 :             ret = AVERROR(ENOMEM);
     447           0 :             goto fail;
     448             :         }
     449           0 :         dst->nb_extended_buf = src->nb_extended_buf;
     450             : 
     451           0 :         for (i = 0; i < src->nb_extended_buf; i++) {
     452           0 :             dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
     453           0 :             if (!dst->extended_buf[i]) {
     454           0 :                 ret = AVERROR(ENOMEM);
     455           0 :                 goto fail;
     456             :             }
     457             :         }
     458             :     }
     459             : 
     460      315156 :     if (src->hw_frames_ctx) {
     461           0 :         dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
     462           0 :         if (!dst->hw_frames_ctx) {
     463           0 :             ret = AVERROR(ENOMEM);
     464           0 :             goto fail;
     465             :         }
     466             :     }
     467             : 
     468             :     /* duplicate extended data */
     469      315156 :     if (src->extended_data != src->data) {
     470           0 :         int ch = src->channels;
     471             : 
     472           0 :         if (!ch) {
     473           0 :             ret = AVERROR(EINVAL);
     474           0 :             goto fail;
     475             :         }
     476             :         CHECK_CHANNELS_CONSISTENCY(src);
     477             : 
     478           0 :         dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
     479           0 :         if (!dst->extended_data) {
     480           0 :             ret = AVERROR(ENOMEM);
     481           0 :             goto fail;
     482             :         }
     483           0 :         memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
     484             :     } else
     485      315156 :         dst->extended_data = dst->data;
     486             : 
     487      315156 :     memcpy(dst->data,     src->data,     sizeof(src->data));
     488      315156 :     memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
     489             : 
     490      315156 :     return 0;
     491             : 
     492           0 : fail:
     493           0 :     av_frame_unref(dst);
     494           0 :     return ret;
     495             : }
     496             : 
     497       12479 : AVFrame *av_frame_clone(const AVFrame *src)
     498             : {
     499       12479 :     AVFrame *ret = av_frame_alloc();
     500             : 
     501       12479 :     if (!ret)
     502           0 :         return NULL;
     503             : 
     504       12479 :     if (av_frame_ref(ret, src) < 0)
     505           0 :         av_frame_free(&ret);
     506             : 
     507       12479 :     return ret;
     508             : }
     509             : 
     510     3354770 : void av_frame_unref(AVFrame *frame)
     511             : {
     512             :     int i;
     513             : 
     514     3354770 :     if (!frame)
     515           0 :         return;
     516             : 
     517     3354770 :     wipe_side_data(frame);
     518             : 
     519    30192930 :     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
     520    26838160 :         av_buffer_unref(&frame->buf[i]);
     521     3354770 :     for (i = 0; i < frame->nb_extended_buf; i++)
     522           0 :         av_buffer_unref(&frame->extended_buf[i]);
     523     3354770 :     av_freep(&frame->extended_buf);
     524     3354770 :     av_dict_free(&frame->metadata);
     525             : #if FF_API_FRAME_QP
     526     3354770 :     av_buffer_unref(&frame->qp_table_buf);
     527             : #endif
     528             : 
     529     3354770 :     av_buffer_unref(&frame->hw_frames_ctx);
     530             : 
     531     3354770 :     av_buffer_unref(&frame->opaque_ref);
     532     3354770 :     av_buffer_unref(&frame->private_ref);
     533             : 
     534     3354770 :     get_frame_defaults(frame);
     535             : }
     536             : 
     537     1162373 : void av_frame_move_ref(AVFrame *dst, AVFrame *src)
     538             : {
     539             :     av_assert1(dst->width == 0 && dst->height == 0);
     540             :     av_assert1(dst->channels == 0);
     541             : 
     542     1162373 :     *dst = *src;
     543     1162373 :     if (src->extended_data == src->data)
     544     1162373 :         dst->extended_data = dst->data;
     545     1162373 :     memset(src, 0, sizeof(*src));
     546     1162373 :     get_frame_defaults(src);
     547     1162373 : }
     548             : 
     549       15779 : int av_frame_is_writable(AVFrame *frame)
     550             : {
     551       15779 :     int i, ret = 1;
     552             : 
     553             :     /* assume non-refcounted frames are not writable */
     554       15779 :     if (!frame->buf[0])
     555           0 :         return 0;
     556             : 
     557      142011 :     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
     558      126232 :         if (frame->buf[i])
     559       32518 :             ret &= !!av_buffer_is_writable(frame->buf[i]);
     560       15779 :     for (i = 0; i < frame->nb_extended_buf; i++)
     561           0 :         ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
     562             : 
     563       15779 :     return ret;
     564             : }
     565             : 
     566        1626 : int av_frame_make_writable(AVFrame *frame)
     567             : {
     568             :     AVFrame tmp;
     569             :     int ret;
     570             : 
     571        1626 :     if (!frame->buf[0])
     572           0 :         return AVERROR(EINVAL);
     573             : 
     574        1626 :     if (av_frame_is_writable(frame))
     575         532 :         return 0;
     576             : 
     577        1094 :     memset(&tmp, 0, sizeof(tmp));
     578        1094 :     tmp.format         = frame->format;
     579        1094 :     tmp.width          = frame->width;
     580        1094 :     tmp.height         = frame->height;
     581        1094 :     tmp.channels       = frame->channels;
     582        1094 :     tmp.channel_layout = frame->channel_layout;
     583        1094 :     tmp.nb_samples     = frame->nb_samples;
     584        1094 :     ret = av_frame_get_buffer(&tmp, 32);
     585        1094 :     if (ret < 0)
     586           0 :         return ret;
     587             : 
     588        1094 :     ret = av_frame_copy(&tmp, frame);
     589        1094 :     if (ret < 0) {
     590           0 :         av_frame_unref(&tmp);
     591           0 :         return ret;
     592             :     }
     593             : 
     594        1094 :     ret = av_frame_copy_props(&tmp, frame);
     595        1094 :     if (ret < 0) {
     596           0 :         av_frame_unref(&tmp);
     597           0 :         return ret;
     598             :     }
     599             : 
     600        1094 :     av_frame_unref(frame);
     601             : 
     602        1094 :     *frame = tmp;
     603        1094 :     if (tmp.data == tmp.extended_data)
     604        1094 :         frame->extended_data = frame->data;
     605             : 
     606        1094 :     return 0;
     607             : }
     608             : 
     609      346641 : int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
     610             : {
     611      346641 :     return frame_copy_props(dst, src, 1);
     612             : }
     613             : 
     614        6196 : AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
     615             : {
     616             :     uint8_t *data;
     617             :     int planes, i;
     618             : 
     619        6196 :     if (frame->nb_samples) {
     620        1295 :         int channels = frame->channels;
     621        1295 :         if (!channels)
     622           0 :             return NULL;
     623             :         CHECK_CHANNELS_CONSISTENCY(frame);
     624        1295 :         planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
     625             :     } else
     626        4901 :         planes = 4;
     627             : 
     628        6196 :     if (plane < 0 || plane >= planes || !frame->extended_data[plane])
     629           0 :         return NULL;
     630        6196 :     data = frame->extended_data[plane];
     631             : 
     632       14189 :     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
     633       14189 :         AVBufferRef *buf = frame->buf[i];
     634       14189 :         if (data >= buf->data && data < buf->data + buf->size)
     635        6196 :             return buf;
     636             :     }
     637           0 :     for (i = 0; i < frame->nb_extended_buf; i++) {
     638           0 :         AVBufferRef *buf = frame->extended_buf[i];
     639           0 :         if (data >= buf->data && data < buf->data + buf->size)
     640           0 :             return buf;
     641             :     }
     642           0 :     return NULL;
     643             : }
     644             : 
     645      101224 : static AVFrameSideData *frame_new_side_data(AVFrame *frame,
     646             :                                             enum AVFrameSideDataType type,
     647             :                                             AVBufferRef *buf)
     648             : {
     649             :     AVFrameSideData *ret, **tmp;
     650             : 
     651      101224 :     if (!buf)
     652           0 :         return NULL;
     653             : 
     654      101224 :     if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
     655           0 :         goto fail;
     656             : 
     657      101224 :     tmp = av_realloc(frame->side_data,
     658      101224 :                      (frame->nb_side_data + 1) * sizeof(*frame->side_data));
     659      101224 :     if (!tmp)
     660           0 :         goto fail;
     661      101224 :     frame->side_data = tmp;
     662             : 
     663      101224 :     ret = av_mallocz(sizeof(*ret));
     664      101224 :     if (!ret)
     665           0 :         goto fail;
     666             : 
     667      101224 :     ret->buf = buf;
     668      101224 :     ret->data = ret->buf->data;
     669      101224 :     ret->size = buf->size;
     670      101224 :     ret->type = type;
     671             : 
     672      101224 :     frame->side_data[frame->nb_side_data++] = ret;
     673             : 
     674      101224 :     return ret;
     675           0 : fail:
     676           0 :     av_buffer_unref(&buf);
     677           0 :     return NULL;
     678             : }
     679             : 
     680       35007 : AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
     681             :                                         enum AVFrameSideDataType type,
     682             :                                         int size)
     683             : {
     684             : 
     685       35007 :     return frame_new_side_data(frame, type, av_buffer_alloc(size));
     686             : }
     687             : 
     688      326852 : AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
     689             :                                         enum AVFrameSideDataType type)
     690             : {
     691             :     int i;
     692             : 
     693      351371 :     for (i = 0; i < frame->nb_side_data; i++) {
     694       24546 :         if (frame->side_data[i]->type == type)
     695          27 :             return frame->side_data[i];
     696             :     }
     697      326825 :     return NULL;
     698             : }
     699             : 
     700        3146 : static int frame_copy_video(AVFrame *dst, const AVFrame *src)
     701             : {
     702             :     const uint8_t *src_data[4];
     703             :     int i, planes;
     704             : 
     705        6292 :     if (dst->width  < src->width ||
     706        3146 :         dst->height < src->height)
     707           0 :         return AVERROR(EINVAL);
     708             : 
     709        3146 :     planes = av_pix_fmt_count_planes(dst->format);
     710        9734 :     for (i = 0; i < planes; i++)
     711        6588 :         if (!dst->data[i] || !src->data[i])
     712           0 :             return AVERROR(EINVAL);
     713             : 
     714        3146 :     memcpy(src_data, src->data, sizeof(src_data));
     715        6292 :     av_image_copy(dst->data, dst->linesize,
     716        3146 :                   src_data, src->linesize,
     717        3146 :                   dst->format, src->width, src->height);
     718             : 
     719        3146 :     return 0;
     720             : }
     721             : 
     722           0 : static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
     723             : {
     724           0 :     int planar   = av_sample_fmt_is_planar(dst->format);
     725           0 :     int channels = dst->channels;
     726           0 :     int planes   = planar ? channels : 1;
     727             :     int i;
     728             : 
     729           0 :     if (dst->nb_samples     != src->nb_samples ||
     730           0 :         dst->channels       != src->channels ||
     731           0 :         dst->channel_layout != src->channel_layout)
     732           0 :         return AVERROR(EINVAL);
     733             : 
     734             :     CHECK_CHANNELS_CONSISTENCY(src);
     735             : 
     736           0 :     for (i = 0; i < planes; i++)
     737           0 :         if (!dst->extended_data[i] || !src->extended_data[i])
     738           0 :             return AVERROR(EINVAL);
     739             : 
     740           0 :     av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
     741           0 :                     dst->nb_samples, channels, dst->format);
     742             : 
     743           0 :     return 0;
     744             : }
     745             : 
     746        3146 : int av_frame_copy(AVFrame *dst, const AVFrame *src)
     747             : {
     748        3146 :     if (dst->format != src->format || dst->format < 0)
     749           0 :         return AVERROR(EINVAL);
     750             : 
     751        3146 :     if (dst->width > 0 && dst->height > 0)
     752        3146 :         return frame_copy_video(dst, src);
     753           0 :     else if (dst->nb_samples > 0 && dst->channels > 0)
     754           0 :         return frame_copy_audio(dst, src);
     755             : 
     756           0 :     return AVERROR(EINVAL);
     757             : }
     758             : 
     759           0 : void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
     760             : {
     761             :     int i;
     762             : 
     763           0 :     for (i = 0; i < frame->nb_side_data; i++) {
     764           0 :         AVFrameSideData *sd = frame->side_data[i];
     765           0 :         if (sd->type == type) {
     766           0 :             free_side_data(&frame->side_data[i]);
     767           0 :             frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
     768           0 :             frame->nb_side_data--;
     769             :         }
     770             :     }
     771           0 : }
     772             : 
     773           0 : const char *av_frame_side_data_name(enum AVFrameSideDataType type)
     774             : {
     775           0 :     switch(type) {
     776           0 :     case AV_FRAME_DATA_PANSCAN:         return "AVPanScan";
     777           0 :     case AV_FRAME_DATA_A53_CC:          return "ATSC A53 Part 4 Closed Captions";
     778           0 :     case AV_FRAME_DATA_STEREO3D:        return "Stereoscopic 3d metadata";
     779           0 :     case AV_FRAME_DATA_MATRIXENCODING:  return "AVMatrixEncoding";
     780           0 :     case AV_FRAME_DATA_DOWNMIX_INFO:    return "Metadata relevant to a downmix procedure";
     781           0 :     case AV_FRAME_DATA_REPLAYGAIN:      return "AVReplayGain";
     782           0 :     case AV_FRAME_DATA_DISPLAYMATRIX:   return "3x3 displaymatrix";
     783           0 :     case AV_FRAME_DATA_AFD:             return "Active format description";
     784           0 :     case AV_FRAME_DATA_MOTION_VECTORS:  return "Motion vectors";
     785           0 :     case AV_FRAME_DATA_SKIP_SAMPLES:    return "Skip samples";
     786           0 :     case AV_FRAME_DATA_AUDIO_SERVICE_TYPE:          return "Audio service type";
     787           0 :     case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA:  return "Mastering display metadata";
     788           0 :     case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL:         return "Content light level metadata";
     789           0 :     case AV_FRAME_DATA_GOP_TIMECODE:                return "GOP timecode";
     790           0 :     case AV_FRAME_DATA_ICC_PROFILE:                 return "ICC profile";
     791             :     }
     792           0 :     return NULL;
     793             : }
     794             : 
     795      103103 : static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
     796             :                                  const AVPixFmtDescriptor *desc)
     797             : {
     798             :     int i, j;
     799             : 
     800      384079 :     for (i = 0; frame->data[i]; i++) {
     801      285594 :         const AVComponentDescriptor *comp = NULL;
     802      285594 :         int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
     803      285594 :         int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
     804             : 
     805      285594 :         if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL) && i == 1) {
     806        4618 :             offsets[i] = 0;
     807        4618 :             break;
     808             :         }
     809             : 
     810             :         /* find any component descriptor for this plane */
     811      548720 :         for (j = 0; j < desc->nb_components; j++) {
     812      548720 :             if (desc->comp[j].plane == i) {
     813      280976 :                 comp = &desc->comp[j];
     814      280976 :                 break;
     815             :             }
     816             :         }
     817      280976 :         if (!comp)
     818           0 :             return AVERROR_BUG;
     819             : 
     820      561952 :         offsets[i] = (frame->crop_top  >> shift_y) * frame->linesize[i] +
     821      280976 :                      (frame->crop_left >> shift_x) * comp->step;
     822             :     }
     823             : 
     824      103103 :     return 0;
     825             : }
     826             : 
     827      103382 : int av_frame_apply_cropping(AVFrame *frame, int flags)
     828             : {
     829             :     const AVPixFmtDescriptor *desc;
     830             :     size_t offsets[4];
     831             :     int i;
     832             : 
     833      103382 :     if (!(frame->width > 0 && frame->height > 0))
     834           0 :         return AVERROR(EINVAL);
     835             : 
     836      206764 :     if (frame->crop_left >= INT_MAX - frame->crop_right        ||
     837      206764 :         frame->crop_top  >= INT_MAX - frame->crop_bottom       ||
     838      206764 :         (frame->crop_left + frame->crop_right) >= frame->width ||
     839      103382 :         (frame->crop_top + frame->crop_bottom) >= frame->height)
     840           0 :         return AVERROR(ERANGE);
     841             : 
     842      103382 :     desc = av_pix_fmt_desc_get(frame->format);
     843      103382 :     if (!desc)
     844           0 :         return AVERROR_BUG;
     845             : 
     846             :     /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
     847             :      * formats cannot be easily handled here either (and corresponding decoders
     848             :      * should not export any cropping anyway), so do the same for those as well.
     849             :      * */
     850      103382 :     if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
     851         286 :         frame->width      -= frame->crop_right;
     852         286 :         frame->height     -= frame->crop_bottom;
     853         286 :         frame->crop_right  = 0;
     854         286 :         frame->crop_bottom = 0;
     855         286 :         return 0;
     856             :     }
     857             : 
     858             :     /* calculate the offsets for each plane */
     859      103096 :     calc_cropping_offsets(offsets, frame, desc);
     860             : 
     861             :     /* adjust the offsets to avoid breaking alignment */
     862      103096 :     if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
     863       94085 :         int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
     864       94085 :         int min_log2_align = INT_MAX;
     865             : 
     866      352625 :         for (i = 0; frame->data[i]; i++) {
     867      258540 :             int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
     868      258540 :             min_log2_align = FFMIN(log2_align, min_log2_align);
     869             :         }
     870             : 
     871             :         /* we assume, and it should always be true, that the data alignment is
     872             :          * related to the cropping alignment by a constant power-of-2 factor */
     873       94085 :         if (log2_crop_align < min_log2_align)
     874           0 :             return AVERROR_BUG;
     875             : 
     876       94085 :         if (min_log2_align < 5) {
     877           7 :             frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
     878           7 :             calc_cropping_offsets(offsets, frame, desc);
     879             :         }
     880             :     }
     881             : 
     882      388669 :     for (i = 0; frame->data[i]; i++)
     883      285573 :         frame->data[i] += offsets[i];
     884             : 
     885      103096 :     frame->width      -= (frame->crop_left + frame->crop_right);
     886      103096 :     frame->height     -= (frame->crop_top  + frame->crop_bottom);
     887      103096 :     frame->crop_left   = 0;
     888      103096 :     frame->crop_right  = 0;
     889      103096 :     frame->crop_top    = 0;
     890      103096 :     frame->crop_bottom = 0;
     891             : 
     892      103096 :     return 0;
     893             : }

Generated by: LCOV version 1.13