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