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