]> git.sesse.net Git - ffmpeg/blob - libavutil/frame.c
avutil/frame: Remove deprecated AVFrame.error
[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     for (i = 0; i < src->nb_side_data; i++) {
320         const AVFrameSideData *sd_src = src->side_data[i];
321         AVFrameSideData *sd_dst;
322         if (   sd_src->type == AV_FRAME_DATA_PANSCAN
323             && (src->width != dst->width || src->height != dst->height))
324             continue;
325         if (force_copy) {
326             sd_dst = av_frame_new_side_data(dst, sd_src->type,
327                                             sd_src->size);
328             if (!sd_dst) {
329                 wipe_side_data(dst);
330                 return AVERROR(ENOMEM);
331             }
332             memcpy(sd_dst->data, sd_src->data, sd_src->size);
333         } else {
334             AVBufferRef *ref = av_buffer_ref(sd_src->buf);
335             sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
336             if (!sd_dst) {
337                 av_buffer_unref(&ref);
338                 wipe_side_data(dst);
339                 return AVERROR(ENOMEM);
340             }
341         }
342         av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
343     }
344
345     ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
346     ret |= av_buffer_replace(&dst->private_ref, src->private_ref);
347     return ret;
348 }
349
350 int av_frame_ref(AVFrame *dst, const AVFrame *src)
351 {
352     int i, ret = 0;
353
354     av_assert1(dst->width == 0 && dst->height == 0);
355     av_assert1(dst->channels == 0);
356
357     dst->format         = src->format;
358     dst->width          = src->width;
359     dst->height         = src->height;
360     dst->channels       = src->channels;
361     dst->channel_layout = src->channel_layout;
362     dst->nb_samples     = src->nb_samples;
363
364     ret = frame_copy_props(dst, src, 0);
365     if (ret < 0)
366         goto fail;
367
368     /* duplicate the frame data if it's not refcounted */
369     if (!src->buf[0]) {
370         ret = av_frame_get_buffer(dst, 0);
371         if (ret < 0)
372             goto fail;
373
374         ret = av_frame_copy(dst, src);
375         if (ret < 0)
376             goto fail;
377
378         return 0;
379     }
380
381     /* ref the buffers */
382     for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
383         if (!src->buf[i])
384             continue;
385         dst->buf[i] = av_buffer_ref(src->buf[i]);
386         if (!dst->buf[i]) {
387             ret = AVERROR(ENOMEM);
388             goto fail;
389         }
390     }
391
392     if (src->extended_buf) {
393         dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
394                                        src->nb_extended_buf);
395         if (!dst->extended_buf) {
396             ret = AVERROR(ENOMEM);
397             goto fail;
398         }
399         dst->nb_extended_buf = src->nb_extended_buf;
400
401         for (i = 0; i < src->nb_extended_buf; i++) {
402             dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
403             if (!dst->extended_buf[i]) {
404                 ret = AVERROR(ENOMEM);
405                 goto fail;
406             }
407         }
408     }
409
410     if (src->hw_frames_ctx) {
411         dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
412         if (!dst->hw_frames_ctx) {
413             ret = AVERROR(ENOMEM);
414             goto fail;
415         }
416     }
417
418     /* duplicate extended data */
419     if (src->extended_data != src->data) {
420         int ch = src->channels;
421
422         if (!ch) {
423             ret = AVERROR(EINVAL);
424             goto fail;
425         }
426         CHECK_CHANNELS_CONSISTENCY(src);
427
428         dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
429         if (!dst->extended_data) {
430             ret = AVERROR(ENOMEM);
431             goto fail;
432         }
433         memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
434     } else
435         dst->extended_data = dst->data;
436
437     memcpy(dst->data,     src->data,     sizeof(src->data));
438     memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
439
440     return 0;
441
442 fail:
443     av_frame_unref(dst);
444     return ret;
445 }
446
447 AVFrame *av_frame_clone(const AVFrame *src)
448 {
449     AVFrame *ret = av_frame_alloc();
450
451     if (!ret)
452         return NULL;
453
454     if (av_frame_ref(ret, src) < 0)
455         av_frame_free(&ret);
456
457     return ret;
458 }
459
460 void av_frame_unref(AVFrame *frame)
461 {
462     int i;
463
464     if (!frame)
465         return;
466
467     wipe_side_data(frame);
468
469     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
470         av_buffer_unref(&frame->buf[i]);
471     for (i = 0; i < frame->nb_extended_buf; i++)
472         av_buffer_unref(&frame->extended_buf[i]);
473     av_freep(&frame->extended_buf);
474     av_dict_free(&frame->metadata);
475
476     av_buffer_unref(&frame->hw_frames_ctx);
477
478     av_buffer_unref(&frame->opaque_ref);
479     av_buffer_unref(&frame->private_ref);
480
481     get_frame_defaults(frame);
482 }
483
484 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
485 {
486     av_assert1(dst->width == 0 && dst->height == 0);
487     av_assert1(dst->channels == 0);
488
489     *dst = *src;
490     if (src->extended_data == src->data)
491         dst->extended_data = dst->data;
492     memset(src, 0, sizeof(*src));
493     get_frame_defaults(src);
494 }
495
496 int av_frame_is_writable(AVFrame *frame)
497 {
498     int i, ret = 1;
499
500     /* assume non-refcounted frames are not writable */
501     if (!frame->buf[0])
502         return 0;
503
504     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
505         if (frame->buf[i])
506             ret &= !!av_buffer_is_writable(frame->buf[i]);
507     for (i = 0; i < frame->nb_extended_buf; i++)
508         ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
509
510     return ret;
511 }
512
513 int av_frame_make_writable(AVFrame *frame)
514 {
515     AVFrame tmp;
516     int ret;
517
518     if (!frame->buf[0])
519         return AVERROR(EINVAL);
520
521     if (av_frame_is_writable(frame))
522         return 0;
523
524     memset(&tmp, 0, sizeof(tmp));
525     tmp.format         = frame->format;
526     tmp.width          = frame->width;
527     tmp.height         = frame->height;
528     tmp.channels       = frame->channels;
529     tmp.channel_layout = frame->channel_layout;
530     tmp.nb_samples     = frame->nb_samples;
531
532     if (frame->hw_frames_ctx)
533         ret = av_hwframe_get_buffer(frame->hw_frames_ctx, &tmp, 0);
534     else
535         ret = av_frame_get_buffer(&tmp, 0);
536     if (ret < 0)
537         return ret;
538
539     ret = av_frame_copy(&tmp, frame);
540     if (ret < 0) {
541         av_frame_unref(&tmp);
542         return ret;
543     }
544
545     ret = av_frame_copy_props(&tmp, frame);
546     if (ret < 0) {
547         av_frame_unref(&tmp);
548         return ret;
549     }
550
551     av_frame_unref(frame);
552
553     *frame = tmp;
554     if (tmp.data == tmp.extended_data)
555         frame->extended_data = frame->data;
556
557     return 0;
558 }
559
560 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
561 {
562     return frame_copy_props(dst, src, 1);
563 }
564
565 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
566 {
567     uint8_t *data;
568     int planes, i;
569
570     if (frame->nb_samples) {
571         int channels = frame->channels;
572         if (!channels)
573             return NULL;
574         CHECK_CHANNELS_CONSISTENCY(frame);
575         planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
576     } else
577         planes = 4;
578
579     if (plane < 0 || plane >= planes || !frame->extended_data[plane])
580         return NULL;
581     data = frame->extended_data[plane];
582
583     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
584         AVBufferRef *buf = frame->buf[i];
585         if (data >= buf->data && data < buf->data + buf->size)
586             return buf;
587     }
588     for (i = 0; i < frame->nb_extended_buf; i++) {
589         AVBufferRef *buf = frame->extended_buf[i];
590         if (data >= buf->data && data < buf->data + buf->size)
591             return buf;
592     }
593     return NULL;
594 }
595
596 AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
597                                                  enum AVFrameSideDataType type,
598                                                  AVBufferRef *buf)
599 {
600     AVFrameSideData *ret, **tmp;
601
602     if (!buf)
603         return NULL;
604
605     if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
606         return NULL;
607
608     tmp = av_realloc(frame->side_data,
609                      (frame->nb_side_data + 1) * sizeof(*frame->side_data));
610     if (!tmp)
611         return NULL;
612     frame->side_data = tmp;
613
614     ret = av_mallocz(sizeof(*ret));
615     if (!ret)
616         return NULL;
617
618     ret->buf = buf;
619     ret->data = ret->buf->data;
620     ret->size = buf->size;
621     ret->type = type;
622
623     frame->side_data[frame->nb_side_data++] = ret;
624
625     return ret;
626 }
627
628 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
629                                         enum AVFrameSideDataType type,
630                                         buffer_size_t size)
631 {
632     AVFrameSideData *ret;
633     AVBufferRef *buf = av_buffer_alloc(size);
634     ret = av_frame_new_side_data_from_buf(frame, type, buf);
635     if (!ret)
636         av_buffer_unref(&buf);
637     return ret;
638 }
639
640 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
641                                         enum AVFrameSideDataType type)
642 {
643     int i;
644
645     for (i = 0; i < frame->nb_side_data; i++) {
646         if (frame->side_data[i]->type == type)
647             return frame->side_data[i];
648     }
649     return NULL;
650 }
651
652 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
653 {
654     const uint8_t *src_data[4];
655     int i, planes;
656
657     if (dst->width  < src->width ||
658         dst->height < src->height)
659         return AVERROR(EINVAL);
660
661     if (src->hw_frames_ctx || dst->hw_frames_ctx)
662         return av_hwframe_transfer_data(dst, src, 0);
663
664     planes = av_pix_fmt_count_planes(dst->format);
665     for (i = 0; i < planes; i++)
666         if (!dst->data[i] || !src->data[i])
667             return AVERROR(EINVAL);
668
669     memcpy(src_data, src->data, sizeof(src_data));
670     av_image_copy(dst->data, dst->linesize,
671                   src_data, src->linesize,
672                   dst->format, src->width, src->height);
673
674     return 0;
675 }
676
677 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
678 {
679     int planar   = av_sample_fmt_is_planar(dst->format);
680     int channels = dst->channels;
681     int planes   = planar ? channels : 1;
682     int i;
683
684     if (dst->nb_samples     != src->nb_samples ||
685         dst->channels       != src->channels ||
686         dst->channel_layout != src->channel_layout)
687         return AVERROR(EINVAL);
688
689     CHECK_CHANNELS_CONSISTENCY(src);
690
691     for (i = 0; i < planes; i++)
692         if (!dst->extended_data[i] || !src->extended_data[i])
693             return AVERROR(EINVAL);
694
695     av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
696                     dst->nb_samples, channels, dst->format);
697
698     return 0;
699 }
700
701 int av_frame_copy(AVFrame *dst, const AVFrame *src)
702 {
703     if (dst->format != src->format || dst->format < 0)
704         return AVERROR(EINVAL);
705
706     if (dst->width > 0 && dst->height > 0)
707         return frame_copy_video(dst, src);
708     else if (dst->nb_samples > 0 && dst->channels > 0)
709         return frame_copy_audio(dst, src);
710
711     return AVERROR(EINVAL);
712 }
713
714 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
715 {
716     int i;
717
718     for (i = frame->nb_side_data - 1; i >= 0; i--) {
719         AVFrameSideData *sd = frame->side_data[i];
720         if (sd->type == type) {
721             free_side_data(&frame->side_data[i]);
722             frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
723             frame->nb_side_data--;
724         }
725     }
726 }
727
728 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
729 {
730     switch(type) {
731     case AV_FRAME_DATA_PANSCAN:         return "AVPanScan";
732     case AV_FRAME_DATA_A53_CC:          return "ATSC A53 Part 4 Closed Captions";
733     case AV_FRAME_DATA_STEREO3D:        return "Stereo 3D";
734     case AV_FRAME_DATA_MATRIXENCODING:  return "AVMatrixEncoding";
735     case AV_FRAME_DATA_DOWNMIX_INFO:    return "Metadata relevant to a downmix procedure";
736     case AV_FRAME_DATA_REPLAYGAIN:      return "AVReplayGain";
737     case AV_FRAME_DATA_DISPLAYMATRIX:   return "3x3 displaymatrix";
738     case AV_FRAME_DATA_AFD:             return "Active format description";
739     case AV_FRAME_DATA_MOTION_VECTORS:  return "Motion vectors";
740     case AV_FRAME_DATA_SKIP_SAMPLES:    return "Skip samples";
741     case AV_FRAME_DATA_AUDIO_SERVICE_TYPE:          return "Audio service type";
742     case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA:  return "Mastering display metadata";
743     case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL:         return "Content light level metadata";
744     case AV_FRAME_DATA_GOP_TIMECODE:                return "GOP timecode";
745     case AV_FRAME_DATA_S12M_TIMECODE:               return "SMPTE 12-1 timecode";
746     case AV_FRAME_DATA_SPHERICAL:                   return "Spherical Mapping";
747     case AV_FRAME_DATA_ICC_PROFILE:                 return "ICC profile";
748     case AV_FRAME_DATA_DYNAMIC_HDR_PLUS: return "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)";
749     case AV_FRAME_DATA_REGIONS_OF_INTEREST: return "Regions Of Interest";
750     case AV_FRAME_DATA_VIDEO_ENC_PARAMS:            return "Video encoding parameters";
751     case AV_FRAME_DATA_SEI_UNREGISTERED:            return "H.26[45] User Data Unregistered SEI message";
752     case AV_FRAME_DATA_FILM_GRAIN_PARAMS:           return "Film grain parameters";
753     case AV_FRAME_DATA_DETECTION_BBOXES:            return "Bounding boxes for object detection and classification";
754     }
755     return NULL;
756 }
757
758 static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
759                                  const AVPixFmtDescriptor *desc)
760 {
761     int i, j;
762
763     for (i = 0; frame->data[i]; i++) {
764         const AVComponentDescriptor *comp = NULL;
765         int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
766         int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
767
768         if (desc->flags & (AV_PIX_FMT_FLAG_PAL | FF_PSEUDOPAL) && i == 1) {
769             offsets[i] = 0;
770             break;
771         }
772
773         /* find any component descriptor for this plane */
774         for (j = 0; j < desc->nb_components; j++) {
775             if (desc->comp[j].plane == i) {
776                 comp = &desc->comp[j];
777                 break;
778             }
779         }
780         if (!comp)
781             return AVERROR_BUG;
782
783         offsets[i] = (frame->crop_top  >> shift_y) * frame->linesize[i] +
784                      (frame->crop_left >> shift_x) * comp->step;
785     }
786
787     return 0;
788 }
789
790 int av_frame_apply_cropping(AVFrame *frame, int flags)
791 {
792     const AVPixFmtDescriptor *desc;
793     size_t offsets[4];
794     int i;
795
796     if (!(frame->width > 0 && frame->height > 0))
797         return AVERROR(EINVAL);
798
799     if (frame->crop_left >= INT_MAX - frame->crop_right        ||
800         frame->crop_top  >= INT_MAX - frame->crop_bottom       ||
801         (frame->crop_left + frame->crop_right) >= frame->width ||
802         (frame->crop_top + frame->crop_bottom) >= frame->height)
803         return AVERROR(ERANGE);
804
805     desc = av_pix_fmt_desc_get(frame->format);
806     if (!desc)
807         return AVERROR_BUG;
808
809     /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
810      * formats cannot be easily handled here either (and corresponding decoders
811      * should not export any cropping anyway), so do the same for those as well.
812      * */
813     if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
814         frame->width      -= frame->crop_right;
815         frame->height     -= frame->crop_bottom;
816         frame->crop_right  = 0;
817         frame->crop_bottom = 0;
818         return 0;
819     }
820
821     /* calculate the offsets for each plane */
822     calc_cropping_offsets(offsets, frame, desc);
823
824     /* adjust the offsets to avoid breaking alignment */
825     if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
826         int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
827         int min_log2_align = INT_MAX;
828
829         for (i = 0; frame->data[i]; i++) {
830             int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
831             min_log2_align = FFMIN(log2_align, min_log2_align);
832         }
833
834         /* we assume, and it should always be true, that the data alignment is
835          * related to the cropping alignment by a constant power-of-2 factor */
836         if (log2_crop_align < min_log2_align)
837             return AVERROR_BUG;
838
839         if (min_log2_align < 5) {
840             frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
841             calc_cropping_offsets(offsets, frame, desc);
842         }
843     }
844
845     for (i = 0; frame->data[i]; i++)
846         frame->data[i] += offsets[i];
847
848     frame->width      -= (frame->crop_left + frame->crop_right);
849     frame->height     -= (frame->crop_top  + frame->crop_bottom);
850     frame->crop_left   = 0;
851     frame->crop_right  = 0;
852     frame->crop_top    = 0;
853     frame->crop_bottom = 0;
854
855     return 0;
856 }