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