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