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