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