AVFrame: add an opaque_ref field
[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
04f3bd34 320 av_buffer_unref(&frame->opaque_ref);
321
7ecc2d40
AK
322 get_frame_defaults(frame);
323}
324
325void av_frame_move_ref(AVFrame *dst, AVFrame *src)
326{
327 *dst = *src;
328 if (src->extended_data == src->data)
329 dst->extended_data = dst->data;
330 memset(src, 0, sizeof(*src));
331 get_frame_defaults(src);
332}
333
334int av_frame_is_writable(AVFrame *frame)
335{
336 int i, ret = 1;
337
338 /* assume non-refcounted frames are not writable */
339 if (!frame->buf[0])
340 return 0;
341
342 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
343 ret &= !!av_buffer_is_writable(frame->buf[i]);
344 for (i = 0; i < frame->nb_extended_buf; i++)
345 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
346
347 return ret;
348}
349
350int av_frame_make_writable(AVFrame *frame)
351{
352 AVFrame tmp;
353 int ret;
354
355 if (!frame->buf[0])
356 return AVERROR(EINVAL);
357
358 if (av_frame_is_writable(frame))
359 return 0;
360
361 memset(&tmp, 0, sizeof(tmp));
362 tmp.format = frame->format;
363 tmp.width = frame->width;
364 tmp.height = frame->height;
365 tmp.channel_layout = frame->channel_layout;
366 tmp.nb_samples = frame->nb_samples;
367 ret = av_frame_get_buffer(&tmp, 32);
368 if (ret < 0)
369 return ret;
370
1155fd02
AK
371 ret = av_frame_copy(&tmp, frame);
372 if (ret < 0) {
373 av_frame_unref(&tmp);
374 return ret;
7ecc2d40
AK
375 }
376
377 ret = av_frame_copy_props(&tmp, frame);
378 if (ret < 0) {
379 av_frame_unref(&tmp);
380 return ret;
381 }
382
383 av_frame_unref(frame);
384
385 *frame = tmp;
386 if (tmp.data == tmp.extended_data)
387 frame->extended_data = frame->data;
388
389 return 0;
390}
391
392int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
393{
77b2cd7b
AK
394 int i;
395
df7aba52
AK
396 dst->key_frame = src->key_frame;
397 dst->pict_type = src->pict_type;
398 dst->sample_aspect_ratio = src->sample_aspect_ratio;
52627248
AK
399 dst->crop_top = src->crop_top;
400 dst->crop_bottom = src->crop_bottom;
401 dst->crop_left = src->crop_left;
402 dst->crop_right = src->crop_right;
df7aba52
AK
403 dst->pts = src->pts;
404 dst->repeat_pict = src->repeat_pict;
405 dst->interlaced_frame = src->interlaced_frame;
406 dst->top_field_first = src->top_field_first;
407 dst->palette_has_changed = src->palette_has_changed;
408 dst->sample_rate = src->sample_rate;
409 dst->opaque = src->opaque;
32c83590
AK
410#if FF_API_PKT_PTS
411FF_DISABLE_DEPRECATION_WARNINGS
df7aba52 412 dst->pkt_pts = src->pkt_pts;
32c83590
AK
413FF_ENABLE_DEPRECATION_WARNINGS
414#endif
df7aba52
AK
415 dst->pkt_dts = src->pkt_dts;
416 dst->reordered_opaque = src->reordered_opaque;
417 dst->quality = src->quality;
418 dst->coded_picture_number = src->coded_picture_number;
7ecc2d40 419 dst->display_picture_number = src->display_picture_number;
c342132f 420 dst->flags = src->flags;
8c02adc6 421 dst->color_primaries = src->color_primaries;
422 dst->color_trc = src->color_trc;
423 dst->colorspace = src->colorspace;
424 dst->color_range = src->color_range;
425 dst->chroma_location = src->chroma_location;
7ecc2d40 426
1aa24df7
VG
427#if FF_API_ERROR_FRAME
428FF_DISABLE_DEPRECATION_WARNINGS
1e8b9738 429 memcpy(dst->error, src->error, sizeof(dst->error));
1aa24df7
VG
430FF_ENABLE_DEPRECATION_WARNINGS
431#endif
1e8b9738 432
77b2cd7b
AK
433 for (i = 0; i < src->nb_side_data; i++) {
434 const AVFrameSideData *sd_src = src->side_data[i];
435 AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
436 sd_src->size);
437 if (!sd_dst) {
5d839778 438 wipe_side_data(dst);
77b2cd7b
AK
439 return AVERROR(ENOMEM);
440 }
441 memcpy(sd_dst->data, sd_src->data, sd_src->size);
442 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
443 }
444
04f3bd34 445 av_buffer_unref(&dst->opaque_ref);
446 if (src->opaque_ref) {
447 dst->opaque_ref = av_buffer_ref(src->opaque_ref);
448 if (!dst->opaque_ref)
449 return AVERROR(ENOMEM);
450 }
451
7ecc2d40
AK
452 return 0;
453}
454
455AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
456{
457 uint8_t *data;
458 int planes, i;
459
460 if (frame->nb_samples) {
461 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
462 if (!channels)
463 return NULL;
464 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
465 } else
466 planes = 4;
467
468 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
469 return NULL;
470 data = frame->extended_data[plane];
471
472 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
473 AVBufferRef *buf = frame->buf[i];
474 if (data >= buf->data && data < buf->data + buf->size)
475 return buf;
476 }
477 for (i = 0; i < frame->nb_extended_buf; i++) {
478 AVBufferRef *buf = frame->extended_buf[i];
479 if (data >= buf->data && data < buf->data + buf->size)
480 return buf;
481 }
482 return NULL;
483}
77b2cd7b
AK
484
485AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
486 enum AVFrameSideDataType type,
487 int size)
488{
489 AVFrameSideData *ret, **tmp;
490
491 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
492 return NULL;
493
494 tmp = av_realloc(frame->side_data,
495 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
496 if (!tmp)
497 return NULL;
498 frame->side_data = tmp;
499
500 ret = av_mallocz(sizeof(*ret));
501 if (!ret)
502 return NULL;
503
504 ret->data = av_malloc(size);
505 if (!ret->data) {
506 av_freep(&ret);
507 return NULL;
508 }
509
510 ret->size = size;
511 ret->type = type;
512
513 frame->side_data[frame->nb_side_data++] = ret;
514
515 return ret;
516}
517
3c8bff07 518AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
77b2cd7b
AK
519 enum AVFrameSideDataType type)
520{
521 int i;
522
523 for (i = 0; i < frame->nb_side_data; i++) {
524 if (frame->side_data[i]->type == type)
525 return frame->side_data[i];
526 }
527 return NULL;
528}
1155fd02
AK
529
530static int frame_copy_video(AVFrame *dst, const AVFrame *src)
531{
532 const uint8_t *src_data[4];
533 int i, planes;
534
535 if (dst->width != src->width ||
536 dst->height != src->height)
537 return AVERROR(EINVAL);
538
539 planes = av_pix_fmt_count_planes(dst->format);
540 for (i = 0; i < planes; i++)
541 if (!dst->data[i] || !src->data[i])
542 return AVERROR(EINVAL);
543
544 memcpy(src_data, src->data, sizeof(src_data));
545 av_image_copy(dst->data, dst->linesize,
546 src_data, src->linesize,
547 dst->format, dst->width, dst->height);
548
549 return 0;
550}
551
552static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
553{
554 int planar = av_sample_fmt_is_planar(dst->format);
555 int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
556 int planes = planar ? channels : 1;
557 int i;
558
559 if (dst->nb_samples != src->nb_samples ||
560 dst->channel_layout != src->channel_layout)
561 return AVERROR(EINVAL);
562
563 for (i = 0; i < planes; i++)
564 if (!dst->extended_data[i] || !src->extended_data[i])
565 return AVERROR(EINVAL);
566
567 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
568 dst->nb_samples, channels, dst->format);
569
570 return 0;
571}
572
573int av_frame_copy(AVFrame *dst, const AVFrame *src)
574{
575 if (dst->format != src->format || dst->format < 0)
576 return AVERROR(EINVAL);
577
578 if (dst->width > 0 && dst->height > 0)
579 return frame_copy_video(dst, src);
580 else if (dst->nb_samples > 0 && dst->channel_layout)
581 return frame_copy_audio(dst, src);
582
583 return AVERROR(EINVAL);
584}
d161ae0a
AK
585
586void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
587{
588 int i;
589
590 for (i = 0; i < frame->nb_side_data; i++) {
591 AVFrameSideData *sd = frame->side_data[i];
592 if (sd->type == type) {
44671b57 593 free_side_data(&frame->side_data[i]);
d161ae0a
AK
594 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
595 frame->nb_side_data--;
596 }
597 }
598}