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