LCOV - code coverage report
Current view: top level - libavutil - frame.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 384 546 70.3 %
Date: 2018-05-20 11:54:08 Functions: 29 52 55.8 %

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

Generated by: LCOV version 1.13