]> git.sesse.net Git - ffmpeg/blob - libavutil/frame.c
Merge commit '83aa4fc3feec7389ac781fece1e994f2dfd7ebdb'
[ffmpeg] / libavutil / frame.c
1 /*
2  *
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with FFmpeg; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 #include "channel_layout.h"
21 #include "avassert.h"
22 #include "buffer.h"
23 #include "common.h"
24 #include "dict.h"
25 #include "frame.h"
26 #include "imgutils.h"
27 #include "mem.h"
28 #include "samplefmt.h"
29
30 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
31 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
32 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
33 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
34 MAKE_ACCESSORS(AVFrame, frame, int,     channels)
35 MAKE_ACCESSORS(AVFrame, frame, int,     sample_rate)
36 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
37 MAKE_ACCESSORS(AVFrame, frame, int,     decode_error_flags)
38 MAKE_ACCESSORS(AVFrame, frame, int,     pkt_size)
39 MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
40 MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
41
42 #define CHECK_CHANNELS_CONSISTENCY(frame) \
43     av_assert2(!(frame)->channel_layout || \
44                (frame)->channels == \
45                av_get_channel_layout_nb_channels((frame)->channel_layout))
46
47 AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
48
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     f->qscale_table = buf->data;
56     f->qstride      = stride;
57     f->qscale_type  = qp_type;
58
59     return 0;
60 }
61
62 int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
63 {
64     *stride = f->qstride;
65     *type   = f->qscale_type;
66
67     if (!f->qp_table_buf)
68         return NULL;
69
70     return f->qp_table_buf->data;
71 }
72
73 const char *av_get_colorspace_name(enum AVColorSpace val)
74 {
75     static const char *name[] = {
76         [AVCOL_SPC_RGB]       = "GBR",
77         [AVCOL_SPC_BT709]     = "bt709",
78         [AVCOL_SPC_FCC]       = "fcc",
79         [AVCOL_SPC_BT470BG]   = "bt470bg",
80         [AVCOL_SPC_SMPTE170M] = "smpte170m",
81         [AVCOL_SPC_SMPTE240M] = "smpte240m",
82         [AVCOL_SPC_YCOCG]     = "YCgCo",
83     };
84     if ((unsigned)val >= FF_ARRAY_ELEMS(name))
85         return NULL;
86     return name[val];
87 }
88
89 static void get_frame_defaults(AVFrame *frame)
90 {
91     if (frame->extended_data != frame->data)
92         av_freep(&frame->extended_data);
93
94     memset(frame, 0, sizeof(*frame));
95
96     frame->pts                   =
97     frame->pkt_dts               =
98     frame->pkt_pts               = AV_NOPTS_VALUE;
99     av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
100     av_frame_set_pkt_duration         (frame, 0);
101     av_frame_set_pkt_pos              (frame, -1);
102     av_frame_set_pkt_size             (frame, -1);
103     frame->key_frame           = 1;
104     frame->sample_aspect_ratio = (AVRational){ 0, 1 };
105     frame->format              = -1; /* unknown */
106     frame->extended_data       = frame->data;
107 #if FF_API_AVFRAME_COLORSPACE
108     frame->color_primaries     = AVCOL_PRI_UNSPECIFIED;
109     frame->color_trc           = AVCOL_TRC_UNSPECIFIED;
110     frame->colorspace          = AVCOL_SPC_UNSPECIFIED;
111     frame->color_range         = AVCOL_RANGE_UNSPECIFIED;
112     frame->chroma_location     = AVCHROMA_LOC_UNSPECIFIED;
113 #endif
114 }
115
116 AVFrame *av_frame_alloc(void)
117 {
118     AVFrame *frame = av_mallocz(sizeof(*frame));
119
120     if (!frame)
121         return NULL;
122
123     frame->extended_data = NULL;
124     get_frame_defaults(frame);
125
126     return frame;
127 }
128
129 void av_frame_free(AVFrame **frame)
130 {
131     if (!frame || !*frame)
132         return;
133
134     av_frame_unref(*frame);
135     av_freep(frame);
136 }
137
138 static int get_video_buffer(AVFrame *frame, int align)
139 {
140     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
141     int ret, i;
142
143     if (!desc)
144         return AVERROR(EINVAL);
145
146     if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
147         return ret;
148
149     if (!frame->linesize[0]) {
150         for(i=1; i<=align; i+=i) {
151             ret = av_image_fill_linesizes(frame->linesize, frame->format,
152                                           FFALIGN(frame->width, i));
153             if (ret < 0)
154                 return ret;
155             if (!(frame->linesize[0] & (align-1)))
156                 break;
157         }
158
159         for (i = 0; i < 4 && frame->linesize[i]; i++)
160             frame->linesize[i] = FFALIGN(frame->linesize[i], align);
161     }
162
163     for (i = 0; i < 4 && frame->linesize[i]; i++) {
164         int h = FFALIGN(frame->height, 32);
165         if (i == 1 || i == 2)
166             h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
167
168         frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
169         if (!frame->buf[i])
170             goto fail;
171
172         frame->data[i] = frame->buf[i]->data;
173     }
174     if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
175         av_buffer_unref(&frame->buf[1]);
176         frame->buf[1] = av_buffer_alloc(1024);
177         if (!frame->buf[1])
178             goto fail;
179         frame->data[1] = frame->buf[1]->data;
180     }
181
182     frame->extended_data = frame->data;
183
184     return 0;
185 fail:
186     av_frame_unref(frame);
187     return AVERROR(ENOMEM);
188 }
189
190 static int get_audio_buffer(AVFrame *frame, int align)
191 {
192     int channels;
193     int planar   = av_sample_fmt_is_planar(frame->format);
194     int planes;
195     int ret, i;
196
197     if (!frame->channels)
198         frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
199
200     channels = frame->channels;
201     planes = planar ? channels : 1;
202
203     CHECK_CHANNELS_CONSISTENCY(frame);
204     if (!frame->linesize[0]) {
205         ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
206                                          frame->nb_samples, frame->format,
207                                          align);
208         if (ret < 0)
209             return ret;
210     }
211
212     if (planes > AV_NUM_DATA_POINTERS) {
213         frame->extended_data = av_mallocz_array(planes,
214                                           sizeof(*frame->extended_data));
215         frame->extended_buf  = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
216                                           sizeof(*frame->extended_buf));
217         if (!frame->extended_data || !frame->extended_buf) {
218             av_freep(&frame->extended_data);
219             av_freep(&frame->extended_buf);
220             return AVERROR(ENOMEM);
221         }
222         frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
223     } else
224         frame->extended_data = frame->data;
225
226     for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
227         frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
228         if (!frame->buf[i]) {
229             av_frame_unref(frame);
230             return AVERROR(ENOMEM);
231         }
232         frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
233     }
234     for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
235         frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
236         if (!frame->extended_buf[i]) {
237             av_frame_unref(frame);
238             return AVERROR(ENOMEM);
239         }
240         frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
241     }
242     return 0;
243
244 }
245
246 int av_frame_get_buffer(AVFrame *frame, int align)
247 {
248     if (frame->format < 0)
249         return AVERROR(EINVAL);
250
251     if (frame->width > 0 && frame->height > 0)
252         return get_video_buffer(frame, align);
253     else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
254         return get_audio_buffer(frame, align);
255
256     return AVERROR(EINVAL);
257 }
258
259 int av_frame_ref(AVFrame *dst, const AVFrame *src)
260 {
261     int i, ret = 0;
262
263     dst->format         = src->format;
264     dst->width          = src->width;
265     dst->height         = src->height;
266     dst->channels       = src->channels;
267     dst->channel_layout = src->channel_layout;
268     dst->nb_samples     = src->nb_samples;
269
270     ret = av_frame_copy_props(dst, src);
271     if (ret < 0)
272         return ret;
273
274     /* duplicate the frame data if it's not refcounted */
275     if (!src->buf[0]) {
276         ret = av_frame_get_buffer(dst, 32);
277         if (ret < 0)
278             return ret;
279
280         ret = av_frame_copy(dst, src);
281         if (ret < 0)
282             av_frame_unref(dst);
283
284         return ret;
285     }
286
287     /* ref the buffers */
288     for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
289         if (!src->buf[i])
290             continue;
291         dst->buf[i] = av_buffer_ref(src->buf[i]);
292         if (!dst->buf[i]) {
293             ret = AVERROR(ENOMEM);
294             goto fail;
295         }
296     }
297
298     if (src->extended_buf) {
299         dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
300                                        src->nb_extended_buf);
301         if (!dst->extended_buf) {
302             ret = AVERROR(ENOMEM);
303             goto fail;
304         }
305         dst->nb_extended_buf = src->nb_extended_buf;
306
307         for (i = 0; i < src->nb_extended_buf; i++) {
308             dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
309             if (!dst->extended_buf[i]) {
310                 ret = AVERROR(ENOMEM);
311                 goto fail;
312             }
313         }
314     }
315
316     /* duplicate extended data */
317     if (src->extended_data != src->data) {
318         int ch = src->channels;
319
320         if (!ch) {
321             ret = AVERROR(EINVAL);
322             goto fail;
323         }
324         CHECK_CHANNELS_CONSISTENCY(src);
325
326         dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
327         if (!dst->extended_data) {
328             ret = AVERROR(ENOMEM);
329             goto fail;
330         }
331         memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
332     } else
333         dst->extended_data = dst->data;
334
335     memcpy(dst->data,     src->data,     sizeof(src->data));
336     memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
337
338     return 0;
339
340 fail:
341     av_frame_unref(dst);
342     return ret;
343 }
344
345 AVFrame *av_frame_clone(const AVFrame *src)
346 {
347     AVFrame *ret = av_frame_alloc();
348
349     if (!ret)
350         return NULL;
351
352     if (av_frame_ref(ret, src) < 0)
353         av_frame_free(&ret);
354
355     return ret;
356 }
357
358 void av_frame_unref(AVFrame *frame)
359 {
360     int i;
361
362     for (i = 0; i < frame->nb_side_data; i++) {
363         av_freep(&frame->side_data[i]->data);
364         av_dict_free(&frame->side_data[i]->metadata);
365         av_freep(&frame->side_data[i]);
366     }
367     av_freep(&frame->side_data);
368
369     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
370         av_buffer_unref(&frame->buf[i]);
371     for (i = 0; i < frame->nb_extended_buf; i++)
372         av_buffer_unref(&frame->extended_buf[i]);
373     av_freep(&frame->extended_buf);
374     av_dict_free(&frame->metadata);
375     av_buffer_unref(&frame->qp_table_buf);
376
377     get_frame_defaults(frame);
378 }
379
380 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
381 {
382     *dst = *src;
383     if (src->extended_data == src->data)
384         dst->extended_data = dst->data;
385     memset(src, 0, sizeof(*src));
386     get_frame_defaults(src);
387 }
388
389 int av_frame_is_writable(AVFrame *frame)
390 {
391     int i, ret = 1;
392
393     /* assume non-refcounted frames are not writable */
394     if (!frame->buf[0])
395         return 0;
396
397     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
398         if (frame->buf[i])
399             ret &= !!av_buffer_is_writable(frame->buf[i]);
400     for (i = 0; i < frame->nb_extended_buf; i++)
401         ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
402
403     return ret;
404 }
405
406 int av_frame_make_writable(AVFrame *frame)
407 {
408     AVFrame tmp;
409     int ret;
410
411     if (!frame->buf[0])
412         return AVERROR(EINVAL);
413
414     if (av_frame_is_writable(frame))
415         return 0;
416
417     memset(&tmp, 0, sizeof(tmp));
418     tmp.format         = frame->format;
419     tmp.width          = frame->width;
420     tmp.height         = frame->height;
421     tmp.channels       = frame->channels;
422     tmp.channel_layout = frame->channel_layout;
423     tmp.nb_samples     = frame->nb_samples;
424     ret = av_frame_get_buffer(&tmp, 32);
425     if (ret < 0)
426         return ret;
427
428     ret = av_frame_copy(&tmp, frame);
429     if (ret < 0) {
430         av_frame_unref(&tmp);
431         return ret;
432     }
433
434     ret = av_frame_copy_props(&tmp, frame);
435     if (ret < 0) {
436         av_frame_unref(&tmp);
437         return ret;
438     }
439
440     av_frame_unref(frame);
441
442     *frame = tmp;
443     if (tmp.data == tmp.extended_data)
444         frame->extended_data = frame->data;
445
446     return 0;
447 }
448
449 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
450 {
451     int i;
452
453     dst->key_frame              = src->key_frame;
454     dst->pict_type              = src->pict_type;
455     dst->sample_aspect_ratio    = src->sample_aspect_ratio;
456     dst->pts                    = src->pts;
457     dst->repeat_pict            = src->repeat_pict;
458     dst->interlaced_frame       = src->interlaced_frame;
459     dst->top_field_first        = src->top_field_first;
460     dst->palette_has_changed    = src->palette_has_changed;
461     dst->sample_rate            = src->sample_rate;
462     dst->opaque                 = src->opaque;
463 #if FF_API_AVFRAME_LAVC
464     dst->type                   = src->type;
465 #endif
466     dst->pkt_pts                = src->pkt_pts;
467     dst->pkt_dts                = src->pkt_dts;
468     dst->pkt_pos                = src->pkt_pos;
469     dst->pkt_size               = src->pkt_size;
470     dst->pkt_duration           = src->pkt_duration;
471     dst->reordered_opaque       = src->reordered_opaque;
472     dst->quality                = src->quality;
473     dst->best_effort_timestamp  = src->best_effort_timestamp;
474     dst->coded_picture_number   = src->coded_picture_number;
475     dst->display_picture_number = src->display_picture_number;
476     dst->flags                  = src->flags;
477     dst->decode_error_flags     = src->decode_error_flags;
478 #if FF_API_AVFRAME_COLORSPACE
479     dst->color_primaries        = src->color_primaries;
480     dst->color_trc              = src->color_trc;
481     dst->colorspace             = src->colorspace;
482     dst->color_range            = src->color_range;
483     dst->chroma_location        = src->chroma_location;
484 #endif
485
486     av_dict_copy(&dst->metadata, src->metadata, 0);
487
488     memcpy(dst->error, src->error, sizeof(dst->error));
489
490     for (i = 0; i < src->nb_side_data; i++) {
491         const AVFrameSideData *sd_src = src->side_data[i];
492         AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
493                                                          sd_src->size);
494         if (!sd_dst) {
495             for (i = 0; i < dst->nb_side_data; i++) {
496                 av_freep(&dst->side_data[i]->data);
497                 av_dict_free(&dst->side_data[i]->metadata);
498                 av_freep(&dst->side_data[i]);
499             }
500             av_freep(&dst->side_data);
501             return AVERROR(ENOMEM);
502         }
503         memcpy(sd_dst->data, sd_src->data, sd_src->size);
504         av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
505     }
506
507     dst->qscale_table = NULL;
508     dst->qstride      = 0;
509     dst->qscale_type  = 0;
510     if (src->qp_table_buf) {
511         dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
512         if (dst->qp_table_buf) {
513             dst->qscale_table = dst->qp_table_buf->data;
514             dst->qstride      = src->qstride;
515             dst->qscale_type  = src->qscale_type;
516         }
517     }
518
519     return 0;
520 }
521
522 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
523 {
524     uint8_t *data;
525     int planes, i;
526
527     if (frame->nb_samples) {
528         int channels = frame->channels;
529         if (!channels)
530             return NULL;
531         CHECK_CHANNELS_CONSISTENCY(frame);
532         planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
533     } else
534         planes = 4;
535
536     if (plane < 0 || plane >= planes || !frame->extended_data[plane])
537         return NULL;
538     data = frame->extended_data[plane];
539
540     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
541         AVBufferRef *buf = frame->buf[i];
542         if (data >= buf->data && data < buf->data + buf->size)
543             return buf;
544     }
545     for (i = 0; i < frame->nb_extended_buf; i++) {
546         AVBufferRef *buf = frame->extended_buf[i];
547         if (data >= buf->data && data < buf->data + buf->size)
548             return buf;
549     }
550     return NULL;
551 }
552
553 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
554                                         enum AVFrameSideDataType type,
555                                         int size)
556 {
557     AVFrameSideData *ret, **tmp;
558
559     if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
560         return NULL;
561
562     tmp = av_realloc(frame->side_data,
563                      (frame->nb_side_data + 1) * sizeof(*frame->side_data));
564     if (!tmp)
565         return NULL;
566     frame->side_data = tmp;
567
568     ret = av_mallocz(sizeof(*ret));
569     if (!ret)
570         return NULL;
571
572     ret->data = av_malloc(size);
573     if (!ret->data) {
574         av_freep(&ret);
575         return NULL;
576     }
577
578     ret->size = size;
579     ret->type = type;
580
581     frame->side_data[frame->nb_side_data++] = ret;
582
583     return ret;
584 }
585
586 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
587                                         enum AVFrameSideDataType type)
588 {
589     int i;
590
591     for (i = 0; i < frame->nb_side_data; i++) {
592         if (frame->side_data[i]->type == type)
593             return frame->side_data[i];
594     }
595     return NULL;
596 }
597
598 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
599 {
600     const uint8_t *src_data[4];
601     int i, planes;
602
603     if (dst->width  < src->width ||
604         dst->height < src->height)
605         return AVERROR(EINVAL);
606
607     planes = av_pix_fmt_count_planes(dst->format);
608     for (i = 0; i < planes; i++)
609         if (!dst->data[i] || !src->data[i])
610             return AVERROR(EINVAL);
611
612     memcpy(src_data, src->data, sizeof(src_data));
613     av_image_copy(dst->data, dst->linesize,
614                   src_data, src->linesize,
615                   dst->format, src->width, src->height);
616
617     return 0;
618 }
619
620 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
621 {
622     int planar   = av_sample_fmt_is_planar(dst->format);
623     int channels = dst->channels;
624     int planes   = planar ? channels : 1;
625     int i;
626
627     if (dst->nb_samples     != src->nb_samples ||
628         dst->channels       != src->channels ||
629         dst->channel_layout != src->channel_layout)
630         return AVERROR(EINVAL);
631
632     CHECK_CHANNELS_CONSISTENCY(src);
633
634     for (i = 0; i < planes; i++)
635         if (!dst->extended_data[i] || !src->extended_data[i])
636             return AVERROR(EINVAL);
637
638     av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
639                     dst->nb_samples, channels, dst->format);
640
641     return 0;
642 }
643
644 int av_frame_copy(AVFrame *dst, const AVFrame *src)
645 {
646     if (dst->format != src->format || dst->format < 0)
647         return AVERROR(EINVAL);
648
649     if (dst->width > 0 && dst->height > 0)
650         return frame_copy_video(dst, src);
651     else if (dst->nb_samples > 0 && dst->channel_layout)
652         return frame_copy_audio(dst, src);
653
654     return AVERROR(EINVAL);
655 }
656
657 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
658 {
659     int i;
660
661     for (i = 0; i < frame->nb_side_data; i++) {
662         AVFrameSideData *sd = frame->side_data[i];
663         if (sd->type == type) {
664             av_freep(&sd->data);
665             av_dict_free(&sd->metadata);
666             av_freep(&frame->side_data[i]);
667             frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
668             frame->nb_side_data--;
669         }
670     }
671 }