]> git.sesse.net Git - ffmpeg/blob - libavutil/frame.c
lavc: export the timestamps when decoding in AVFrame.pts
[ffmpeg] / libavutil / frame.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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 "buffer.h"
21 #include "common.h"
22 #include "dict.h"
23 #include "frame.h"
24 #include "imgutils.h"
25 #include "mem.h"
26 #include "samplefmt.h"
27
28 static void get_frame_defaults(AVFrame *frame)
29 {
30     if (frame->extended_data != frame->data)
31         av_freep(&frame->extended_data);
32
33     memset(frame, 0, sizeof(*frame));
34
35     frame->pts                 = AV_NOPTS_VALUE;
36     frame->key_frame           = 1;
37     frame->sample_aspect_ratio = (AVRational){ 0, 1 };
38     frame->format              = -1; /* unknown */
39     frame->extended_data       = frame->data;
40     frame->color_primaries     = AVCOL_PRI_UNSPECIFIED;
41     frame->color_trc           = AVCOL_TRC_UNSPECIFIED;
42     frame->colorspace          = AVCOL_SPC_UNSPECIFIED;
43     frame->color_range         = AVCOL_RANGE_UNSPECIFIED;
44     frame->chroma_location     = AVCHROMA_LOC_UNSPECIFIED;
45 }
46
47 static void free_side_data(AVFrameSideData **ptr_sd)
48 {
49     AVFrameSideData *sd = *ptr_sd;
50
51     av_freep(&sd->data);
52     av_dict_free(&sd->metadata);
53     av_freep(ptr_sd);
54 }
55
56 static void wipe_side_data(AVFrame *frame)
57 {
58     int i;
59
60     for (i = 0; i < frame->nb_side_data; i++) {
61         free_side_data(&frame->side_data[i]);
62     }
63     frame->nb_side_data = 0;
64
65     av_freep(&frame->side_data);
66 }
67
68 AVFrame *av_frame_alloc(void)
69 {
70     AVFrame *frame = av_mallocz(sizeof(*frame));
71
72     if (!frame)
73         return NULL;
74
75     get_frame_defaults(frame);
76
77     return frame;
78 }
79
80 void av_frame_free(AVFrame **frame)
81 {
82     if (!frame || !*frame)
83         return;
84
85     av_frame_unref(*frame);
86     av_freep(frame);
87 }
88
89 static int get_video_buffer(AVFrame *frame, int align)
90 {
91     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
92     int ret, i;
93
94     if (!desc)
95         return AVERROR(EINVAL);
96
97     if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
98         return ret;
99
100     if (!frame->linesize[0]) {
101         ret = av_image_fill_linesizes(frame->linesize, frame->format,
102                                       frame->width);
103         if (ret < 0)
104             return ret;
105
106         for (i = 0; i < 4 && frame->linesize[i]; i++)
107             frame->linesize[i] = FFALIGN(frame->linesize[i], align);
108     }
109
110     for (i = 0; i < 4 && frame->linesize[i]; i++) {
111         int h = frame->height;
112         if (i == 1 || i == 2)
113             h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
114
115         frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
116         if (!frame->buf[i])
117             goto fail;
118
119         frame->data[i] = frame->buf[i]->data;
120     }
121     if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
122         av_buffer_unref(&frame->buf[1]);
123         frame->buf[1] = av_buffer_alloc(1024);
124         if (!frame->buf[1])
125             goto fail;
126         frame->data[1] = frame->buf[1]->data;
127     }
128
129     frame->extended_data = frame->data;
130
131     return 0;
132 fail:
133     av_frame_unref(frame);
134     return AVERROR(ENOMEM);
135 }
136
137 static int get_audio_buffer(AVFrame *frame, int align)
138 {
139     int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
140     int planar   = av_sample_fmt_is_planar(frame->format);
141     int planes   = planar ? channels : 1;
142     int ret, i;
143
144     if (!frame->linesize[0]) {
145         ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
146                                          frame->nb_samples, frame->format,
147                                          align);
148         if (ret < 0)
149             return ret;
150     }
151
152     if (planes > AV_NUM_DATA_POINTERS) {
153         frame->extended_data = av_mallocz(planes *
154                                           sizeof(*frame->extended_data));
155         frame->extended_buf  = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
156                                           sizeof(*frame->extended_buf));
157         if (!frame->extended_data || !frame->extended_buf) {
158             av_freep(&frame->extended_data);
159             av_freep(&frame->extended_buf);
160             return AVERROR(ENOMEM);
161         }
162         frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
163     } else
164         frame->extended_data = frame->data;
165
166     for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
167         frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
168         if (!frame->buf[i]) {
169             av_frame_unref(frame);
170             return AVERROR(ENOMEM);
171         }
172         frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
173     }
174     for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
175         frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
176         if (!frame->extended_buf[i]) {
177             av_frame_unref(frame);
178             return AVERROR(ENOMEM);
179         }
180         frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
181     }
182     return 0;
183
184 }
185
186 int av_frame_get_buffer(AVFrame *frame, int align)
187 {
188     if (frame->format < 0)
189         return AVERROR(EINVAL);
190
191     if (frame->width > 0 && frame->height > 0)
192         return get_video_buffer(frame, align);
193     else if (frame->nb_samples > 0 && frame->channel_layout)
194         return get_audio_buffer(frame, align);
195
196     return AVERROR(EINVAL);
197 }
198
199 int av_frame_ref(AVFrame *dst, const AVFrame *src)
200 {
201     int i, ret = 0;
202
203     dst->format         = src->format;
204     dst->width          = src->width;
205     dst->height         = src->height;
206     dst->channel_layout = src->channel_layout;
207     dst->nb_samples     = src->nb_samples;
208
209     ret = av_frame_copy_props(dst, src);
210     if (ret < 0)
211         return ret;
212
213     /* duplicate the frame data if it's not refcounted */
214     if (!src->buf[0]) {
215         ret = av_frame_get_buffer(dst, 32);
216         if (ret < 0)
217             return ret;
218
219         ret = av_frame_copy(dst, src);
220         if (ret < 0)
221             av_frame_unref(dst);
222
223         return ret;
224     }
225
226     /* ref the buffers */
227     for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
228         dst->buf[i] = av_buffer_ref(src->buf[i]);
229         if (!dst->buf[i]) {
230             ret = AVERROR(ENOMEM);
231             goto fail;
232         }
233     }
234
235     if (src->extended_buf) {
236         dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
237                                        src->nb_extended_buf);
238         if (!dst->extended_buf) {
239             ret = AVERROR(ENOMEM);
240             goto fail;
241         }
242         dst->nb_extended_buf = src->nb_extended_buf;
243
244         for (i = 0; i < src->nb_extended_buf; i++) {
245             dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
246             if (!dst->extended_buf[i]) {
247                 ret = AVERROR(ENOMEM);
248                 goto fail;
249             }
250         }
251     }
252
253     if (src->hw_frames_ctx) {
254         dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
255         if (!dst->hw_frames_ctx) {
256             ret = AVERROR(ENOMEM);
257             goto fail;
258         }
259     }
260
261     /* duplicate extended data */
262     if (src->extended_data != src->data) {
263         int ch = av_get_channel_layout_nb_channels(src->channel_layout);
264
265         if (!ch) {
266             ret = AVERROR(EINVAL);
267             goto fail;
268         }
269
270         dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
271         if (!dst->extended_data) {
272             ret = AVERROR(ENOMEM);
273             goto fail;
274         }
275         memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
276     } else
277         dst->extended_data = dst->data;
278
279     memcpy(dst->data,     src->data,     sizeof(src->data));
280     memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
281
282     return 0;
283
284 fail:
285     av_frame_unref(dst);
286     return ret;
287 }
288
289 AVFrame *av_frame_clone(const AVFrame *src)
290 {
291     AVFrame *ret = av_frame_alloc();
292
293     if (!ret)
294         return NULL;
295
296     if (av_frame_ref(ret, src) < 0)
297         av_frame_free(&ret);
298
299     return ret;
300 }
301
302 void av_frame_unref(AVFrame *frame)
303 {
304     int i;
305
306     wipe_side_data(frame);
307
308     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
309         av_buffer_unref(&frame->buf[i]);
310     for (i = 0; i < frame->nb_extended_buf; i++)
311         av_buffer_unref(&frame->extended_buf[i]);
312     av_freep(&frame->extended_buf);
313
314     av_buffer_unref(&frame->hw_frames_ctx);
315
316     get_frame_defaults(frame);
317 }
318
319 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
320 {
321     *dst = *src;
322     if (src->extended_data == src->data)
323         dst->extended_data = dst->data;
324     memset(src, 0, sizeof(*src));
325     get_frame_defaults(src);
326 }
327
328 int av_frame_is_writable(AVFrame *frame)
329 {
330     int i, ret = 1;
331
332     /* assume non-refcounted frames are not writable */
333     if (!frame->buf[0])
334         return 0;
335
336     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
337         ret &= !!av_buffer_is_writable(frame->buf[i]);
338     for (i = 0; i < frame->nb_extended_buf; i++)
339         ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
340
341     return ret;
342 }
343
344 int av_frame_make_writable(AVFrame *frame)
345 {
346     AVFrame tmp;
347     int ret;
348
349     if (!frame->buf[0])
350         return AVERROR(EINVAL);
351
352     if (av_frame_is_writable(frame))
353         return 0;
354
355     memset(&tmp, 0, sizeof(tmp));
356     tmp.format         = frame->format;
357     tmp.width          = frame->width;
358     tmp.height         = frame->height;
359     tmp.channel_layout = frame->channel_layout;
360     tmp.nb_samples     = frame->nb_samples;
361     ret = av_frame_get_buffer(&tmp, 32);
362     if (ret < 0)
363         return ret;
364
365     ret = av_frame_copy(&tmp, frame);
366     if (ret < 0) {
367         av_frame_unref(&tmp);
368         return ret;
369     }
370
371     ret = av_frame_copy_props(&tmp, frame);
372     if (ret < 0) {
373         av_frame_unref(&tmp);
374         return ret;
375     }
376
377     av_frame_unref(frame);
378
379     *frame = tmp;
380     if (tmp.data == tmp.extended_data)
381         frame->extended_data = frame->data;
382
383     return 0;
384 }
385
386 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
387 {
388     int i;
389
390     dst->key_frame              = src->key_frame;
391     dst->pict_type              = src->pict_type;
392     dst->sample_aspect_ratio    = src->sample_aspect_ratio;
393     dst->pts                    = src->pts;
394     dst->repeat_pict            = src->repeat_pict;
395     dst->interlaced_frame       = src->interlaced_frame;
396     dst->top_field_first        = src->top_field_first;
397     dst->palette_has_changed    = src->palette_has_changed;
398     dst->sample_rate            = src->sample_rate;
399     dst->opaque                 = src->opaque;
400 #if FF_API_PKT_PTS
401 FF_DISABLE_DEPRECATION_WARNINGS
402     dst->pkt_pts                = src->pkt_pts;
403 FF_ENABLE_DEPRECATION_WARNINGS
404 #endif
405     dst->pkt_dts                = src->pkt_dts;
406     dst->reordered_opaque       = src->reordered_opaque;
407     dst->quality                = src->quality;
408     dst->coded_picture_number   = src->coded_picture_number;
409     dst->display_picture_number = src->display_picture_number;
410     dst->flags                  = src->flags;
411     dst->color_primaries        = src->color_primaries;
412     dst->color_trc              = src->color_trc;
413     dst->colorspace             = src->colorspace;
414     dst->color_range            = src->color_range;
415     dst->chroma_location        = src->chroma_location;
416
417 #if FF_API_ERROR_FRAME
418 FF_DISABLE_DEPRECATION_WARNINGS
419     memcpy(dst->error, src->error, sizeof(dst->error));
420 FF_ENABLE_DEPRECATION_WARNINGS
421 #endif
422
423     for (i = 0; i < src->nb_side_data; i++) {
424         const AVFrameSideData *sd_src = src->side_data[i];
425         AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
426                                                          sd_src->size);
427         if (!sd_dst) {
428             wipe_side_data(dst);
429             return AVERROR(ENOMEM);
430         }
431         memcpy(sd_dst->data, sd_src->data, sd_src->size);
432         av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
433     }
434
435     return 0;
436 }
437
438 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
439 {
440     uint8_t *data;
441     int planes, i;
442
443     if (frame->nb_samples) {
444         int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
445         if (!channels)
446             return NULL;
447         planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
448     } else
449         planes = 4;
450
451     if (plane < 0 || plane >= planes || !frame->extended_data[plane])
452         return NULL;
453     data = frame->extended_data[plane];
454
455     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
456         AVBufferRef *buf = frame->buf[i];
457         if (data >= buf->data && data < buf->data + buf->size)
458             return buf;
459     }
460     for (i = 0; i < frame->nb_extended_buf; i++) {
461         AVBufferRef *buf = frame->extended_buf[i];
462         if (data >= buf->data && data < buf->data + buf->size)
463             return buf;
464     }
465     return NULL;
466 }
467
468 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
469                                         enum AVFrameSideDataType type,
470                                         int size)
471 {
472     AVFrameSideData *ret, **tmp;
473
474     if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
475         return NULL;
476
477     tmp = av_realloc(frame->side_data,
478                      (frame->nb_side_data + 1) * sizeof(*frame->side_data));
479     if (!tmp)
480         return NULL;
481     frame->side_data = tmp;
482
483     ret = av_mallocz(sizeof(*ret));
484     if (!ret)
485         return NULL;
486
487     ret->data = av_malloc(size);
488     if (!ret->data) {
489         av_freep(&ret);
490         return NULL;
491     }
492
493     ret->size = size;
494     ret->type = type;
495
496     frame->side_data[frame->nb_side_data++] = ret;
497
498     return ret;
499 }
500
501 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
502                                         enum AVFrameSideDataType type)
503 {
504     int i;
505
506     for (i = 0; i < frame->nb_side_data; i++) {
507         if (frame->side_data[i]->type == type)
508             return frame->side_data[i];
509     }
510     return NULL;
511 }
512
513 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
514 {
515     const uint8_t *src_data[4];
516     int i, planes;
517
518     if (dst->width  != src->width ||
519         dst->height != src->height)
520         return AVERROR(EINVAL);
521
522     planes = av_pix_fmt_count_planes(dst->format);
523     for (i = 0; i < planes; i++)
524         if (!dst->data[i] || !src->data[i])
525             return AVERROR(EINVAL);
526
527     memcpy(src_data, src->data, sizeof(src_data));
528     av_image_copy(dst->data, dst->linesize,
529                   src_data, src->linesize,
530                   dst->format, dst->width, dst->height);
531
532     return 0;
533 }
534
535 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
536 {
537     int planar   = av_sample_fmt_is_planar(dst->format);
538     int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
539     int planes   = planar ? channels : 1;
540     int i;
541
542     if (dst->nb_samples     != src->nb_samples ||
543         dst->channel_layout != src->channel_layout)
544         return AVERROR(EINVAL);
545
546     for (i = 0; i < planes; i++)
547         if (!dst->extended_data[i] || !src->extended_data[i])
548             return AVERROR(EINVAL);
549
550     av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
551                     dst->nb_samples, channels, dst->format);
552
553     return 0;
554 }
555
556 int av_frame_copy(AVFrame *dst, const AVFrame *src)
557 {
558     if (dst->format != src->format || dst->format < 0)
559         return AVERROR(EINVAL);
560
561     if (dst->width > 0 && dst->height > 0)
562         return frame_copy_video(dst, src);
563     else if (dst->nb_samples > 0 && dst->channel_layout)
564         return frame_copy_audio(dst, src);
565
566     return AVERROR(EINVAL);
567 }
568
569 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
570 {
571     int i;
572
573     for (i = 0; i < frame->nb_side_data; i++) {
574         AVFrameSideData *sd = frame->side_data[i];
575         if (sd->type == type) {
576             free_side_data(&frame->side_data[i]);
577             frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
578             frame->nb_side_data--;
579         }
580     }
581 }