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