]> git.sesse.net Git - ffmpeg/blob - libavutil/frame.c
Merge commit '5fa255b65c7887cc913f097aed1b581fbf1a8745'
[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
29 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
30 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
31 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
32 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
33 MAKE_ACCESSORS(AVFrame, frame, int,     channels)
34 MAKE_ACCESSORS(AVFrame, frame, int,     sample_rate)
35 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
36 MAKE_ACCESSORS(AVFrame, frame, int,     decode_error_flags)
37 MAKE_ACCESSORS(AVFrame, frame, int,     pkt_size)
38 MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
39 MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
40
41 #define CHECK_CHANNELS_CONSISTENCY(frame) \
42     av_assert2(!(frame)->channel_layout || \
43                (frame)->channels == \
44                av_get_channel_layout_nb_channels((frame)->channel_layout))
45
46 AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
47
48 #if FF_API_FRAME_QP
49 int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
50 {
51     av_buffer_unref(&f->qp_table_buf);
52
53     f->qp_table_buf = buf;
54
55 FF_DISABLE_DEPRECATION_WARNINGS
56     f->qscale_table = buf->data;
57     f->qstride      = stride;
58     f->qscale_type  = qp_type;
59 FF_ENABLE_DEPRECATION_WARNINGS
60
61     return 0;
62 }
63
64 int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
65 {
66 FF_DISABLE_DEPRECATION_WARNINGS
67     *stride = f->qstride;
68     *type   = f->qscale_type;
69 FF_ENABLE_DEPRECATION_WARNINGS
70
71     if (!f->qp_table_buf)
72         return NULL;
73
74     return f->qp_table_buf->data;
75 }
76 #endif
77
78 const char *av_get_colorspace_name(enum AVColorSpace val)
79 {
80     static const char * const name[] = {
81         [AVCOL_SPC_RGB]       = "GBR",
82         [AVCOL_SPC_BT709]     = "bt709",
83         [AVCOL_SPC_FCC]       = "fcc",
84         [AVCOL_SPC_BT470BG]   = "bt470bg",
85         [AVCOL_SPC_SMPTE170M] = "smpte170m",
86         [AVCOL_SPC_SMPTE240M] = "smpte240m",
87         [AVCOL_SPC_YCOCG]     = "YCgCo",
88     };
89     if ((unsigned)val >= FF_ARRAY_ELEMS(name))
90         return NULL;
91     return name[val];
92 }
93
94 static void get_frame_defaults(AVFrame *frame)
95 {
96     if (frame->extended_data != frame->data)
97         av_freep(&frame->extended_data);
98
99     memset(frame, 0, sizeof(*frame));
100
101     frame->pts                   =
102     frame->pkt_dts               =
103     frame->pkt_pts               = AV_NOPTS_VALUE;
104     frame->best_effort_timestamp = AV_NOPTS_VALUE;
105     frame->pkt_duration        = 0;
106     frame->pkt_pos             = -1;
107     frame->pkt_size            = -1;
108     frame->key_frame           = 1;
109     frame->sample_aspect_ratio = (AVRational){ 0, 1 };
110     frame->format              = -1; /* unknown */
111     frame->extended_data       = frame->data;
112     frame->color_primaries     = AVCOL_PRI_UNSPECIFIED;
113     frame->color_trc           = AVCOL_TRC_UNSPECIFIED;
114     frame->colorspace          = AVCOL_SPC_UNSPECIFIED;
115     frame->color_range         = AVCOL_RANGE_UNSPECIFIED;
116     frame->chroma_location     = AVCHROMA_LOC_UNSPECIFIED;
117     frame->flags               = 0;
118 }
119
120 static void free_side_data(AVFrameSideData **ptr_sd)
121 {
122     AVFrameSideData *sd = *ptr_sd;
123
124     av_buffer_unref(&sd->buf);
125     av_dict_free(&sd->metadata);
126     av_freep(ptr_sd);
127 }
128
129 static void wipe_side_data(AVFrame *frame)
130 {
131     int i;
132
133     for (i = 0; i < frame->nb_side_data; i++) {
134         free_side_data(&frame->side_data[i]);
135     }
136     frame->nb_side_data = 0;
137
138     av_freep(&frame->side_data);
139 }
140
141 AVFrame *av_frame_alloc(void)
142 {
143     AVFrame *frame = av_mallocz(sizeof(*frame));
144
145     if (!frame)
146         return NULL;
147
148     frame->extended_data = NULL;
149     get_frame_defaults(frame);
150
151     return frame;
152 }
153
154 void av_frame_free(AVFrame **frame)
155 {
156     if (!frame || !*frame)
157         return;
158
159     av_frame_unref(*frame);
160     av_freep(frame);
161 }
162
163 static int get_video_buffer(AVFrame *frame, int align)
164 {
165     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
166     int ret, i;
167
168     if (!desc)
169         return AVERROR(EINVAL);
170
171     if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
172         return ret;
173
174     if (!frame->linesize[0]) {
175         for(i=1; i<=align; i+=i) {
176             ret = av_image_fill_linesizes(frame->linesize, frame->format,
177                                           FFALIGN(frame->width, i));
178             if (ret < 0)
179                 return ret;
180             if (!(frame->linesize[0] & (align-1)))
181                 break;
182         }
183
184         for (i = 0; i < 4 && frame->linesize[i]; i++)
185             frame->linesize[i] = FFALIGN(frame->linesize[i], align);
186     }
187
188     for (i = 0; i < 4 && frame->linesize[i]; i++) {
189         int h = FFALIGN(frame->height, 32);
190         if (i == 1 || i == 2)
191             h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
192
193         frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
194         if (!frame->buf[i])
195             goto fail;
196
197         frame->data[i] = frame->buf[i]->data;
198     }
199     if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
200         av_buffer_unref(&frame->buf[1]);
201         frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
202         if (!frame->buf[1])
203             goto fail;
204         frame->data[1] = frame->buf[1]->data;
205     }
206
207     frame->extended_data = frame->data;
208
209     return 0;
210 fail:
211     av_frame_unref(frame);
212     return AVERROR(ENOMEM);
213 }
214
215 static int get_audio_buffer(AVFrame *frame, int align)
216 {
217     int channels;
218     int planar   = av_sample_fmt_is_planar(frame->format);
219     int planes;
220     int ret, i;
221
222     if (!frame->channels)
223         frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
224
225     channels = frame->channels;
226     planes = planar ? channels : 1;
227
228     CHECK_CHANNELS_CONSISTENCY(frame);
229     if (!frame->linesize[0]) {
230         ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
231                                          frame->nb_samples, frame->format,
232                                          align);
233         if (ret < 0)
234             return ret;
235     }
236
237     if (planes > AV_NUM_DATA_POINTERS) {
238         frame->extended_data = av_mallocz_array(planes,
239                                           sizeof(*frame->extended_data));
240         frame->extended_buf  = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
241                                           sizeof(*frame->extended_buf));
242         if (!frame->extended_data || !frame->extended_buf) {
243             av_freep(&frame->extended_data);
244             av_freep(&frame->extended_buf);
245             return AVERROR(ENOMEM);
246         }
247         frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
248     } else
249         frame->extended_data = frame->data;
250
251     for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
252         frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
253         if (!frame->buf[i]) {
254             av_frame_unref(frame);
255             return AVERROR(ENOMEM);
256         }
257         frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
258     }
259     for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
260         frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
261         if (!frame->extended_buf[i]) {
262             av_frame_unref(frame);
263             return AVERROR(ENOMEM);
264         }
265         frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
266     }
267     return 0;
268
269 }
270
271 int av_frame_get_buffer(AVFrame *frame, int align)
272 {
273     if (frame->format < 0)
274         return AVERROR(EINVAL);
275
276     if (frame->width > 0 && frame->height > 0)
277         return get_video_buffer(frame, align);
278     else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
279         return get_audio_buffer(frame, align);
280
281     return AVERROR(EINVAL);
282 }
283
284 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
285 {
286     int i;
287
288     dst->key_frame              = src->key_frame;
289     dst->pict_type              = src->pict_type;
290     dst->sample_aspect_ratio    = src->sample_aspect_ratio;
291     dst->pts                    = src->pts;
292     dst->repeat_pict            = src->repeat_pict;
293     dst->interlaced_frame       = src->interlaced_frame;
294     dst->top_field_first        = src->top_field_first;
295     dst->palette_has_changed    = src->palette_has_changed;
296     dst->sample_rate            = src->sample_rate;
297     dst->opaque                 = src->opaque;
298     dst->pkt_pts                = src->pkt_pts;
299     dst->pkt_dts                = src->pkt_dts;
300     dst->pkt_pos                = src->pkt_pos;
301     dst->pkt_size               = src->pkt_size;
302     dst->pkt_duration           = src->pkt_duration;
303     dst->reordered_opaque       = src->reordered_opaque;
304     dst->quality                = src->quality;
305     dst->best_effort_timestamp  = src->best_effort_timestamp;
306     dst->coded_picture_number   = src->coded_picture_number;
307     dst->display_picture_number = src->display_picture_number;
308     dst->flags                  = src->flags;
309     dst->decode_error_flags     = src->decode_error_flags;
310     dst->color_primaries        = src->color_primaries;
311     dst->color_trc              = src->color_trc;
312     dst->colorspace             = src->colorspace;
313     dst->color_range            = src->color_range;
314     dst->chroma_location        = src->chroma_location;
315
316     av_dict_copy(&dst->metadata, src->metadata, 0);
317
318 #if FF_API_ERROR_FRAME
319 FF_DISABLE_DEPRECATION_WARNINGS
320     memcpy(dst->error, src->error, sizeof(dst->error));
321 FF_ENABLE_DEPRECATION_WARNINGS
322 #endif
323
324     for (i = 0; i < src->nb_side_data; i++) {
325         const AVFrameSideData *sd_src = src->side_data[i];
326         AVFrameSideData *sd_dst;
327         if (   sd_src->type == AV_FRAME_DATA_PANSCAN
328             && (src->width != dst->width || src->height != dst->height))
329             continue;
330         if (force_copy) {
331             sd_dst = av_frame_new_side_data(dst, sd_src->type,
332                                             sd_src->size);
333             if (!sd_dst) {
334                 wipe_side_data(dst);
335                 return AVERROR(ENOMEM);
336             }
337             memcpy(sd_dst->data, sd_src->data, sd_src->size);
338         } else {
339             sd_dst = av_frame_new_side_data(dst, sd_src->type, 0);
340             if (!sd_dst) {
341                 wipe_side_data(dst);
342                 return AVERROR(ENOMEM);
343             }
344             sd_dst->buf = av_buffer_ref(sd_src->buf);
345             if (!sd_dst->buf) {
346                 wipe_side_data(dst);
347                 return AVERROR(ENOMEM);
348             }
349             sd_dst->data = sd_dst->buf->data;
350             sd_dst->size = sd_dst->buf->size;
351         }
352         av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
353     }
354
355 #if FF_API_FRAME_QP
356 FF_DISABLE_DEPRECATION_WARNINGS
357     dst->qscale_table = NULL;
358     dst->qstride      = 0;
359     dst->qscale_type  = 0;
360     av_buffer_unref(&dst->qp_table_buf);
361     if (src->qp_table_buf) {
362         dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
363         if (dst->qp_table_buf) {
364             dst->qscale_table = dst->qp_table_buf->data;
365             dst->qstride      = src->qstride;
366             dst->qscale_type  = src->qscale_type;
367         }
368     }
369 FF_ENABLE_DEPRECATION_WARNINGS
370 #endif
371
372     return 0;
373 }
374
375 int av_frame_ref(AVFrame *dst, const AVFrame *src)
376 {
377     int i, ret = 0;
378
379     av_assert1(dst->width == 0 && dst->height == 0);
380     av_assert1(dst->channels == 0);
381
382     dst->format         = src->format;
383     dst->width          = src->width;
384     dst->height         = src->height;
385     dst->channels       = src->channels;
386     dst->channel_layout = src->channel_layout;
387     dst->nb_samples     = src->nb_samples;
388
389     ret = frame_copy_props(dst, src, 0);
390     if (ret < 0)
391         return ret;
392
393     /* duplicate the frame data if it's not refcounted */
394     if (!src->buf[0]) {
395         ret = av_frame_get_buffer(dst, 32);
396         if (ret < 0)
397             return ret;
398
399         ret = av_frame_copy(dst, src);
400         if (ret < 0)
401             av_frame_unref(dst);
402
403         return ret;
404     }
405
406     /* ref the buffers */
407     for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
408         if (!src->buf[i])
409             continue;
410         dst->buf[i] = av_buffer_ref(src->buf[i]);
411         if (!dst->buf[i]) {
412             ret = AVERROR(ENOMEM);
413             goto fail;
414         }
415     }
416
417     if (src->extended_buf) {
418         dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
419                                        src->nb_extended_buf);
420         if (!dst->extended_buf) {
421             ret = AVERROR(ENOMEM);
422             goto fail;
423         }
424         dst->nb_extended_buf = src->nb_extended_buf;
425
426         for (i = 0; i < src->nb_extended_buf; i++) {
427             dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
428             if (!dst->extended_buf[i]) {
429                 ret = AVERROR(ENOMEM);
430                 goto fail;
431             }
432         }
433     }
434
435     if (src->hw_frames_ctx) {
436         dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
437         if (!dst->hw_frames_ctx) {
438             ret = AVERROR(ENOMEM);
439             goto fail;
440         }
441     }
442
443     /* duplicate extended data */
444     if (src->extended_data != src->data) {
445         int ch = src->channels;
446
447         if (!ch) {
448             ret = AVERROR(EINVAL);
449             goto fail;
450         }
451         CHECK_CHANNELS_CONSISTENCY(src);
452
453         dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
454         if (!dst->extended_data) {
455             ret = AVERROR(ENOMEM);
456             goto fail;
457         }
458         memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
459     } else
460         dst->extended_data = dst->data;
461
462     memcpy(dst->data,     src->data,     sizeof(src->data));
463     memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
464
465     return 0;
466
467 fail:
468     av_frame_unref(dst);
469     return ret;
470 }
471
472 AVFrame *av_frame_clone(const AVFrame *src)
473 {
474     AVFrame *ret = av_frame_alloc();
475
476     if (!ret)
477         return NULL;
478
479     if (av_frame_ref(ret, src) < 0)
480         av_frame_free(&ret);
481
482     return ret;
483 }
484
485 void av_frame_unref(AVFrame *frame)
486 {
487     int i;
488
489     if (!frame)
490         return;
491
492     wipe_side_data(frame);
493
494     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
495         av_buffer_unref(&frame->buf[i]);
496     for (i = 0; i < frame->nb_extended_buf; i++)
497         av_buffer_unref(&frame->extended_buf[i]);
498     av_freep(&frame->extended_buf);
499     av_dict_free(&frame->metadata);
500 #if FF_API_FRAME_QP
501     av_buffer_unref(&frame->qp_table_buf);
502 #endif
503
504     av_buffer_unref(&frame->hw_frames_ctx);
505
506     get_frame_defaults(frame);
507 }
508
509 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
510 {
511     av_assert1(dst->width == 0 && dst->height == 0);
512     av_assert1(dst->channels == 0);
513
514     *dst = *src;
515     if (src->extended_data == src->data)
516         dst->extended_data = dst->data;
517     memset(src, 0, sizeof(*src));
518     get_frame_defaults(src);
519 }
520
521 int av_frame_is_writable(AVFrame *frame)
522 {
523     int i, ret = 1;
524
525     /* assume non-refcounted frames are not writable */
526     if (!frame->buf[0])
527         return 0;
528
529     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
530         if (frame->buf[i])
531             ret &= !!av_buffer_is_writable(frame->buf[i]);
532     for (i = 0; i < frame->nb_extended_buf; i++)
533         ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
534
535     return ret;
536 }
537
538 int av_frame_make_writable(AVFrame *frame)
539 {
540     AVFrame tmp;
541     int ret;
542
543     if (!frame->buf[0])
544         return AVERROR(EINVAL);
545
546     if (av_frame_is_writable(frame))
547         return 0;
548
549     memset(&tmp, 0, sizeof(tmp));
550     tmp.format         = frame->format;
551     tmp.width          = frame->width;
552     tmp.height         = frame->height;
553     tmp.channels       = frame->channels;
554     tmp.channel_layout = frame->channel_layout;
555     tmp.nb_samples     = frame->nb_samples;
556     ret = av_frame_get_buffer(&tmp, 32);
557     if (ret < 0)
558         return ret;
559
560     ret = av_frame_copy(&tmp, frame);
561     if (ret < 0) {
562         av_frame_unref(&tmp);
563         return ret;
564     }
565
566     ret = av_frame_copy_props(&tmp, frame);
567     if (ret < 0) {
568         av_frame_unref(&tmp);
569         return ret;
570     }
571
572     av_frame_unref(frame);
573
574     *frame = tmp;
575     if (tmp.data == tmp.extended_data)
576         frame->extended_data = frame->data;
577
578     return 0;
579 }
580
581 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
582 {
583     return frame_copy_props(dst, src, 1);
584 }
585
586 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
587 {
588     uint8_t *data;
589     int planes, i;
590
591     if (frame->nb_samples) {
592         int channels = frame->channels;
593         if (!channels)
594             return NULL;
595         CHECK_CHANNELS_CONSISTENCY(frame);
596         planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
597     } else
598         planes = 4;
599
600     if (plane < 0 || plane >= planes || !frame->extended_data[plane])
601         return NULL;
602     data = frame->extended_data[plane];
603
604     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
605         AVBufferRef *buf = frame->buf[i];
606         if (data >= buf->data && data < buf->data + buf->size)
607             return buf;
608     }
609     for (i = 0; i < frame->nb_extended_buf; i++) {
610         AVBufferRef *buf = frame->extended_buf[i];
611         if (data >= buf->data && data < buf->data + buf->size)
612             return buf;
613     }
614     return NULL;
615 }
616
617 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
618                                         enum AVFrameSideDataType type,
619                                         int size)
620 {
621     AVFrameSideData *ret, **tmp;
622
623     if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
624         return NULL;
625
626     tmp = av_realloc(frame->side_data,
627                      (frame->nb_side_data + 1) * sizeof(*frame->side_data));
628     if (!tmp)
629         return NULL;
630     frame->side_data = tmp;
631
632     ret = av_mallocz(sizeof(*ret));
633     if (!ret)
634         return NULL;
635
636     if (size > 0) {
637         ret->buf = av_buffer_alloc(size);
638         if (!ret->buf) {
639             av_freep(&ret);
640             return NULL;
641         }
642
643         ret->data = ret->buf->data;
644         ret->size = size;
645     }
646     ret->type = type;
647
648     frame->side_data[frame->nb_side_data++] = ret;
649
650     return ret;
651 }
652
653 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
654                                         enum AVFrameSideDataType type)
655 {
656     int i;
657
658     for (i = 0; i < frame->nb_side_data; i++) {
659         if (frame->side_data[i]->type == type)
660             return frame->side_data[i];
661     }
662     return NULL;
663 }
664
665 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
666 {
667     const uint8_t *src_data[4];
668     int i, planes;
669
670     if (dst->width  < src->width ||
671         dst->height < src->height)
672         return AVERROR(EINVAL);
673
674     planes = av_pix_fmt_count_planes(dst->format);
675     for (i = 0; i < planes; i++)
676         if (!dst->data[i] || !src->data[i])
677             return AVERROR(EINVAL);
678
679     memcpy(src_data, src->data, sizeof(src_data));
680     av_image_copy(dst->data, dst->linesize,
681                   src_data, src->linesize,
682                   dst->format, src->width, src->height);
683
684     return 0;
685 }
686
687 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
688 {
689     int planar   = av_sample_fmt_is_planar(dst->format);
690     int channels = dst->channels;
691     int planes   = planar ? channels : 1;
692     int i;
693
694     if (dst->nb_samples     != src->nb_samples ||
695         dst->channels       != src->channels ||
696         dst->channel_layout != src->channel_layout)
697         return AVERROR(EINVAL);
698
699     CHECK_CHANNELS_CONSISTENCY(src);
700
701     for (i = 0; i < planes; i++)
702         if (!dst->extended_data[i] || !src->extended_data[i])
703             return AVERROR(EINVAL);
704
705     av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
706                     dst->nb_samples, channels, dst->format);
707
708     return 0;
709 }
710
711 int av_frame_copy(AVFrame *dst, const AVFrame *src)
712 {
713     if (dst->format != src->format || dst->format < 0)
714         return AVERROR(EINVAL);
715
716     if (dst->width > 0 && dst->height > 0)
717         return frame_copy_video(dst, src);
718     else if (dst->nb_samples > 0 && dst->channel_layout)
719         return frame_copy_audio(dst, src);
720
721     return AVERROR(EINVAL);
722 }
723
724 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
725 {
726     int i;
727
728     for (i = 0; i < frame->nb_side_data; i++) {
729         AVFrameSideData *sd = frame->side_data[i];
730         if (sd->type == type) {
731             free_side_data(&frame->side_data[i]);
732             frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
733             frame->nb_side_data--;
734         }
735     }
736 }
737
738 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
739 {
740     switch(type) {
741     case AV_FRAME_DATA_PANSCAN:         return "AVPanScan";
742     case AV_FRAME_DATA_A53_CC:          return "ATSC A53 Part 4 Closed Captions";
743     case AV_FRAME_DATA_STEREO3D:        return "Stereoscopic 3d metadata";
744     case AV_FRAME_DATA_MATRIXENCODING:  return "AVMatrixEncoding";
745     case AV_FRAME_DATA_DOWNMIX_INFO:    return "Metadata relevant to a downmix procedure";
746     case AV_FRAME_DATA_REPLAYGAIN:      return "AVReplayGain";
747     case AV_FRAME_DATA_DISPLAYMATRIX:   return "3x3 displaymatrix";
748     case AV_FRAME_DATA_AFD:             return "Active format description";
749     case AV_FRAME_DATA_MOTION_VECTORS:  return "Motion vectors";
750     case AV_FRAME_DATA_SKIP_SAMPLES:    return "Skip samples";
751     case AV_FRAME_DATA_AUDIO_SERVICE_TYPE:          return "Audio service type";
752     case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA:  return "Mastering display metadata";
753     case AV_FRAME_DATA_GOP_TIMECODE:                return "GOP timecode";
754     }
755     return NULL;
756 }