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