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