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