decode: copy the output parameters from the last bsf in the chain back to the AVCodec...
[libav.git] / libavcodec / decode.c
CommitLineData
3fe2a01d
AK
1/*
2 * generic decoding-related code
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdint.h>
22#include <string.h>
23
24#include "config.h"
25
26#include "libavutil/avassert.h"
061a0c14 27#include "libavutil/avstring.h"
3fe2a01d
AK
28#include "libavutil/common.h"
29#include "libavutil/frame.h"
30#include "libavutil/hwcontext.h"
31#include "libavutil/imgutils.h"
019ab88a 32#include "libavutil/intmath.h"
3fe2a01d
AK
33
34#include "avcodec.h"
35#include "bytestream.h"
061a0c14 36#include "decode.h"
39056b52 37#include "hwaccel.h"
3fe2a01d
AK
38#include "internal.h"
39#include "thread.h"
40
41static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
42{
43 int size = 0, ret;
44 const uint8_t *data;
45 uint32_t flags;
46
47 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
48 if (!data)
49 return 0;
50
51 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
52 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
53 "changes, but PARAM_CHANGE side data was sent to it.\n");
54 ret = AVERROR(EINVAL);
55 goto fail2;
56 }
57
58 if (size < 4)
59 goto fail;
60
61 flags = bytestream_get_le32(&data);
62 size -= 4;
63
64 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
65 if (size < 4)
66 goto fail;
67 avctx->channels = bytestream_get_le32(&data);
68 size -= 4;
69 }
70 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
71 if (size < 8)
72 goto fail;
73 avctx->channel_layout = bytestream_get_le64(&data);
74 size -= 8;
75 }
76 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
77 if (size < 4)
78 goto fail;
79 avctx->sample_rate = bytestream_get_le32(&data);
80 size -= 4;
81 }
82 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
83 if (size < 8)
84 goto fail;
85 avctx->width = bytestream_get_le32(&data);
86 avctx->height = bytestream_get_le32(&data);
87 size -= 8;
88 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
89 if (ret < 0)
90 goto fail2;
91 }
92
93 return 0;
94fail:
95 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
96 ret = AVERROR_INVALIDDATA;
97fail2:
98 if (ret < 0) {
99 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
100 if (avctx->err_recognition & AV_EF_EXPLODE)
101 return ret;
102 }
103 return 0;
104}
105
549d0bdc
AK
106static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
107{
108 av_packet_unref(avci->last_pkt_props);
109 if (pkt)
110 return av_packet_copy_props(avci->last_pkt_props, pkt);
111 return 0;
112}
113
3fe2a01d
AK
114static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
115{
116 int ret;
117
118 /* move the original frame to our backup */
119 av_frame_unref(avci->to_free);
120 av_frame_move_ref(avci->to_free, frame);
121
122 /* now copy everything except the AVBufferRefs back
123 * note that we make a COPY of the side data, so calling av_frame_free() on
124 * the caller's frame will work properly */
125 ret = av_frame_copy_props(frame, avci->to_free);
126 if (ret < 0)
127 return ret;
128
129 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
130 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
131 if (avci->to_free->extended_data != avci->to_free->data) {
132 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
133 int size = planes * sizeof(*frame->extended_data);
134
135 if (!size) {
136 av_frame_unref(frame);
137 return AVERROR_BUG;
138 }
139
140 frame->extended_data = av_malloc(size);
141 if (!frame->extended_data) {
142 av_frame_unref(frame);
143 return AVERROR(ENOMEM);
144 }
145 memcpy(frame->extended_data, avci->to_free->extended_data,
146 size);
147 } else
148 frame->extended_data = frame->data;
149
150 frame->format = avci->to_free->format;
151 frame->width = avci->to_free->width;
152 frame->height = avci->to_free->height;
153 frame->channel_layout = avci->to_free->channel_layout;
154 frame->nb_samples = avci->to_free->nb_samples;
155
156 return 0;
157}
158
ad99cbc9 159int ff_decode_bsfs_init(AVCodecContext *avctx)
972c71e9
AK
160{
161 AVCodecInternal *avci = avctx->internal;
162 DecodeFilterContext *s = &avci->filter;
163 const char *bsfs_str;
164 int ret;
165
166 if (s->nb_bsfs)
167 return 0;
168
169 bsfs_str = avctx->codec->bsfs ? avctx->codec->bsfs : "null";
170 while (bsfs_str && *bsfs_str) {
171 AVBSFContext **tmp;
172 const AVBitStreamFilter *filter;
173 char *bsf;
174
175 bsf = av_get_token(&bsfs_str, ",");
176 if (!bsf) {
177 ret = AVERROR(ENOMEM);
178 goto fail;
179 }
180
181 filter = av_bsf_get_by_name(bsf);
182 if (!filter) {
183 av_log(avctx, AV_LOG_ERROR, "A non-existing bitstream filter %s "
184 "requested by a decoder. This is a bug, please report it.\n",
185 bsf);
186 ret = AVERROR_BUG;
187 av_freep(&bsf);
188 goto fail;
189 }
190 av_freep(&bsf);
191
192 tmp = av_realloc_array(s->bsfs, s->nb_bsfs + 1, sizeof(*s->bsfs));
193 if (!tmp) {
194 ret = AVERROR(ENOMEM);
195 goto fail;
196 }
197 s->bsfs = tmp;
198 s->nb_bsfs++;
199
200 ret = av_bsf_alloc(filter, &s->bsfs[s->nb_bsfs - 1]);
201 if (ret < 0)
202 goto fail;
203
204 if (s->nb_bsfs == 1) {
205 /* We do not currently have an API for passing the input timebase into decoders,
206 * but no filters used here should actually need it.
207 * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
208 s->bsfs[s->nb_bsfs - 1]->time_base_in = (AVRational){ 1, 90000 };
209 ret = avcodec_parameters_from_context(s->bsfs[s->nb_bsfs - 1]->par_in,
210 avctx);
211 } else {
212 s->bsfs[s->nb_bsfs - 1]->time_base_in = s->bsfs[s->nb_bsfs - 2]->time_base_out;
213 ret = avcodec_parameters_copy(s->bsfs[s->nb_bsfs - 1]->par_in,
214 s->bsfs[s->nb_bsfs - 2]->par_out);
215 }
216 if (ret < 0)
217 goto fail;
218
219 ret = av_bsf_init(s->bsfs[s->nb_bsfs - 1]);
220 if (ret < 0)
221 goto fail;
222 }
223
662558f9
JA
224 ret = avcodec_parameters_to_context(avctx, s->bsfs[s->nb_bsfs - 1]->par_out);
225 if (ret < 0)
226 return ret;
227
972c71e9
AK
228 return 0;
229fail:
230 ff_decode_bsfs_uninit(avctx);
231 return ret;
232}
233
234/* try to get one output packet from the filter chain */
235static int bsfs_poll(AVCodecContext *avctx, AVPacket *pkt)
236{
237 DecodeFilterContext *s = &avctx->internal->filter;
238 int idx, ret;
239
240 /* start with the last filter in the chain */
241 idx = s->nb_bsfs - 1;
242 while (idx >= 0) {
243 /* request a packet from the currently selected filter */
244 ret = av_bsf_receive_packet(s->bsfs[idx], pkt);
245 if (ret == AVERROR(EAGAIN)) {
246 /* no packets available, try the next filter up the chain */
247 ret = 0;
248 idx--;
249 continue;
250 } else if (ret < 0 && ret != AVERROR_EOF) {
251 return ret;
252 }
253
254 /* got a packet or EOF -- pass it to the caller or to the next filter
255 * down the chain */
256 if (idx == s->nb_bsfs - 1) {
257 return ret;
258 } else {
259 idx++;
260 ret = av_bsf_send_packet(s->bsfs[idx], ret < 0 ? NULL : pkt);
261 if (ret < 0) {
262 av_log(avctx, AV_LOG_ERROR,
263 "Error pre-processing a packet before decoding\n");
264 av_packet_unref(pkt);
265 return ret;
266 }
267 }
268 }
269
270 return AVERROR(EAGAIN);
271}
272
061a0c14 273int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
3fe2a01d 274{
061a0c14 275 AVCodecInternal *avci = avctx->internal;
3fe2a01d
AK
276 int ret;
277
061a0c14
AK
278 if (avci->draining)
279 return AVERROR_EOF;
280
972c71e9
AK
281 ret = bsfs_poll(avctx, pkt);
282 if (ret == AVERROR_EOF)
283 avci->draining = 1;
284 if (ret < 0)
285 return ret;
061a0c14
AK
286
287 ret = extract_packet_props(avctx->internal, pkt);
288 if (ret < 0)
289 goto finish;
290
291 ret = apply_param_change(avctx, pkt);
292 if (ret < 0)
293 goto finish;
294
295 if (avctx->codec->receive_frame)
296 avci->compat_decode_consumed += pkt->size;
3fe2a01d 297
061a0c14
AK
298 return 0;
299finish:
300 av_packet_unref(pkt);
301 return ret;
302}
303
304/*
305 * The core of the receive_frame_wrapper for the decoders implementing
306 * the simple API. Certain decoders might consume partial packets without
307 * returning any output, so this function needs to be called in a loop until it
308 * returns EAGAIN.
309 **/
310static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
311{
312 AVCodecInternal *avci = avctx->internal;
313 DecodeSimpleContext *ds = &avci->ds;
314 AVPacket *pkt = ds->in_pkt;
315 int got_frame;
316 int ret;
3fe2a01d 317
061a0c14
AK
318 if (!pkt->data && !avci->draining) {
319 av_packet_unref(pkt);
320 ret = ff_decode_get_packet(avctx, pkt);
321 if (ret < 0 && ret != AVERROR_EOF)
322 return ret;
323 }
3fe2a01d
AK
324
325 // Some codecs (at least wma lossless) will crash when feeding drain packets
326 // after EOF was signaled.
061a0c14
AK
327 if (avci->draining_done)
328 return AVERROR_EOF;
329
330 if (!pkt->data &&
331 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
332 avctx->active_thread_type & FF_THREAD_FRAME))
3fe2a01d
AK
333 return AVERROR_EOF;
334
061a0c14
AK
335 got_frame = 0;
336
337 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
338 ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
3fe2a01d 339 } else {
061a0c14
AK
340 ret = avctx->codec->decode(avctx, frame, &got_frame, pkt);
341
342 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
343 frame->pkt_dts = pkt->dts;
344 /* get_buffer is supposed to set frame parameters */
345 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
346 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
347 frame->width = avctx->width;
348 frame->height = avctx->height;
349 frame->format = avctx->codec->type == AVMEDIA_TYPE_VIDEO ?
350 avctx->pix_fmt : avctx->sample_fmt;
351 }
3fe2a01d
AK
352 }
353
061a0c14
AK
354 emms_c();
355
356 if (!got_frame)
357 av_frame_unref(frame);
358
359 if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
360 ret = pkt->size;
361
3fe2a01d 362 if (avctx->internal->draining && !got_frame)
061a0c14
AK
363 avci->draining_done = 1;
364
365 avci->compat_decode_consumed += ret;
3fe2a01d 366
061a0c14
AK
367 if (ret >= pkt->size || ret < 0) {
368 av_packet_unref(pkt);
3fe2a01d
AK
369 } else {
370 int consumed = ret;
371
061a0c14
AK
372 pkt->data += consumed;
373 pkt->size -= consumed;
374 pkt->pts = AV_NOPTS_VALUE;
375 pkt->dts = AV_NOPTS_VALUE;
376 avci->last_pkt_props->pts = AV_NOPTS_VALUE;
377 avci->last_pkt_props->dts = AV_NOPTS_VALUE;
3fe2a01d
AK
378 }
379
380 if (got_frame)
061a0c14
AK
381 av_assert0(frame->buf[0]);
382
383 return ret < 0 ? ret : 0;
384}
385
386static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
387{
388 int ret;
389
390 while (!frame->buf[0]) {
391 ret = decode_simple_internal(avctx, frame);
392 if (ret < 0)
393 return ret;
394 }
3fe2a01d
AK
395
396 return 0;
397}
398
061a0c14 399static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
3fe2a01d 400{
061a0c14 401 AVCodecInternal *avci = avctx->internal;
3fe2a01d
AK
402 int ret;
403
061a0c14
AK
404 av_assert0(!frame->buf[0]);
405
406 if (avctx->codec->receive_frame)
407 ret = avctx->codec->receive_frame(avctx, frame);
408 else
409 ret = decode_simple_receive_frame(avctx, frame);
410
411 if (ret == AVERROR_EOF)
412 avci->draining_done = 1;
413
359a8a3e
AK
414 /* unwrap the per-frame decode data and restore the original opaque_ref*/
415 if (!ret) {
416 /* the only case where decode data is not set should be decoders
417 * that do not call ff_get_buffer() */
418 av_assert0((frame->opaque_ref && frame->opaque_ref->size == sizeof(FrameDecodeData)) ||
419 !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
420
421 if (frame->opaque_ref) {
422 FrameDecodeData *fdd;
423 AVBufferRef *user_opaque_ref;
424
425 fdd = (FrameDecodeData*)frame->opaque_ref->data;
426
badf0951
AK
427 if (fdd->post_process) {
428 ret = fdd->post_process(avctx, frame);
429 if (ret < 0) {
430 av_frame_unref(frame);
431 return ret;
432 }
433 }
434
359a8a3e
AK
435 user_opaque_ref = fdd->user_opaque_ref;
436 fdd->user_opaque_ref = NULL;
437 av_buffer_unref(&frame->opaque_ref);
438 frame->opaque_ref = user_opaque_ref;
439 }
440 }
441
061a0c14
AK
442 return ret;
443}
444
445int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
446{
447 AVCodecInternal *avci = avctx->internal;
448 int ret = 0;
449
3fe2a01d
AK
450 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
451 return AVERROR(EINVAL);
452
453 if (avctx->internal->draining)
454 return AVERROR_EOF;
455
972c71e9
AK
456 av_packet_unref(avci->buffer_pkt);
457 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
061a0c14
AK
458 ret = av_packet_ref(avci->buffer_pkt, avpkt);
459 if (ret < 0)
460 return ret;
3fe2a01d
AK
461 }
462
972c71e9
AK
463 ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
464 if (ret < 0) {
465 av_packet_unref(avci->buffer_pkt);
466 return ret;
467 }
468
061a0c14
AK
469 if (!avci->buffer_frame->buf[0]) {
470 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
471 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
472 return ret;
3fe2a01d
AK
473 }
474
061a0c14 475 return 0;
3fe2a01d
AK
476}
477
019ab88a
AK
478static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
479{
019ab88a
AK
480 /* make sure we are noisy about decoders returning invalid cropping data */
481 if (frame->crop_left >= INT_MAX - frame->crop_right ||
482 frame->crop_top >= INT_MAX - frame->crop_bottom ||
483 (frame->crop_left + frame->crop_right) >= frame->width ||
484 (frame->crop_top + frame->crop_bottom) >= frame->height) {
485 av_log(avctx, AV_LOG_WARNING,
486 "Invalid cropping information set by a decoder: %zu/%zu/%zu/%zu "
487 "(frame size %dx%d). This is a bug, please report it\n",
488 frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
489 frame->width, frame->height);
490 frame->crop_left = 0;
491 frame->crop_right = 0;
492 frame->crop_top = 0;
493 frame->crop_bottom = 0;
494 return 0;
495 }
496
497 if (!avctx->apply_cropping)
498 return 0;
499
47399ccd 500 return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
501 AV_FRAME_CROP_UNALIGNED : 0);
019ab88a
AK
502}
503
3fe2a01d
AK
504int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
505{
061a0c14 506 AVCodecInternal *avci = avctx->internal;
3fe2a01d
AK
507 int ret;
508
509 av_frame_unref(frame);
510
511 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
512 return AVERROR(EINVAL);
513
061a0c14
AK
514 if (avci->buffer_frame->buf[0]) {
515 av_frame_move_ref(frame, avci->buffer_frame);
516 } else {
517 ret = decode_receive_frame_internal(avctx, frame);
518 if (ret < 0)
519 return ret;
3fe2a01d
AK
520 }
521
019ab88a
AK
522 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
523 ret = apply_cropping(avctx, frame);
524 if (ret < 0) {
525 av_frame_unref(frame);
526 return ret;
527 }
528 }
529
061a0c14 530 avctx->frame_number++;
3fe2a01d 531
3fe2a01d
AK
532 return 0;
533}
534
061a0c14
AK
535static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
536 int *got_frame, AVPacket *pkt)
3fe2a01d
AK
537{
538 AVCodecInternal *avci = avctx->internal;
efddf2c0 539 int ret = 0;
3fe2a01d 540
061a0c14 541 av_assert0(avci->compat_decode_consumed == 0);
3fe2a01d 542
061a0c14
AK
543 *got_frame = 0;
544 avci->compat_decode = 1;
3fe2a01d 545
061a0c14
AK
546 if (avci->compat_decode_partial_size > 0 &&
547 avci->compat_decode_partial_size != pkt->size) {
548 av_log(avctx, AV_LOG_ERROR,
549 "Got unexpected packet size after a partial decode\n");
550 ret = AVERROR(EINVAL);
551 goto finish;
552 }
549d0bdc 553
061a0c14
AK
554 if (!avci->compat_decode_partial_size) {
555 ret = avcodec_send_packet(avctx, pkt);
556 if (ret == AVERROR_EOF)
557 ret = 0;
558 else if (ret == AVERROR(EAGAIN)) {
559 /* we fully drain all the output in each decode call, so this should not
560 * ever happen */
561 ret = AVERROR_BUG;
562 goto finish;
563 } else if (ret < 0)
564 goto finish;
565 }
3fe2a01d 566
061a0c14
AK
567 while (ret >= 0) {
568 ret = avcodec_receive_frame(avctx, frame);
569 if (ret < 0) {
570 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
571 ret = 0;
572 goto finish;
3fe2a01d
AK
573 }
574
061a0c14 575 if (frame != avci->compat_decode_frame) {
3fe2a01d 576 if (!avctx->refcounted_frames) {
061a0c14
AK
577 ret = unrefcount_frame(avci, frame);
578 if (ret < 0)
579 goto finish;
3fe2a01d
AK
580 }
581
061a0c14
AK
582 *got_frame = 1;
583 frame = avci->compat_decode_frame;
584 } else {
585 if (!avci->compat_decode_warned) {
586 av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
587 "API cannot return all the frames for this decoder. "
588 "Some frames will be dropped. Update your code to the "
589 "new decoding API to fix this.\n");
590 avci->compat_decode_warned = 1;
591 }
592 }
3fe2a01d 593
972c71e9 594 if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
061a0c14
AK
595 break;
596 }
597
598finish:
972c71e9
AK
599 if (ret == 0) {
600 /* if there are any bsfs then assume full packet is always consumed */
601 if (avctx->codec->bsfs)
602 ret = pkt->size;
603 else
604 ret = FFMIN(avci->compat_decode_consumed, pkt->size);
605 }
061a0c14
AK
606 avci->compat_decode_consumed = 0;
607 avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
3fe2a01d
AK
608
609 return ret;
610}
611
061a0c14
AK
612int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
613 int *got_picture_ptr,
614 AVPacket *avpkt)
615{
616 return compat_decode(avctx, picture, got_picture_ptr, avpkt);
617}
618
3fe2a01d
AK
619int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
620 AVFrame *frame,
621 int *got_frame_ptr,
622 AVPacket *avpkt)
623{
061a0c14 624 return compat_decode(avctx, frame, got_frame_ptr, avpkt);
3fe2a01d
AK
625}
626
627int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
628 int *got_sub_ptr,
629 AVPacket *avpkt)
630{
631 int ret;
632
549d0bdc
AK
633 ret = extract_packet_props(avctx->internal, avpkt);
634 if (ret < 0)
635 return ret;
636
3fe2a01d
AK
637 *got_sub_ptr = 0;
638 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
639 if (*got_sub_ptr)
640 avctx->frame_number++;
641 return ret;
642}
643
39056b52
MT
644enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
645 const enum AVPixelFormat *fmt)
3fe2a01d 646{
39056b52
MT
647 const AVPixFmtDescriptor *desc;
648 const AVCodecHWConfig *config;
649 int i, n;
650
651 // If a device was supplied when the codec was opened, assume that the
652 // user wants to use it.
653 if (avctx->hw_device_ctx && avctx->codec->hw_configs) {
654 AVHWDeviceContext *device_ctx =
655 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
656 for (i = 0;; i++) {
657 config = &avctx->codec->hw_configs[i]->public;
658 if (!config)
659 break;
660 if (!(config->methods &
661 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
662 continue;
663 if (device_ctx->type != config->device_type)
664 continue;
665 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
666 if (config->pix_fmt == fmt[n])
667 return fmt[n];
668 }
669 }
670 }
671 // No device or other setup, so we have to choose from things which
672 // don't any other external information.
673
674 // If the last element of the list is a software format, choose it
675 // (this should be best software format if any exist).
676 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
677 desc = av_pix_fmt_desc_get(fmt[n - 1]);
678 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
679 return fmt[n - 1];
680
681 // Finally, traverse the list in order and choose the first entry
682 // with no external dependencies (if there is no hardware configuration
683 // information available then this just picks the first entry).
684 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
685 for (i = 0;; i++) {
686 config = avcodec_get_hw_config(avctx->codec, i);
687 if (!config)
688 break;
689 if (config->pix_fmt == fmt[n])
690 break;
691 }
692 if (!config) {
693 // No specific config available, so the decoder must be able
694 // to handle this format without any additional setup.
695 return fmt[n];
696 }
697 if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
698 // Usable with only internal setup.
699 return fmt[n];
700 }
701 }
3fe2a01d 702
39056b52
MT
703 // Nothing is usable, give up.
704 return AV_PIX_FMT_NONE;
3fe2a01d
AK
705}
706
b46a77f1 707int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
708 enum AVHWDeviceType dev_type)
709{
710 AVHWDeviceContext *device_ctx;
711 AVHWFramesContext *frames_ctx;
712 int ret;
713
714 if (!avctx->hwaccel)
715 return AVERROR(ENOSYS);
716
717 if (avctx->hw_frames_ctx)
718 return 0;
719 if (!avctx->hw_device_ctx) {
720 av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
721 "required for hardware accelerated decoding.\n");
722 return AVERROR(EINVAL);
723 }
724
725 device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
726 if (device_ctx->type != dev_type) {
727 av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
728 "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
729 av_hwdevice_get_type_name(device_ctx->type));
730 return AVERROR(EINVAL);
731 }
732
733 ret = avcodec_get_hw_frames_parameters(avctx,
734 avctx->hw_device_ctx,
735 avctx->hwaccel->pix_fmt,
e724bdff 736 &avctx->hw_frames_ctx);
737 if (ret < 0)
b46a77f1 738 return ret;
b46a77f1 739
740 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
741
742
743 if (frames_ctx->initial_pool_size) {
744 // We guarantee 4 base work surfaces. The function above guarantees 1
745 // (the absolute minimum), so add the missing count.
746 frames_ctx->initial_pool_size += 3;
b46a77f1 747 }
748
749 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
750 if (ret < 0) {
751 av_buffer_unref(&avctx->hw_frames_ctx);
752 return ret;
753 }
754
755 return 0;
756}
757
758int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
759 AVBufferRef *device_ref,
760 enum AVPixelFormat hw_pix_fmt,
761 AVBufferRef **out_frames_ref)
762{
763 AVBufferRef *frames_ref = NULL;
39056b52
MT
764 const AVCodecHWConfigInternal *hw_config;
765 const AVHWAccel *hwa;
766 int i, ret;
767
768 for (i = 0;; i++) {
769 hw_config = avctx->codec->hw_configs[i];
770 if (!hw_config)
771 return AVERROR(ENOENT);
772 if (hw_config->public.pix_fmt == hw_pix_fmt)
773 break;
774 }
b46a77f1 775
39056b52 776 hwa = hw_config->hwaccel;
b46a77f1 777 if (!hwa || !hwa->frame_params)
778 return AVERROR(ENOENT);
779
780 frames_ref = av_hwframe_ctx_alloc(device_ref);
781 if (!frames_ref)
782 return AVERROR(ENOMEM);
783
784 ret = hwa->frame_params(avctx, frames_ref);
785 if (ret >= 0) {
5b145290
MT
786 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
787
788 if (frames_ctx->initial_pool_size) {
789 // If the user has requested that extra output surfaces be
790 // available then add them here.
791 if (avctx->extra_hw_frames > 0)
792 frames_ctx->initial_pool_size += avctx->extra_hw_frames;
cad739da
MT
793
794 // If frame threading is enabled then an extra surface per thread
795 // is also required.
796 if (avctx->active_thread_type & FF_THREAD_FRAME)
797 frames_ctx->initial_pool_size += avctx->thread_count;
5b145290
MT
798 }
799
b46a77f1 800 *out_frames_ref = frames_ref;
801 } else {
802 av_buffer_unref(&frames_ref);
803 }
804 return ret;
805}
806
39056b52
MT
807static int hwaccel_init(AVCodecContext *avctx,
808 const AVCodecHWConfigInternal *hw_config)
3fe2a01d 809{
39056b52
MT
810 const AVHWAccel *hwaccel;
811 int err;
3fe2a01d 812
39056b52
MT
813 hwaccel = hw_config->hwaccel;
814 if (hwaccel->priv_data_size) {
815 avctx->internal->hwaccel_priv_data =
816 av_mallocz(hwaccel->priv_data_size);
3fe2a01d
AK
817 if (!avctx->internal->hwaccel_priv_data)
818 return AVERROR(ENOMEM);
819 }
820
2117725d 821 avctx->hwaccel = hwaccel;
39056b52
MT
822 err = hwaccel->init(avctx);
823 if (err < 0) {
824 av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
825 "hwaccel initialisation returned error.\n",
826 av_get_pix_fmt_name(hw_config->public.pix_fmt));
827 av_freep(&avctx->internal->hwaccel_priv_data);
828 avctx->hwaccel = NULL;
829 return err;
3fe2a01d
AK
830 }
831
3fe2a01d
AK
832 return 0;
833}
834
39056b52
MT
835static void hwaccel_uninit(AVCodecContext *avctx)
836{
837 if (avctx->hwaccel && avctx->hwaccel->uninit)
838 avctx->hwaccel->uninit(avctx);
839
840 av_freep(&avctx->internal->hwaccel_priv_data);
841
842 avctx->hwaccel = NULL;
843
844 av_buffer_unref(&avctx->hw_frames_ctx);
845}
846
3fe2a01d
AK
847int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
848{
849 const AVPixFmtDescriptor *desc;
850 enum AVPixelFormat *choices;
39056b52
MT
851 enum AVPixelFormat ret, user_choice;
852 const AVCodecHWConfigInternal *hw_config;
853 const AVCodecHWConfig *config;
854 int i, n, err;
855
856 // Find end of list.
857 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
858 // Must contain at least one entry.
3fe2a01d 859 av_assert0(n >= 1);
39056b52
MT
860 // If a software format is available, it must be the last entry.
861 desc = av_pix_fmt_desc_get(fmt[n - 1]);
862 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
863 // No software format is available.
864 } else {
865 avctx->sw_pix_fmt = fmt[n - 1];
866 }
3fe2a01d
AK
867
868 choices = av_malloc_array(n + 1, sizeof(*choices));
869 if (!choices)
870 return AV_PIX_FMT_NONE;
871
872 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
873
874 for (;;) {
39056b52
MT
875 // Remove the previous hwaccel, if there was one.
876 hwaccel_uninit(avctx);
3fe2a01d 877
39056b52
MT
878 user_choice = avctx->get_format(avctx, choices);
879 if (user_choice == AV_PIX_FMT_NONE) {
880 // Explicitly chose nothing, give up.
881 ret = AV_PIX_FMT_NONE;
882 break;
883 }
3fe2a01d 884
39056b52 885 desc = av_pix_fmt_desc_get(user_choice);
3fe2a01d 886 if (!desc) {
39056b52
MT
887 av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
888 "get_format() callback.\n");
3fe2a01d
AK
889 ret = AV_PIX_FMT_NONE;
890 break;
891 }
39056b52
MT
892 av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
893 desc->name);
3fe2a01d 894
39056b52
MT
895 for (i = 0; i < n; i++) {
896 if (choices[i] == user_choice)
897 break;
898 }
899 if (i == n) {
900 av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
901 "%s not in possible list.\n", desc->name);
3fe2a01d 902 break;
39056b52 903 }
3fe2a01d 904
39056b52
MT
905 if (avctx->codec->hw_configs) {
906 for (i = 0;; i++) {
907 hw_config = avctx->codec->hw_configs[i];
908 if (!hw_config)
909 break;
910 if (hw_config->public.pix_fmt == user_choice)
911 break;
3fe2a01d 912 }
39056b52
MT
913 } else {
914 hw_config = NULL;
3fe2a01d
AK
915 }
916
39056b52
MT
917 if (!hw_config) {
918 // No config available, so no extra setup required.
919 ret = user_choice;
3fe2a01d 920 break;
39056b52
MT
921 }
922 config = &hw_config->public;
923
924 if (config->methods &
925 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
926 avctx->hw_frames_ctx) {
927 const AVHWFramesContext *frames_ctx =
928 (AVHWFramesContext*)avctx->hw_frames_ctx->data;
929 if (frames_ctx->format != user_choice) {
930 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
931 "does not match the format of the provided frames "
932 "context.\n", desc->name);
933 goto try_again;
934 }
935 } else if (config->methods &
936 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
937 avctx->hw_device_ctx) {
938 const AVHWDeviceContext *device_ctx =
939 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
940 if (device_ctx->type != config->device_type) {
941 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
942 "does not match the type of the provided device "
943 "context.\n", desc->name);
944 goto try_again;
945 }
946 } else if (config->methods &
947 AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
948 // Internal-only setup, no additional configuration.
949 } else if (config->methods &
950 AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
951 // Some ad-hoc configuration we can't see and can't check.
952 } else {
953 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
954 "missing configuration.\n", desc->name);
955 goto try_again;
956 }
957 if (hw_config->hwaccel) {
958 av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel "
959 "initialisation.\n", desc->name);
960 err = hwaccel_init(avctx, hw_config);
961 if (err < 0)
962 goto try_again;
963 }
964 ret = user_choice;
965 break;
966
967 try_again:
968 av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
969 "get_format() without it.\n", desc->name);
970 for (i = 0; i < n; i++) {
971 if (choices[i] == user_choice)
972 break;
973 }
974 for (; i + 1 < n; i++)
975 choices[i] = choices[i + 1];
976 --n;
3fe2a01d
AK
977 }
978
979 av_freep(&choices);
980 return ret;
981}
982
983static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
984{
985 FramePool *pool = avctx->internal->pool;
986 int i, ret;
987
988 switch (avctx->codec_type) {
989 case AVMEDIA_TYPE_VIDEO: {
990 uint8_t *data[4];
991 int linesize[4];
992 int size[4] = { 0 };
993 int w = frame->width;
994 int h = frame->height;
995 int tmpsize, unaligned;
996
997 if (pool->format == frame->format &&
998 pool->width == frame->width && pool->height == frame->height)
999 return 0;
1000
1001 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
1002
1003 do {
1004 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
1005 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
1006 av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
1007 // increase alignment of w for next try (rhs gives the lowest bit set in w)
1008 w += w & ~(w - 1);
1009
1010 unaligned = 0;
1011 for (i = 0; i < 4; i++)
1012 unaligned |= linesize[i] % pool->stride_align[i];
1013 } while (unaligned);
1014
1015 tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
1016 NULL, linesize);
1017 if (tmpsize < 0)
1018 return -1;
1019
1020 for (i = 0; i < 3 && data[i + 1]; i++)
1021 size[i] = data[i + 1] - data[i];
1022 size[i] = tmpsize - (data[i] - data[0]);
1023
1024 for (i = 0; i < 4; i++) {
1025 av_buffer_pool_uninit(&pool->pools[i]);
1026 pool->linesize[i] = linesize[i];
1027 if (size[i]) {
1028 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
1029 if (!pool->pools[i]) {
1030 ret = AVERROR(ENOMEM);
1031 goto fail;
1032 }
1033 }
1034 }
1035 pool->format = frame->format;
1036 pool->width = frame->width;
1037 pool->height = frame->height;
1038
1039 break;
1040 }
1041 case AVMEDIA_TYPE_AUDIO: {
1042 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
1043 int planar = av_sample_fmt_is_planar(frame->format);
1044 int planes = planar ? ch : 1;
1045
1046 if (pool->format == frame->format && pool->planes == planes &&
1047 pool->channels == ch && frame->nb_samples == pool->samples)
1048 return 0;
1049
1050 av_buffer_pool_uninit(&pool->pools[0]);
1051 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
1052 frame->nb_samples, frame->format, 0);
1053 if (ret < 0)
1054 goto fail;
1055
1056 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
1057 if (!pool->pools[0]) {
1058 ret = AVERROR(ENOMEM);
1059 goto fail;
1060 }
1061
1062 pool->format = frame->format;
1063 pool->planes = planes;
1064 pool->channels = ch;
1065 pool->samples = frame->nb_samples;
1066 break;
1067 }
1068 default: av_assert0(0);
1069 }
1070 return 0;
1071fail:
1072 for (i = 0; i < 4; i++)
1073 av_buffer_pool_uninit(&pool->pools[i]);
1074 pool->format = -1;
1075 pool->planes = pool->channels = pool->samples = 0;
1076 pool->width = pool->height = 0;
1077 return ret;
1078}
1079
1080static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
1081{
1082 FramePool *pool = avctx->internal->pool;
1083 int planes = pool->planes;
1084 int i;
1085
1086 frame->linesize[0] = pool->linesize[0];
1087
1088 if (planes > AV_NUM_DATA_POINTERS) {
1089 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
1090 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
1091 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
1092 sizeof(*frame->extended_buf));
1093 if (!frame->extended_data || !frame->extended_buf) {
1094 av_freep(&frame->extended_data);
1095 av_freep(&frame->extended_buf);
1096 return AVERROR(ENOMEM);
1097 }
1098 } else
1099 frame->extended_data = frame->data;
1100
1101 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
1102 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
1103 if (!frame->buf[i])
1104 goto fail;
1105 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
1106 }
1107 for (i = 0; i < frame->nb_extended_buf; i++) {
1108 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
1109 if (!frame->extended_buf[i])
1110 goto fail;
1111 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
1112 }
1113
1114 if (avctx->debug & FF_DEBUG_BUFFERS)
1115 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
1116
1117 return 0;
1118fail:
1119 av_frame_unref(frame);
1120 return AVERROR(ENOMEM);
1121}
1122
1123static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
1124{
1125 FramePool *pool = s->internal->pool;
1126 int i;
1127
1128 if (pic->data[0]) {
1129 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
1130 return -1;
1131 }
1132
1133 memset(pic->data, 0, sizeof(pic->data));
1134 pic->extended_data = pic->data;
1135
1136 for (i = 0; i < 4 && pool->pools[i]; i++) {
1137 pic->linesize[i] = pool->linesize[i];
1138
1139 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
1140 if (!pic->buf[i])
1141 goto fail;
1142
1143 pic->data[i] = pic->buf[i]->data;
1144 }
1145 for (; i < AV_NUM_DATA_POINTERS; i++) {
1146 pic->data[i] = NULL;
1147 pic->linesize[i] = 0;
1148 }
1149 if (pic->data[1] && !pic->data[2])
1150 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
1151
1152 if (s->debug & FF_DEBUG_BUFFERS)
1153 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
1154
1155 return 0;
1156fail:
1157 av_frame_unref(pic);
1158 return AVERROR(ENOMEM);
1159}
1160
1161int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
1162{
1163 int ret;
1164
3fdf50f9
AK
1165 if (avctx->hw_frames_ctx) {
1166 ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
1167 frame->width = avctx->coded_width;
1168 frame->height = avctx->coded_height;
1169 return ret;
1170 }
3fe2a01d
AK
1171
1172 if ((ret = update_frame_pool(avctx, frame)) < 0)
1173 return ret;
1174
1175 switch (avctx->codec_type) {
1176 case AVMEDIA_TYPE_VIDEO:
1177 return video_get_buffer(avctx, frame);
1178 case AVMEDIA_TYPE_AUDIO:
1179 return audio_get_buffer(avctx, frame);
1180 default:
1181 return -1;
1182 }
1183}
1184
1185int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
1186{
549d0bdc 1187 AVPacket *pkt = avctx->internal->last_pkt_props;
3fe2a01d
AK
1188 int i;
1189 struct {
1190 enum AVPacketSideDataType packet;
1191 enum AVFrameSideDataType frame;
1192 } sd[] = {
1193 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
1194 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
2fb6acd9 1195 { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
3fe2a01d
AK
1196 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
1197 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
1198 };
1199
1200 frame->color_primaries = avctx->color_primaries;
1201 frame->color_trc = avctx->color_trc;
1202 frame->colorspace = avctx->colorspace;
1203 frame->color_range = avctx->color_range;
1204 frame->chroma_location = avctx->chroma_sample_location;
1205
1206 frame->reordered_opaque = avctx->reordered_opaque;
3fe2a01d
AK
1207
1208#if FF_API_PKT_PTS
1209FF_DISABLE_DEPRECATION_WARNINGS
1210 frame->pkt_pts = pkt->pts;
1211FF_ENABLE_DEPRECATION_WARNINGS
1212#endif
1213 frame->pts = pkt->pts;
1214
1215 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
1216 int size;
1217 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
1218 if (packet_sd) {
1219 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1220 sd[i].frame,
1221 size);
1222 if (!frame_sd)
1223 return AVERROR(ENOMEM);
1224
1225 memcpy(frame_sd->data, packet_sd, size);
1226 }
1227 }
1228
1229 return 0;
1230}
1231
359a8a3e
AK
1232static void decode_data_free(void *opaque, uint8_t *data)
1233{
1234 FrameDecodeData *fdd = (FrameDecodeData*)data;
1235
1236 av_buffer_unref(&fdd->user_opaque_ref);
1237
badf0951
AK
1238 if (fdd->post_process_opaque_free)
1239 fdd->post_process_opaque_free(fdd->post_process_opaque);
1240
704311b2
AK
1241 if (fdd->hwaccel_priv_free)
1242 fdd->hwaccel_priv_free(fdd->hwaccel_priv);
1243
359a8a3e
AK
1244 av_freep(&fdd);
1245}
1246
1247static int attach_decode_data(AVFrame *frame)
1248{
1249 AVBufferRef *fdd_buf;
1250 FrameDecodeData *fdd;
1251
1252 fdd = av_mallocz(sizeof(*fdd));
1253 if (!fdd)
1254 return AVERROR(ENOMEM);
1255
1256 fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
1257 NULL, AV_BUFFER_FLAG_READONLY);
1258 if (!fdd_buf) {
1259 av_freep(&fdd);
1260 return AVERROR(ENOMEM);
1261 }
1262
1263 fdd->user_opaque_ref = frame->opaque_ref;
1264 frame->opaque_ref = fdd_buf;
1265
1266 return 0;
1267}
1268
3fe2a01d
AK
1269int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1270{
1271 const AVHWAccel *hwaccel = avctx->hwaccel;
1272 int override_dimensions = 1;
1273 int ret;
1274
1275 switch (avctx->codec_type) {
1276 case AVMEDIA_TYPE_VIDEO:
1277 if (frame->width <= 0 || frame->height <= 0) {
1278 frame->width = FFMAX(avctx->width, avctx->coded_width);
1279 frame->height = FFMAX(avctx->height, avctx->coded_height);
1280 override_dimensions = 0;
1281 }
1282 if (frame->format < 0)
1283 frame->format = avctx->pix_fmt;
1284 if (!frame->sample_aspect_ratio.num)
1285 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
1286
1287 if (av_image_check_sar(frame->width, frame->height,
1288 frame->sample_aspect_ratio) < 0) {
1289 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1290 frame->sample_aspect_ratio.num,
1291 frame->sample_aspect_ratio.den);
1292 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
1293 }
1294
1295 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1296 return ret;
1297 break;
1298 case AVMEDIA_TYPE_AUDIO:
1299 if (!frame->sample_rate)
1300 frame->sample_rate = avctx->sample_rate;
1301 if (frame->format < 0)
1302 frame->format = avctx->sample_fmt;
1303 if (!frame->channel_layout) {
1304 if (avctx->channel_layout) {
1305 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
1306 avctx->channels) {
1307 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
1308 "configuration.\n");
1309 return AVERROR(EINVAL);
1310 }
1311
1312 frame->channel_layout = avctx->channel_layout;
1313 } else {
1314 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1315 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
1316 avctx->channels);
1317 return AVERROR(ENOSYS);
1318 }
1319
1320 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
1321 if (!frame->channel_layout)
1322 frame->channel_layout = (1ULL << avctx->channels) - 1;
1323 }
1324 }
1325 break;
1326 default: return AVERROR(EINVAL);
1327 }
1328
1329 ret = ff_decode_frame_props(avctx, frame);
1330 if (ret < 0)
1331 return ret;
1332
1333 if (hwaccel) {
1334 if (hwaccel->alloc_frame) {
1335 ret = hwaccel->alloc_frame(avctx, frame);
1336 goto end;
1337 }
1338 } else
1339 avctx->sw_pix_fmt = avctx->pix_fmt;
1340
1341 ret = avctx->get_buffer2(avctx, frame, flags);
359a8a3e
AK
1342 if (ret < 0)
1343 goto end;
1344
1345 ret = attach_decode_data(frame);
1346 if (ret < 0)
1347 goto end;
3fe2a01d
AK
1348
1349end:
019ab88a
AK
1350 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
1351 !(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
3fe2a01d
AK
1352 frame->width = avctx->width;
1353 frame->height = avctx->height;
1354 }
1355
de776714
AK
1356 if (ret < 0)
1357 av_frame_unref(frame);
1358
3fe2a01d
AK
1359 return ret;
1360}
1361
1362int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
1363{
1364 AVFrame *tmp;
1365 int ret;
1366
1367 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1368
1369 if (!frame->data[0])
1370 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1371
1372 if (av_frame_is_writable(frame))
1373 return ff_decode_frame_props(avctx, frame);
1374
1375 tmp = av_frame_alloc();
1376 if (!tmp)
1377 return AVERROR(ENOMEM);
1378
1379 av_frame_move_ref(tmp, frame);
1380
1381 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1382 if (ret < 0) {
1383 av_frame_free(&tmp);
1384 return ret;
1385 }
1386
1387 av_frame_copy(frame, tmp);
1388 av_frame_free(&tmp);
1389
1390 return 0;
1391}
1392
ad99cbc9
JA
1393static void bsfs_flush(AVCodecContext *avctx)
1394{
1395 DecodeFilterContext *s = &avctx->internal->filter;
1396
1397 for (int i = 0; i < s->nb_bsfs; i++)
1398 av_bsf_flush(s->bsfs[i]);
1399}
1400
3fe2a01d
AK
1401void avcodec_flush_buffers(AVCodecContext *avctx)
1402{
1403 avctx->internal->draining = 0;
1404 avctx->internal->draining_done = 0;
1405 av_frame_unref(avctx->internal->buffer_frame);
061a0c14 1406 av_frame_unref(avctx->internal->compat_decode_frame);
3fe2a01d
AK
1407 av_packet_unref(avctx->internal->buffer_pkt);
1408 avctx->internal->buffer_pkt_valid = 0;
1409
061a0c14
AK
1410 av_packet_unref(avctx->internal->ds.in_pkt);
1411
3fe2a01d
AK
1412 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1413 ff_thread_flush(avctx);
1414 else if (avctx->codec->flush)
1415 avctx->codec->flush(avctx);
1416
ad99cbc9 1417 bsfs_flush(avctx);
972c71e9 1418
3fe2a01d
AK
1419 if (!avctx->refcounted_frames)
1420 av_frame_unref(avctx->internal->to_free);
1421}
972c71e9
AK
1422
1423void ff_decode_bsfs_uninit(AVCodecContext *avctx)
1424{
1425 DecodeFilterContext *s = &avctx->internal->filter;
1426 int i;
1427
1428 for (i = 0; i < s->nb_bsfs; i++)
1429 av_bsf_free(&s->bsfs[i]);
1430 av_freep(&s->bsfs);
1431 s->nb_bsfs = 0;
1432}