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