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