LCOV - code coverage report
Current view: top level - libavutil - frame.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 351 511 68.7 %
Date: 2017-10-20 23:03:07 Functions: 27 53 50.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           0 : MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
      35           0 : MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
      36           0 : MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
      37           0 : MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
      38           0 : MAKE_ACCESSORS(AVFrame, frame, int,     channels)
      39           0 : MAKE_ACCESSORS(AVFrame, frame, int,     sample_rate)
      40           0 : MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
      41           0 : MAKE_ACCESSORS(AVFrame, frame, int,     decode_error_flags)
      42           0 : MAKE_ACCESSORS(AVFrame, frame, int,     pkt_size)
      43           0 : MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
      44           0 : MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
      45             : 
      46             : #define CHECK_CHANNELS_CONSISTENCY(frame) \
      47             :     av_assert2(!(frame)->channel_layout || \
      48             :                (frame)->channels == \
      49             :                av_get_channel_layout_nb_channels((frame)->channel_layout))
      50             : 
      51           0 : AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
      52             : 
      53             : #if FF_API_FRAME_QP
      54       11712 : int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
      55             : {
      56       11712 :     av_buffer_unref(&f->qp_table_buf);
      57             : 
      58       11712 :     f->qp_table_buf = buf;
      59             : 
      60             : FF_DISABLE_DEPRECATION_WARNINGS
      61       11712 :     f->qscale_table = buf->data;
      62       11712 :     f->qstride      = stride;
      63       11712 :     f->qscale_type  = qp_type;
      64             : FF_ENABLE_DEPRECATION_WARNINGS
      65             : 
      66       11712 :     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          62 : 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          62 :     if ((unsigned)val >= FF_ARRAY_ELEMS(name))
      95           0 :         return NULL;
      96          62 :     return name[val];
      97             : }
      98             : 
      99     5523323 : static void get_frame_defaults(AVFrame *frame)
     100             : {
     101     5523323 :     if (frame->extended_data != frame->data)
     102     2184133 :         av_freep(&frame->extended_data);
     103             : 
     104     5523323 :     memset(frame, 0, sizeof(*frame));
     105             : 
     106     5523323 :     frame->pts                   =
     107     5523323 :     frame->pkt_dts               = AV_NOPTS_VALUE;
     108             : #if FF_API_PKT_PTS
     109             : FF_DISABLE_DEPRECATION_WARNINGS
     110     5523323 :     frame->pkt_pts               = AV_NOPTS_VALUE;
     111             : FF_ENABLE_DEPRECATION_WARNINGS
     112             : #endif
     113     5523323 :     frame->best_effort_timestamp = AV_NOPTS_VALUE;
     114     5523323 :     frame->pkt_duration        = 0;
     115     5523323 :     frame->pkt_pos             = -1;
     116     5523323 :     frame->pkt_size            = -1;
     117     5523323 :     frame->key_frame           = 1;
     118     5523323 :     frame->sample_aspect_ratio = (AVRational){ 0, 1 };
     119     5523323 :     frame->format              = -1; /* unknown */
     120     5523323 :     frame->extended_data       = frame->data;
     121     5523323 :     frame->color_primaries     = AVCOL_PRI_UNSPECIFIED;
     122     5523323 :     frame->color_trc           = AVCOL_TRC_UNSPECIFIED;
     123     5523323 :     frame->colorspace          = AVCOL_SPC_UNSPECIFIED;
     124     5523323 :     frame->color_range         = AVCOL_RANGE_UNSPECIFIED;
     125     5523323 :     frame->chroma_location     = AVCHROMA_LOC_UNSPECIFIED;
     126     5523323 :     frame->flags               = 0;
     127     5523323 : }
     128             : 
     129       98356 : static void free_side_data(AVFrameSideData **ptr_sd)
     130             : {
     131       98356 :     AVFrameSideData *sd = *ptr_sd;
     132             : 
     133       98356 :     av_buffer_unref(&sd->buf);
     134       98356 :     av_dict_free(&sd->metadata);
     135       98356 :     av_freep(ptr_sd);
     136       98356 : }
     137             : 
     138     3339192 : static void wipe_side_data(AVFrame *frame)
     139             : {
     140             :     int i;
     141             : 
     142     3437548 :     for (i = 0; i < frame->nb_side_data; i++) {
     143       98356 :         free_side_data(&frame->side_data[i]);
     144             :     }
     145     3339192 :     frame->nb_side_data = 0;
     146             : 
     147     3339192 :     av_freep(&frame->side_data);
     148     3339192 : }
     149             : 
     150     1026006 : AVFrame *av_frame_alloc(void)
     151             : {
     152     1026006 :     AVFrame *frame = av_mallocz(sizeof(*frame));
     153             : 
     154     1026006 :     if (!frame)
     155           0 :         return NULL;
     156             : 
     157     1026006 :     frame->extended_data = NULL;
     158     1026006 :     get_frame_defaults(frame);
     159             : 
     160     1026006 :     return frame;
     161             : }
     162             : 
     163     1380569 : void av_frame_free(AVFrame **frame)
     164             : {
     165     1380569 :     if (!frame || !*frame)
     166      354518 :         return;
     167             : 
     168     1026051 :     av_frame_unref(*frame);
     169     1026051 :     av_freep(frame);
     170             : }
     171             : 
     172        1390 : static int get_video_buffer(AVFrame *frame, int align)
     173             : {
     174        1390 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
     175             :     int ret, i;
     176             : 
     177        1390 :     if (!desc)
     178           0 :         return AVERROR(EINVAL);
     179             : 
     180        1390 :     if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
     181           0 :         return ret;
     182             : 
     183        1390 :     if (!frame->linesize[0]) {
     184        1390 :         if (align <= 0)
     185           0 :             align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
     186             : 
     187        3705 :         for(i=1; i<=align; i+=i) {
     188        3705 :             ret = av_image_fill_linesizes(frame->linesize, frame->format,
     189        3705 :                                           FFALIGN(frame->width, i));
     190        3705 :             if (ret < 0)
     191           0 :                 return ret;
     192        3705 :             if (!(frame->linesize[0] & (align-1)))
     193        1390 :                 break;
     194             :         }
     195             : 
     196        4660 :         for (i = 0; i < 4 && frame->linesize[i]; i++)
     197        3270 :             frame->linesize[i] = FFALIGN(frame->linesize[i], align);
     198             :     }
     199             : 
     200        4660 :     for (i = 0; i < 4 && frame->linesize[i]; i++) {
     201        3270 :         int h = FFALIGN(frame->height, 32);
     202        3270 :         if (i == 1 || i == 2)
     203        1776 :             h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
     204             : 
     205        3270 :         frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
     206        3270 :         if (!frame->buf[i])
     207           0 :             goto fail;
     208             : 
     209        3270 :         frame->data[i] = frame->buf[i]->data;
     210             :     }
     211        1390 :     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        1390 :     frame->extended_data = frame->data;
     220             : 
     221        1390 :     return 0;
     222           0 : fail:
     223           0 :     av_frame_unref(frame);
     224           0 :     return AVERROR(ENOMEM);
     225             : }
     226             : 
     227          68 : static int get_audio_buffer(AVFrame *frame, int align)
     228             : {
     229             :     int channels;
     230          68 :     int planar   = av_sample_fmt_is_planar(frame->format);
     231             :     int planes;
     232             :     int ret, i;
     233             : 
     234          68 :     if (!frame->channels)
     235          29 :         frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
     236             : 
     237          68 :     channels = frame->channels;
     238          68 :     planes = planar ? channels : 1;
     239             : 
     240             :     CHECK_CHANNELS_CONSISTENCY(frame);
     241          68 :     if (!frame->linesize[0]) {
     242          68 :         ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
     243          68 :                                          frame->nb_samples, frame->format,
     244             :                                          align);
     245          68 :         if (ret < 0)
     246           0 :             return ret;
     247             :     }
     248             : 
     249          68 :     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          68 :         frame->extended_data = frame->data;
     262             : 
     263         159 :     for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
     264          91 :         frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
     265          91 :         if (!frame->buf[i]) {
     266           0 :             av_frame_unref(frame);
     267           0 :             return AVERROR(ENOMEM);
     268             :         }
     269          91 :         frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
     270             :     }
     271          68 :     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          68 :     return 0;
     280             : 
     281             : }
     282             : 
     283        1458 : int av_frame_get_buffer(AVFrame *frame, int align)
     284             : {
     285        1458 :     if (frame->format < 0)
     286           0 :         return AVERROR(EINVAL);
     287             : 
     288        1458 :     if (frame->width > 0 && frame->height > 0)
     289        1390 :         return get_video_buffer(frame, align);
     290          68 :     else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
     291          68 :         return get_audio_buffer(frame, align);
     292             : 
     293           0 :     return AVERROR(EINVAL);
     294             : }
     295             : 
     296      657489 : static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
     297             : {
     298             :     int i;
     299             : 
     300      657489 :     dst->key_frame              = src->key_frame;
     301      657489 :     dst->pict_type              = src->pict_type;
     302      657489 :     dst->sample_aspect_ratio    = src->sample_aspect_ratio;
     303      657489 :     dst->crop_top               = src->crop_top;
     304      657489 :     dst->crop_bottom            = src->crop_bottom;
     305      657489 :     dst->crop_left              = src->crop_left;
     306      657489 :     dst->crop_right             = src->crop_right;
     307      657489 :     dst->pts                    = src->pts;
     308      657489 :     dst->repeat_pict            = src->repeat_pict;
     309      657489 :     dst->interlaced_frame       = src->interlaced_frame;
     310      657489 :     dst->top_field_first        = src->top_field_first;
     311      657489 :     dst->palette_has_changed    = src->palette_has_changed;
     312      657489 :     dst->sample_rate            = src->sample_rate;
     313      657489 :     dst->opaque                 = src->opaque;
     314             : #if FF_API_PKT_PTS
     315             : FF_DISABLE_DEPRECATION_WARNINGS
     316      657489 :     dst->pkt_pts                = src->pkt_pts;
     317             : FF_ENABLE_DEPRECATION_WARNINGS
     318             : #endif
     319      657489 :     dst->pkt_dts                = src->pkt_dts;
     320      657489 :     dst->pkt_pos                = src->pkt_pos;
     321      657489 :     dst->pkt_size               = src->pkt_size;
     322      657489 :     dst->pkt_duration           = src->pkt_duration;
     323      657489 :     dst->reordered_opaque       = src->reordered_opaque;
     324      657489 :     dst->quality                = src->quality;
     325      657489 :     dst->best_effort_timestamp  = src->best_effort_timestamp;
     326      657489 :     dst->coded_picture_number   = src->coded_picture_number;
     327      657489 :     dst->display_picture_number = src->display_picture_number;
     328      657489 :     dst->flags                  = src->flags;
     329      657489 :     dst->decode_error_flags     = src->decode_error_flags;
     330      657489 :     dst->color_primaries        = src->color_primaries;
     331      657489 :     dst->color_trc              = src->color_trc;
     332      657489 :     dst->colorspace             = src->colorspace;
     333      657489 :     dst->color_range            = src->color_range;
     334      657489 :     dst->chroma_location        = src->chroma_location;
     335             : 
     336      657489 :     av_dict_copy(&dst->metadata, src->metadata, 0);
     337             : 
     338             : #if FF_API_ERROR_FRAME
     339             : FF_DISABLE_DEPRECATION_WARNINGS
     340      657489 :     memcpy(dst->error, src->error, sizeof(dst->error));
     341             : FF_ENABLE_DEPRECATION_WARNINGS
     342             : #endif
     343             : 
     344      728873 :     for (i = 0; i < src->nb_side_data; i++) {
     345       71384 :         const AVFrameSideData *sd_src = src->side_data[i];
     346             :         AVFrameSideData *sd_dst;
     347       71384 :         if (   sd_src->type == AV_FRAME_DATA_PANSCAN
     348       12073 :             && (src->width != dst->width || src->height != dst->height))
     349          75 :             continue;
     350       71309 :         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       63969 :             sd_dst = frame_new_side_data(dst, sd_src->type, av_buffer_ref(sd_src->buf));
     360       63969 :             if (!sd_dst) {
     361           0 :                 wipe_side_data(dst);
     362           0 :                 return AVERROR(ENOMEM);
     363             :             }
     364             :         }
     365       71309 :         av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
     366             :     }
     367             : 
     368             : #if FF_API_FRAME_QP
     369             : FF_DISABLE_DEPRECATION_WARNINGS
     370      657489 :     dst->qscale_table = NULL;
     371      657489 :     dst->qstride      = 0;
     372      657489 :     dst->qscale_type  = 0;
     373      657489 :     av_buffer_unref(&dst->qp_table_buf);
     374      657489 :     if (src->qp_table_buf) {
     375       14599 :         dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
     376       14599 :         if (dst->qp_table_buf) {
     377       14599 :             dst->qscale_table = dst->qp_table_buf->data;
     378       14599 :             dst->qstride      = src->qstride;
     379       14599 :             dst->qscale_type  = src->qscale_type;
     380             :         }
     381             :     }
     382             : FF_ENABLE_DEPRECATION_WARNINGS
     383             : #endif
     384             : 
     385      657489 :     av_buffer_unref(&dst->opaque_ref);
     386      657489 :     if (src->opaque_ref) {
     387           0 :         dst->opaque_ref = av_buffer_ref(src->opaque_ref);
     388           0 :         if (!dst->opaque_ref)
     389           0 :             return AVERROR(ENOMEM);
     390             :     }
     391             : 
     392      657489 :     return 0;
     393             : }
     394             : 
     395      311904 : int av_frame_ref(AVFrame *dst, const AVFrame *src)
     396             : {
     397      311904 :     int i, ret = 0;
     398             : 
     399             :     av_assert1(dst->width == 0 && dst->height == 0);
     400             :     av_assert1(dst->channels == 0);
     401             : 
     402      311904 :     dst->format         = src->format;
     403      311904 :     dst->width          = src->width;
     404      311904 :     dst->height         = src->height;
     405      311904 :     dst->channels       = src->channels;
     406      311904 :     dst->channel_layout = src->channel_layout;
     407      311904 :     dst->nb_samples     = src->nb_samples;
     408             : 
     409      311904 :     ret = frame_copy_props(dst, src, 0);
     410      311904 :     if (ret < 0)
     411           0 :         return ret;
     412             : 
     413             :     /* duplicate the frame data if it's not refcounted */
     414      311904 :     if (!src->buf[0]) {
     415          13 :         ret = av_frame_get_buffer(dst, 32);
     416          13 :         if (ret < 0)
     417           0 :             return ret;
     418             : 
     419          13 :         ret = av_frame_copy(dst, src);
     420          13 :         if (ret < 0)
     421           0 :             av_frame_unref(dst);
     422             : 
     423          13 :         return ret;
     424             :     }
     425             : 
     426             :     /* ref the buffers */
     427     2807019 :     for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
     428     2495128 :         if (!src->buf[i])
     429     1643196 :             continue;
     430      851932 :         dst->buf[i] = av_buffer_ref(src->buf[i]);
     431      851932 :         if (!dst->buf[i]) {
     432           0 :             ret = AVERROR(ENOMEM);
     433           0 :             goto fail;
     434             :         }
     435             :     }
     436             : 
     437      311891 :     if (src->extended_buf) {
     438           0 :         dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
     439           0 :                                        src->nb_extended_buf);
     440           0 :         if (!dst->extended_buf) {
     441           0 :             ret = AVERROR(ENOMEM);
     442           0 :             goto fail;
     443             :         }
     444           0 :         dst->nb_extended_buf = src->nb_extended_buf;
     445             : 
     446           0 :         for (i = 0; i < src->nb_extended_buf; i++) {
     447           0 :             dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
     448           0 :             if (!dst->extended_buf[i]) {
     449           0 :                 ret = AVERROR(ENOMEM);
     450           0 :                 goto fail;
     451             :             }
     452             :         }
     453             :     }
     454             : 
     455      311891 :     if (src->hw_frames_ctx) {
     456           0 :         dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
     457           0 :         if (!dst->hw_frames_ctx) {
     458           0 :             ret = AVERROR(ENOMEM);
     459           0 :             goto fail;
     460             :         }
     461             :     }
     462             : 
     463             :     /* duplicate extended data */
     464      311891 :     if (src->extended_data != src->data) {
     465           0 :         int ch = src->channels;
     466             : 
     467           0 :         if (!ch) {
     468           0 :             ret = AVERROR(EINVAL);
     469           0 :             goto fail;
     470             :         }
     471             :         CHECK_CHANNELS_CONSISTENCY(src);
     472             : 
     473           0 :         dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
     474           0 :         if (!dst->extended_data) {
     475           0 :             ret = AVERROR(ENOMEM);
     476           0 :             goto fail;
     477             :         }
     478           0 :         memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
     479             :     } else
     480      311891 :         dst->extended_data = dst->data;
     481             : 
     482      311891 :     memcpy(dst->data,     src->data,     sizeof(src->data));
     483      311891 :     memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
     484             : 
     485      311891 :     return 0;
     486             : 
     487           0 : fail:
     488           0 :     av_frame_unref(dst);
     489           0 :     return ret;
     490             : }
     491             : 
     492       12478 : AVFrame *av_frame_clone(const AVFrame *src)
     493             : {
     494       12478 :     AVFrame *ret = av_frame_alloc();
     495             : 
     496       12478 :     if (!ret)
     497           0 :         return NULL;
     498             : 
     499       12478 :     if (av_frame_ref(ret, src) < 0)
     500           0 :         av_frame_free(&ret);
     501             : 
     502       12478 :     return ret;
     503             : }
     504             : 
     505     3339192 : void av_frame_unref(AVFrame *frame)
     506             : {
     507             :     int i;
     508             : 
     509     3339192 :     if (!frame)
     510           0 :         return;
     511             : 
     512     3339192 :     wipe_side_data(frame);
     513             : 
     514    30052728 :     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
     515    26713536 :         av_buffer_unref(&frame->buf[i]);
     516     3339192 :     for (i = 0; i < frame->nb_extended_buf; i++)
     517           0 :         av_buffer_unref(&frame->extended_buf[i]);
     518     3339192 :     av_freep(&frame->extended_buf);
     519     3339192 :     av_dict_free(&frame->metadata);
     520             : #if FF_API_FRAME_QP
     521     3339192 :     av_buffer_unref(&frame->qp_table_buf);
     522             : #endif
     523             : 
     524     3339192 :     av_buffer_unref(&frame->hw_frames_ctx);
     525             : 
     526     3339192 :     av_buffer_unref(&frame->opaque_ref);
     527             : 
     528     3339192 :     get_frame_defaults(frame);
     529             : }
     530             : 
     531     1158125 : void av_frame_move_ref(AVFrame *dst, AVFrame *src)
     532             : {
     533             :     av_assert1(dst->width == 0 && dst->height == 0);
     534             :     av_assert1(dst->channels == 0);
     535             : 
     536     1158125 :     *dst = *src;
     537     1158125 :     if (src->extended_data == src->data)
     538     1158125 :         dst->extended_data = dst->data;
     539     1158125 :     memset(src, 0, sizeof(*src));
     540     1158125 :     get_frame_defaults(src);
     541     1158125 : }
     542             : 
     543       15637 : int av_frame_is_writable(AVFrame *frame)
     544             : {
     545       15637 :     int i, ret = 1;
     546             : 
     547             :     /* assume non-refcounted frames are not writable */
     548       15637 :     if (!frame->buf[0])
     549           0 :         return 0;
     550             : 
     551      140733 :     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
     552      125096 :         if (frame->buf[i])
     553       32374 :             ret &= !!av_buffer_is_writable(frame->buf[i]);
     554       15637 :     for (i = 0; i < frame->nb_extended_buf; i++)
     555           0 :         ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
     556             : 
     557       15637 :     return ret;
     558             : }
     559             : 
     560        1625 : int av_frame_make_writable(AVFrame *frame)
     561             : {
     562             :     AVFrame tmp;
     563             :     int ret;
     564             : 
     565        1625 :     if (!frame->buf[0])
     566           0 :         return AVERROR(EINVAL);
     567             : 
     568        1625 :     if (av_frame_is_writable(frame))
     569         532 :         return 0;
     570             : 
     571        1093 :     memset(&tmp, 0, sizeof(tmp));
     572        1093 :     tmp.format         = frame->format;
     573        1093 :     tmp.width          = frame->width;
     574        1093 :     tmp.height         = frame->height;
     575        1093 :     tmp.channels       = frame->channels;
     576        1093 :     tmp.channel_layout = frame->channel_layout;
     577        1093 :     tmp.nb_samples     = frame->nb_samples;
     578        1093 :     ret = av_frame_get_buffer(&tmp, 32);
     579        1093 :     if (ret < 0)
     580           0 :         return ret;
     581             : 
     582        1093 :     ret = av_frame_copy(&tmp, frame);
     583        1093 :     if (ret < 0) {
     584           0 :         av_frame_unref(&tmp);
     585           0 :         return ret;
     586             :     }
     587             : 
     588        1093 :     ret = av_frame_copy_props(&tmp, frame);
     589        1093 :     if (ret < 0) {
     590           0 :         av_frame_unref(&tmp);
     591           0 :         return ret;
     592             :     }
     593             : 
     594        1093 :     av_frame_unref(frame);
     595             : 
     596        1093 :     *frame = tmp;
     597        1093 :     if (tmp.data == tmp.extended_data)
     598        1093 :         frame->extended_data = frame->data;
     599             : 
     600        1093 :     return 0;
     601             : }
     602             : 
     603      345585 : int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
     604             : {
     605      345585 :     return frame_copy_props(dst, src, 1);
     606             : }
     607             : 
     608        6196 : AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
     609             : {
     610             :     uint8_t *data;
     611             :     int planes, i;
     612             : 
     613        6196 :     if (frame->nb_samples) {
     614        1295 :         int channels = frame->channels;
     615        1295 :         if (!channels)
     616           0 :             return NULL;
     617             :         CHECK_CHANNELS_CONSISTENCY(frame);
     618        1295 :         planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
     619             :     } else
     620        4901 :         planes = 4;
     621             : 
     622        6196 :     if (plane < 0 || plane >= planes || !frame->extended_data[plane])
     623           0 :         return NULL;
     624        6196 :     data = frame->extended_data[plane];
     625             : 
     626       14189 :     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
     627       14189 :         AVBufferRef *buf = frame->buf[i];
     628       14189 :         if (data >= buf->data && data < buf->data + buf->size)
     629        6196 :             return buf;
     630             :     }
     631           0 :     for (i = 0; i < frame->nb_extended_buf; i++) {
     632           0 :         AVBufferRef *buf = frame->extended_buf[i];
     633           0 :         if (data >= buf->data && data < buf->data + buf->size)
     634           0 :             return buf;
     635             :     }
     636           0 :     return NULL;
     637             : }
     638             : 
     639       98356 : static AVFrameSideData *frame_new_side_data(AVFrame *frame,
     640             :                                             enum AVFrameSideDataType type,
     641             :                                             AVBufferRef *buf)
     642             : {
     643             :     AVFrameSideData *ret, **tmp;
     644             : 
     645       98356 :     if (!buf)
     646           0 :         return NULL;
     647             : 
     648       98356 :     if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
     649           0 :         goto fail;
     650             : 
     651       98356 :     tmp = av_realloc(frame->side_data,
     652       98356 :                      (frame->nb_side_data + 1) * sizeof(*frame->side_data));
     653       98356 :     if (!tmp)
     654           0 :         goto fail;
     655       98356 :     frame->side_data = tmp;
     656             : 
     657       98356 :     ret = av_mallocz(sizeof(*ret));
     658       98356 :     if (!ret)
     659           0 :         goto fail;
     660             : 
     661       98356 :     ret->buf = buf;
     662       98356 :     ret->data = ret->buf->data;
     663       98356 :     ret->size = buf->size;
     664       98356 :     ret->type = type;
     665             : 
     666       98356 :     frame->side_data[frame->nb_side_data++] = ret;
     667             : 
     668       98356 :     return ret;
     669           0 : fail:
     670           0 :     av_buffer_unref(&buf);
     671           0 :     return NULL;
     672             : }
     673             : 
     674       34387 : AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
     675             :                                         enum AVFrameSideDataType type,
     676             :                                         int size)
     677             : {
     678             : 
     679       34387 :     return frame_new_side_data(frame, type, av_buffer_alloc(size));
     680             : }
     681             : 
     682      326490 : AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
     683             :                                         enum AVFrameSideDataType type)
     684             : {
     685             :     int i;
     686             : 
     687      351009 :     for (i = 0; i < frame->nb_side_data; i++) {
     688       24546 :         if (frame->side_data[i]->type == type)
     689          27 :             return frame->side_data[i];
     690             :     }
     691      326463 :     return NULL;
     692             : }
     693             : 
     694        3145 : static int frame_copy_video(AVFrame *dst, const AVFrame *src)
     695             : {
     696             :     const uint8_t *src_data[4];
     697             :     int i, planes;
     698             : 
     699        6290 :     if (dst->width  < src->width ||
     700        3145 :         dst->height < src->height)
     701           0 :         return AVERROR(EINVAL);
     702             : 
     703        3145 :     planes = av_pix_fmt_count_planes(dst->format);
     704        9730 :     for (i = 0; i < planes; i++)
     705        6585 :         if (!dst->data[i] || !src->data[i])
     706           0 :             return AVERROR(EINVAL);
     707             : 
     708        3145 :     memcpy(src_data, src->data, sizeof(src_data));
     709        6290 :     av_image_copy(dst->data, dst->linesize,
     710        3145 :                   src_data, src->linesize,
     711        3145 :                   dst->format, src->width, src->height);
     712             : 
     713        3145 :     return 0;
     714             : }
     715             : 
     716           0 : static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
     717             : {
     718           0 :     int planar   = av_sample_fmt_is_planar(dst->format);
     719           0 :     int channels = dst->channels;
     720           0 :     int planes   = planar ? channels : 1;
     721             :     int i;
     722             : 
     723           0 :     if (dst->nb_samples     != src->nb_samples ||
     724           0 :         dst->channels       != src->channels ||
     725           0 :         dst->channel_layout != src->channel_layout)
     726           0 :         return AVERROR(EINVAL);
     727             : 
     728             :     CHECK_CHANNELS_CONSISTENCY(src);
     729             : 
     730           0 :     for (i = 0; i < planes; i++)
     731           0 :         if (!dst->extended_data[i] || !src->extended_data[i])
     732           0 :             return AVERROR(EINVAL);
     733             : 
     734           0 :     av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
     735           0 :                     dst->nb_samples, channels, dst->format);
     736             : 
     737           0 :     return 0;
     738             : }
     739             : 
     740        3145 : int av_frame_copy(AVFrame *dst, const AVFrame *src)
     741             : {
     742        3145 :     if (dst->format != src->format || dst->format < 0)
     743           0 :         return AVERROR(EINVAL);
     744             : 
     745        3145 :     if (dst->width > 0 && dst->height > 0)
     746        3145 :         return frame_copy_video(dst, src);
     747           0 :     else if (dst->nb_samples > 0 && dst->channels > 0)
     748           0 :         return frame_copy_audio(dst, src);
     749             : 
     750           0 :     return AVERROR(EINVAL);
     751             : }
     752             : 
     753           0 : void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
     754             : {
     755             :     int i;
     756             : 
     757           0 :     for (i = 0; i < frame->nb_side_data; i++) {
     758           0 :         AVFrameSideData *sd = frame->side_data[i];
     759           0 :         if (sd->type == type) {
     760           0 :             free_side_data(&frame->side_data[i]);
     761           0 :             frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
     762           0 :             frame->nb_side_data--;
     763             :         }
     764             :     }
     765           0 : }
     766             : 
     767           0 : const char *av_frame_side_data_name(enum AVFrameSideDataType type)
     768             : {
     769           0 :     switch(type) {
     770           0 :     case AV_FRAME_DATA_PANSCAN:         return "AVPanScan";
     771           0 :     case AV_FRAME_DATA_A53_CC:          return "ATSC A53 Part 4 Closed Captions";
     772           0 :     case AV_FRAME_DATA_STEREO3D:        return "Stereoscopic 3d metadata";
     773           0 :     case AV_FRAME_DATA_MATRIXENCODING:  return "AVMatrixEncoding";
     774           0 :     case AV_FRAME_DATA_DOWNMIX_INFO:    return "Metadata relevant to a downmix procedure";
     775           0 :     case AV_FRAME_DATA_REPLAYGAIN:      return "AVReplayGain";
     776           0 :     case AV_FRAME_DATA_DISPLAYMATRIX:   return "3x3 displaymatrix";
     777           0 :     case AV_FRAME_DATA_AFD:             return "Active format description";
     778           0 :     case AV_FRAME_DATA_MOTION_VECTORS:  return "Motion vectors";
     779           0 :     case AV_FRAME_DATA_SKIP_SAMPLES:    return "Skip samples";
     780           0 :     case AV_FRAME_DATA_AUDIO_SERVICE_TYPE:          return "Audio service type";
     781           0 :     case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA:  return "Mastering display metadata";
     782           0 :     case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL:         return "Content light level metadata";
     783           0 :     case AV_FRAME_DATA_GOP_TIMECODE:                return "GOP timecode";
     784           0 :     case AV_FRAME_DATA_ICC_PROFILE:                 return "ICC profile";
     785             :     }
     786           0 :     return NULL;
     787             : }
     788             : 
     789      102029 : static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
     790             :                                  const AVPixFmtDescriptor *desc)
     791             : {
     792             :     int i, j;
     793             : 
     794      380144 :     for (i = 0; frame->data[i]; i++) {
     795      282731 :         const AVComponentDescriptor *comp = NULL;
     796      282731 :         int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
     797      282731 :         int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
     798             : 
     799      282731 :         if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL) && i == 1) {
     800        4616 :             offsets[i] = 0;
     801        4616 :             break;
     802             :         }
     803             : 
     804             :         /* find any component descriptor for this plane */
     805      543177 :         for (j = 0; j < desc->nb_components; j++) {
     806      543177 :             if (desc->comp[j].plane == i) {
     807      278115 :                 comp = &desc->comp[j];
     808      278115 :                 break;
     809             :             }
     810             :         }
     811      278115 :         if (!comp)
     812           0 :             return AVERROR_BUG;
     813             : 
     814      556230 :         offsets[i] = (frame->crop_top  >> shift_y) * frame->linesize[i] +
     815      278115 :                      (frame->crop_left >> shift_x) * comp->step;
     816             :     }
     817             : 
     818      102029 :     return 0;
     819             : }
     820             : 
     821      102308 : int av_frame_apply_cropping(AVFrame *frame, int flags)
     822             : {
     823             :     const AVPixFmtDescriptor *desc;
     824             :     size_t offsets[4];
     825             :     int i;
     826             : 
     827      102308 :     if (!(frame->width > 0 && frame->height > 0))
     828           0 :         return AVERROR(EINVAL);
     829             : 
     830      204616 :     if (frame->crop_left >= INT_MAX - frame->crop_right        ||
     831      204616 :         frame->crop_top  >= INT_MAX - frame->crop_bottom       ||
     832      204616 :         (frame->crop_left + frame->crop_right) >= frame->width ||
     833      102308 :         (frame->crop_top + frame->crop_bottom) >= frame->height)
     834           0 :         return AVERROR(ERANGE);
     835             : 
     836      102308 :     desc = av_pix_fmt_desc_get(frame->format);
     837      102308 :     if (!desc)
     838           0 :         return AVERROR_BUG;
     839             : 
     840             :     /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
     841             :      * formats cannot be easily handled here either (and corresponding decoders
     842             :      * should not export any cropping anyway), so do the same for those as well.
     843             :      * */
     844      102308 :     if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
     845         286 :         frame->width      -= frame->crop_right;
     846         286 :         frame->height     -= frame->crop_bottom;
     847         286 :         frame->crop_right  = 0;
     848         286 :         frame->crop_bottom = 0;
     849         286 :         return 0;
     850             :     }
     851             : 
     852             :     /* calculate the offsets for each plane */
     853      102022 :     calc_cropping_offsets(offsets, frame, desc);
     854             : 
     855             :     /* adjust the offsets to avoid breaking alignment */
     856      102022 :     if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
     857       93011 :         int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
     858       93011 :         int min_log2_align = INT_MAX;
     859             : 
     860      348688 :         for (i = 0; frame->data[i]; i++) {
     861      255677 :             int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
     862      255677 :             min_log2_align = FFMIN(log2_align, min_log2_align);
     863             :         }
     864             : 
     865             :         /* we assume, and it should always be true, that the data alignment is
     866             :          * related to the cropping alignment by a constant power-of-2 factor */
     867       93011 :         if (log2_crop_align < min_log2_align)
     868           0 :             return AVERROR_BUG;
     869             : 
     870       93011 :         if (min_log2_align < 5) {
     871           7 :             frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
     872           7 :             calc_cropping_offsets(offsets, frame, desc);
     873             :         }
     874             :     }
     875             : 
     876      384732 :     for (i = 0; frame->data[i]; i++)
     877      282710 :         frame->data[i] += offsets[i];
     878             : 
     879      102022 :     frame->width      -= (frame->crop_left + frame->crop_right);
     880      102022 :     frame->height     -= (frame->crop_top  + frame->crop_bottom);
     881      102022 :     frame->crop_left   = 0;
     882      102022 :     frame->crop_right  = 0;
     883      102022 :     frame->crop_top    = 0;
     884      102022 :     frame->crop_bottom = 0;
     885             : 
     886      102022 :     return 0;
     887             : }

Generated by: LCOV version 1.13