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