mmaldec: add vc1 decoding support
[libav.git] / libavcodec / mmaldec.c
1 /*
2 * MMAL Video Decoder
3 * Copyright (c) 2015 Rodger Combs
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * MMAL Video Decoder
25 */
26
27 #include <bcm_host.h>
28 #include <interface/mmal/mmal.h>
29 #include <interface/mmal/mmal_parameters_video.h>
30 #include <interface/mmal/util/mmal_util.h>
31 #include <interface/mmal/util/mmal_util_params.h>
32 #include <interface/mmal/util/mmal_default_components.h>
33 #include <interface/mmal/vc/mmal_vc_api.h>
34
35 #include "avcodec.h"
36 #include "internal.h"
37 #include "libavutil/atomic.h"
38 #include "libavutil/avassert.h"
39 #include "libavutil/buffer.h"
40 #include "libavutil/common.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/opt.h"
43 #include "libavutil/log.h"
44
45 typedef struct FFBufferEntry {
46 AVBufferRef *ref;
47 void *data;
48 size_t length;
49 int64_t pts, dts;
50 int flags;
51 struct FFBufferEntry *next;
52 } FFBufferEntry;
53
54 // MMAL_POOL_T destroys all of its MMAL_BUFFER_HEADER_Ts. If we want correct
55 // refcounting for AVFrames, we can free the MMAL_POOL_T only after all AVFrames
56 // have been unreferenced.
57 typedef struct FFPoolRef {
58 volatile int refcount;
59 MMAL_POOL_T *pool;
60 } FFPoolRef;
61
62 typedef struct FFBufferRef {
63 MMAL_BUFFER_HEADER_T *buffer;
64 FFPoolRef *pool;
65 } FFBufferRef;
66
67 typedef struct MMALDecodeContext {
68 AVClass *av_class;
69 int extra_buffers;
70
71 MMAL_COMPONENT_T *decoder;
72 MMAL_QUEUE_T *queue_decoded_frames;
73 MMAL_POOL_T *pool_in;
74 FFPoolRef *pool_out;
75
76 // Waiting input packets. Because the libavcodec API requires decoding and
77 // returning packets in lockstep, it can happen that queue_decoded_frames
78 // contains almost all surfaces - then the decoder input queue can quickly
79 // fill up and won't accept new input either. Without consuming input, the
80 // libavcodec API can't return new frames, and we have a logical deadlock.
81 // This is avoided by queuing such buffers here.
82 FFBufferEntry *waiting_buffers, *waiting_buffers_tail;
83
84 int64_t packets_sent;
85 volatile int packets_buffered;
86 int64_t frames_output;
87 int eos_received;
88 int eos_sent;
89 int extradata_sent;
90 } MMALDecodeContext;
91
92 // Assume decoder is guaranteed to produce output after at least this many
93 // packets (where each packet contains 1 frame).
94 #define MAX_DELAYED_FRAMES 16
95
96 static void ffmmal_poolref_unref(FFPoolRef *ref)
97 {
98 if (ref && avpriv_atomic_int_add_and_fetch(&ref->refcount, -1) == 0) {
99 mmal_pool_destroy(ref->pool);
100 av_free(ref);
101 }
102 }
103
104 static void ffmmal_release_frame(void *opaque, uint8_t *data)
105 {
106 FFBufferRef *ref = (void *)data;
107
108 mmal_buffer_header_release(ref->buffer);
109 ffmmal_poolref_unref(ref->pool);
110
111 av_free(ref);
112 }
113
114 // Setup frame with a new reference to buffer. The buffer must have been
115 // allocated from the given pool.
116 static int ffmmal_set_ref(AVFrame *frame, FFPoolRef *pool,
117 MMAL_BUFFER_HEADER_T *buffer)
118 {
119 FFBufferRef *ref = av_mallocz(sizeof(*ref));
120 if (!ref)
121 return AVERROR(ENOMEM);
122
123 ref->pool = pool;
124 ref->buffer = buffer;
125
126 frame->buf[0] = av_buffer_create((void *)ref, sizeof(*ref),
127 ffmmal_release_frame, NULL,
128 AV_BUFFER_FLAG_READONLY);
129 if (!frame->buf[0]) {
130 av_free(ref);
131 return AVERROR(ENOMEM);
132 }
133
134 avpriv_atomic_int_add_and_fetch(&ref->pool->refcount, 1);
135 mmal_buffer_header_acquire(buffer);
136
137 frame->format = AV_PIX_FMT_MMAL;
138 frame->data[3] = (uint8_t *)ref->buffer;
139 return 0;
140 }
141
142 static void ffmmal_stop_decoder(AVCodecContext *avctx)
143 {
144 MMALDecodeContext *ctx = avctx->priv_data;
145 MMAL_COMPONENT_T *decoder = ctx->decoder;
146 MMAL_BUFFER_HEADER_T *buffer;
147
148 mmal_port_disable(decoder->input[0]);
149 mmal_port_disable(decoder->output[0]);
150 mmal_port_disable(decoder->control);
151
152 mmal_port_flush(decoder->input[0]);
153 mmal_port_flush(decoder->output[0]);
154 mmal_port_flush(decoder->control);
155
156 while ((buffer = mmal_queue_get(ctx->queue_decoded_frames)))
157 mmal_buffer_header_release(buffer);
158
159 while (ctx->waiting_buffers) {
160 FFBufferEntry *buffer = ctx->waiting_buffers;
161
162 ctx->waiting_buffers = buffer->next;
163
164 av_buffer_unref(&buffer->ref);
165 av_free(buffer);
166 }
167 ctx->waiting_buffers_tail = NULL;
168
169 av_assert0(avpriv_atomic_int_get(&ctx->packets_buffered) == 0);
170
171 ctx->frames_output = ctx->eos_received = ctx->eos_sent = ctx->packets_sent = ctx->extradata_sent = 0;
172 }
173
174 static av_cold int ffmmal_close_decoder(AVCodecContext *avctx)
175 {
176 MMALDecodeContext *ctx = avctx->priv_data;
177
178 if (ctx->decoder)
179 ffmmal_stop_decoder(avctx);
180
181 mmal_component_destroy(ctx->decoder);
182 ctx->decoder = NULL;
183 mmal_queue_destroy(ctx->queue_decoded_frames);
184 mmal_pool_destroy(ctx->pool_in);
185 ffmmal_poolref_unref(ctx->pool_out);
186
187 mmal_vc_deinit();
188
189 return 0;
190 }
191
192 static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
193 {
194 AVCodecContext *avctx = (AVCodecContext*)port->userdata;
195 MMALDecodeContext *ctx = avctx->priv_data;
196
197 if (!buffer->cmd) {
198 FFBufferEntry *entry = buffer->user_data;
199 av_buffer_unref(&entry->ref);
200 if (entry->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
201 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, -1);
202 av_free(entry);
203 }
204 mmal_buffer_header_release(buffer);
205 }
206
207 static void output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
208 {
209 AVCodecContext *avctx = (AVCodecContext*)port->userdata;
210 MMALDecodeContext *ctx = avctx->priv_data;
211
212 mmal_queue_put(ctx->queue_decoded_frames, buffer);
213 }
214
215 static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
216 {
217 AVCodecContext *avctx = (AVCodecContext*)port->userdata;
218 MMAL_STATUS_T status;
219
220 if (buffer->cmd == MMAL_EVENT_ERROR) {
221 status = *(uint32_t *)buffer->data;
222 av_log(avctx, AV_LOG_ERROR, "MMAL error %d on control port\n", (int)status);
223 } else {
224 char s[20];
225 av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
226 av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on control port\n", s);
227 }
228
229 mmal_buffer_header_release(buffer);
230 }
231
232 // Feed free output buffers to the decoder.
233 static int ffmmal_fill_output_port(AVCodecContext *avctx)
234 {
235 MMALDecodeContext *ctx = avctx->priv_data;
236 MMAL_BUFFER_HEADER_T *buffer;
237 MMAL_STATUS_T status;
238
239 if (!ctx->pool_out)
240 return AVERROR_UNKNOWN; // format change code failed with OOM previously
241
242 while ((buffer = mmal_queue_get(ctx->pool_out->pool->queue))) {
243 if ((status = mmal_port_send_buffer(ctx->decoder->output[0], buffer))) {
244 mmal_buffer_header_release(buffer);
245 av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending output buffer.\n", (int)status);
246 return AVERROR_UNKNOWN;
247 }
248 }
249
250 return 0;
251 }
252
253 static enum AVColorSpace ffmmal_csp_to_av_csp(MMAL_FOURCC_T fourcc)
254 {
255 switch (fourcc) {
256 case MMAL_COLOR_SPACE_BT470_2_BG:
257 case MMAL_COLOR_SPACE_BT470_2_M:
258 case MMAL_COLOR_SPACE_ITUR_BT601: return AVCOL_SPC_BT470BG;
259 case MMAL_COLOR_SPACE_ITUR_BT709: return AVCOL_SPC_BT709;
260 case MMAL_COLOR_SPACE_FCC: return AVCOL_SPC_FCC;
261 case MMAL_COLOR_SPACE_SMPTE240M: return AVCOL_SPC_SMPTE240M;
262 default: return AVCOL_SPC_UNSPECIFIED;
263 }
264 }
265
266 static int ffmal_update_format(AVCodecContext *avctx)
267 {
268 MMALDecodeContext *ctx = avctx->priv_data;
269 MMAL_STATUS_T status;
270 int ret = 0;
271 MMAL_COMPONENT_T *decoder = ctx->decoder;
272 MMAL_ES_FORMAT_T *format_out = decoder->output[0]->format;
273
274 ffmmal_poolref_unref(ctx->pool_out);
275 if (!(ctx->pool_out = av_mallocz(sizeof(*ctx->pool_out)))) {
276 ret = AVERROR(ENOMEM);
277 goto fail;
278 }
279 ctx->pool_out->refcount = 1;
280
281 if (!format_out)
282 goto fail;
283
284 if ((status = mmal_port_parameter_set_uint32(decoder->output[0], MMAL_PARAMETER_EXTRA_BUFFERS, ctx->extra_buffers)))
285 goto fail;
286
287 if ((status = mmal_port_parameter_set_boolean(decoder->output[0], MMAL_PARAMETER_VIDEO_INTERPOLATE_TIMESTAMPS, 0)))
288 goto fail;
289
290 if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
291 format_out->encoding = MMAL_ENCODING_OPAQUE;
292 } else {
293 format_out->encoding_variant = format_out->encoding = MMAL_ENCODING_I420;
294 }
295
296 if ((status = mmal_port_format_commit(decoder->output[0])))
297 goto fail;
298
299 if ((ret = ff_set_dimensions(avctx, format_out->es->video.crop.x + format_out->es->video.crop.width,
300 format_out->es->video.crop.y + format_out->es->video.crop.height)) < 0)
301 goto fail;
302
303 if (format_out->es->video.par.num && format_out->es->video.par.den) {
304 avctx->sample_aspect_ratio.num = format_out->es->video.par.num;
305 avctx->sample_aspect_ratio.den = format_out->es->video.par.den;
306 }
307
308 avctx->colorspace = ffmmal_csp_to_av_csp(format_out->es->video.color_space);
309
310 decoder->output[0]->buffer_size =
311 FFMAX(decoder->output[0]->buffer_size_min, decoder->output[0]->buffer_size_recommended);
312 decoder->output[0]->buffer_num =
313 FFMAX(decoder->output[0]->buffer_num_min, decoder->output[0]->buffer_num_recommended) + ctx->extra_buffers;
314 ctx->pool_out->pool = mmal_pool_create(decoder->output[0]->buffer_num,
315 decoder->output[0]->buffer_size);
316 if (!ctx->pool_out->pool) {
317 ret = AVERROR(ENOMEM);
318 goto fail;
319 }
320
321 return 0;
322
323 fail:
324 return ret < 0 ? ret : AVERROR_UNKNOWN;
325 }
326
327 static av_cold int ffmmal_init_decoder(AVCodecContext *avctx)
328 {
329 MMALDecodeContext *ctx = avctx->priv_data;
330 MMAL_STATUS_T status;
331 MMAL_ES_FORMAT_T *format_in;
332 MMAL_COMPONENT_T *decoder;
333 int ret = 0;
334
335 bcm_host_init();
336
337 if (mmal_vc_init()) {
338 av_log(avctx, AV_LOG_ERROR, "Cannot initialize MMAL VC driver!\n");
339 return AVERROR(ENOSYS);
340 }
341
342 if ((ret = ff_get_format(avctx, avctx->codec->pix_fmts)) < 0)
343 return ret;
344
345 avctx->pix_fmt = ret;
346
347 if ((status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, &ctx->decoder)))
348 goto fail;
349
350 decoder = ctx->decoder;
351
352 format_in = decoder->input[0]->format;
353 format_in->type = MMAL_ES_TYPE_VIDEO;
354 switch (avctx->codec_id) {
355 case AV_CODEC_ID_MPEG2VIDEO:
356 format_in->encoding = MMAL_ENCODING_MP2V;
357 av_log(avctx, AV_LOG_DEBUG, "Use MMAL MP2V encoding\n");
358 break;
359 case AV_CODEC_ID_VC1:
360 format_in->encoding = MMAL_ENCODING_WVC1;
361 av_log(avctx, AV_LOG_DEBUG, "Use MMAL WVC1 encoding\n");
362 break;
363 case AV_CODEC_ID_H264:
364 default:
365 format_in->encoding = MMAL_ENCODING_H264;
366 av_log(avctx, AV_LOG_DEBUG, "Use MMAL H264 encoding\n");
367 break;
368 }
369 format_in->es->video.width = FFALIGN(avctx->width, 32);
370 format_in->es->video.height = FFALIGN(avctx->height, 16);
371 format_in->es->video.crop.width = avctx->width;
372 format_in->es->video.crop.height = avctx->height;
373 format_in->es->video.frame_rate.num = 24000;
374 format_in->es->video.frame_rate.den = 1001;
375 format_in->es->video.par.num = avctx->sample_aspect_ratio.num;
376 format_in->es->video.par.den = avctx->sample_aspect_ratio.den;
377 format_in->flags = MMAL_ES_FORMAT_FLAG_FRAMED;
378
379 if ((status = mmal_port_format_commit(decoder->input[0])))
380 goto fail;
381
382 decoder->input[0]->buffer_num =
383 FFMAX(decoder->input[0]->buffer_num_min, 20);
384 decoder->input[0]->buffer_size =
385 FFMAX(decoder->input[0]->buffer_size_min, 512 * 1024);
386 ctx->pool_in = mmal_pool_create(decoder->input[0]->buffer_num, 0);
387 if (!ctx->pool_in) {
388 ret = AVERROR(ENOMEM);
389 goto fail;
390 }
391
392 if ((ret = ffmal_update_format(avctx)) < 0)
393 goto fail;
394
395 ctx->queue_decoded_frames = mmal_queue_create();
396 if (!ctx->queue_decoded_frames)
397 goto fail;
398
399 decoder->input[0]->userdata = (void*)avctx;
400 decoder->output[0]->userdata = (void*)avctx;
401 decoder->control->userdata = (void*)avctx;
402
403 if ((status = mmal_port_enable(decoder->control, control_port_cb)))
404 goto fail;
405 if ((status = mmal_port_enable(decoder->input[0], input_callback)))
406 goto fail;
407 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
408 goto fail;
409
410 if ((status = mmal_component_enable(decoder)))
411 goto fail;
412
413 return 0;
414
415 fail:
416 ffmmal_close_decoder(avctx);
417 return ret < 0 ? ret : AVERROR_UNKNOWN;
418 }
419
420 static void ffmmal_flush(AVCodecContext *avctx)
421 {
422 MMALDecodeContext *ctx = avctx->priv_data;
423 MMAL_COMPONENT_T *decoder = ctx->decoder;
424 MMAL_STATUS_T status;
425
426 ffmmal_stop_decoder(avctx);
427
428 if ((status = mmal_port_enable(decoder->control, control_port_cb)))
429 goto fail;
430 if ((status = mmal_port_enable(decoder->input[0], input_callback)))
431 goto fail;
432 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
433 goto fail;
434
435 return;
436
437 fail:
438 av_log(avctx, AV_LOG_ERROR, "MMAL flush error: %i\n", (int)status);
439 }
440
441 // Split packets and add them to the waiting_buffers list. We don't queue them
442 // immediately, because it can happen that the decoder is temporarily blocked
443 // (due to us not reading/returning enough output buffers) and won't accept
444 // new input. (This wouldn't be an issue if MMAL input buffers always were
445 // complete frames - then the input buffer just would have to be big enough.)
446 // If is_extradata is set, send it as MMAL_BUFFER_HEADER_FLAG_CONFIG.
447 static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt,
448 int is_extradata)
449 {
450 MMALDecodeContext *ctx = avctx->priv_data;
451 AVBufferRef *buf = NULL;
452 int size = 0;
453 uint8_t *data = (uint8_t *)"";
454 uint8_t *start;
455 int ret = 0;
456
457 if (avpkt->size) {
458 if (avpkt->buf) {
459 buf = av_buffer_ref(avpkt->buf);
460 size = avpkt->size;
461 data = avpkt->data;
462 } else {
463 buf = av_buffer_alloc(avpkt->size);
464 if (buf) {
465 memcpy(buf->data, avpkt->data, avpkt->size);
466 size = buf->size;
467 data = buf->data;
468 }
469 }
470 if (!buf) {
471 ret = AVERROR(ENOMEM);
472 goto done;
473 }
474 if (!is_extradata)
475 ctx->packets_sent++;
476 } else {
477 if (!ctx->packets_sent) {
478 // Short-cut the flush logic to avoid upsetting MMAL.
479 ctx->eos_sent = 1;
480 ctx->eos_received = 1;
481 goto done;
482 }
483 }
484
485 start = data;
486
487 do {
488 FFBufferEntry *buffer = av_mallocz(sizeof(*buffer));
489 if (!buffer) {
490 ret = AVERROR(ENOMEM);
491 goto done;
492 }
493
494 buffer->data = data;
495 buffer->length = FFMIN(size, ctx->decoder->input[0]->buffer_size);
496
497 if (is_extradata)
498 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_CONFIG;
499
500 if (data == start)
501 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
502
503 data += buffer->length;
504 size -= buffer->length;
505
506 buffer->pts = avpkt->pts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->pts;
507 buffer->dts = avpkt->dts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->dts;
508
509 if (!size) {
510 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
511 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, 1);
512 }
513
514 if (!buffer->length) {
515 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
516 ctx->eos_sent = 1;
517 }
518
519 if (buf) {
520 buffer->ref = av_buffer_ref(buf);
521 if (!buffer->ref) {
522 av_free(buffer);
523 ret = AVERROR(ENOMEM);
524 goto done;
525 }
526 }
527
528 // Insert at end of the list
529 if (!ctx->waiting_buffers)
530 ctx->waiting_buffers = buffer;
531 if (ctx->waiting_buffers_tail)
532 ctx->waiting_buffers_tail->next = buffer;
533 ctx->waiting_buffers_tail = buffer;
534 } while (size);
535
536 done:
537 av_buffer_unref(&buf);
538 return ret;
539 }
540
541 // Move prepared/split packets from waiting_buffers to the MMAL decoder.
542 static int ffmmal_fill_input_port(AVCodecContext *avctx)
543 {
544 MMALDecodeContext *ctx = avctx->priv_data;
545
546 while (ctx->waiting_buffers) {
547 MMAL_BUFFER_HEADER_T *mbuffer;
548 FFBufferEntry *buffer;
549 MMAL_STATUS_T status;
550
551 mbuffer = mmal_queue_get(ctx->pool_in->queue);
552 if (!mbuffer)
553 return 0;
554
555 buffer = ctx->waiting_buffers;
556
557 mmal_buffer_header_reset(mbuffer);
558 mbuffer->cmd = 0;
559 mbuffer->pts = buffer->pts;
560 mbuffer->dts = buffer->dts;
561 mbuffer->flags = buffer->flags;
562 mbuffer->data = buffer->data;
563 mbuffer->length = buffer->length;
564 mbuffer->user_data = buffer;
565 mbuffer->alloc_size = ctx->decoder->input[0]->buffer_size;
566
567 // Remove from start of the list
568 ctx->waiting_buffers = buffer->next;
569 if (ctx->waiting_buffers_tail == buffer)
570 ctx->waiting_buffers_tail = NULL;
571
572 if ((status = mmal_port_send_buffer(ctx->decoder->input[0], mbuffer))) {
573 mmal_buffer_header_release(mbuffer);
574 av_buffer_unref(&buffer->ref);
575 if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
576 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, -1);
577 av_free(buffer);
578 }
579
580 if (status) {
581 av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending input\n", (int)status);
582 return AVERROR_UNKNOWN;
583 }
584 }
585
586 return 0;
587 }
588
589 static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame,
590 MMAL_BUFFER_HEADER_T *buffer)
591 {
592 MMALDecodeContext *ctx = avctx->priv_data;
593 int ret = 0;
594
595 if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
596 if (!ctx->pool_out)
597 return AVERROR_UNKNOWN; // format change code failed with OOM previously
598
599 if ((ret = ff_decode_frame_props(avctx, frame)) < 0)
600 goto done;
601
602 if ((ret = ffmmal_set_ref(frame, ctx->pool_out, buffer)) < 0)
603 goto done;
604 } else {
605 int w = FFALIGN(avctx->width, 32);
606 int h = FFALIGN(avctx->height, 16);
607 uint8_t *src[4];
608 int linesize[4];
609
610 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
611 goto done;
612
613 av_image_fill_arrays(src, linesize,
614 buffer->data + buffer->type->video.offset[0],
615 avctx->pix_fmt, w, h, 1);
616 av_image_copy(frame->data, frame->linesize, src, linesize,
617 avctx->pix_fmt, avctx->width, avctx->height);
618 }
619
620 frame->pkt_pts = buffer->pts == MMAL_TIME_UNKNOWN ? AV_NOPTS_VALUE : buffer->pts;
621 frame->pkt_dts = AV_NOPTS_VALUE;
622
623 done:
624 return ret;
625 }
626
627 // Fetch a decoded buffer and place it into the frame parameter.
628 static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
629 {
630 MMALDecodeContext *ctx = avctx->priv_data;
631 MMAL_BUFFER_HEADER_T *buffer = NULL;
632 MMAL_STATUS_T status = 0;
633 int ret = 0;
634
635 if (ctx->eos_received)
636 goto done;
637
638 while (1) {
639 // To ensure decoding in lockstep with a constant delay between fed packets
640 // and output frames, we always wait until an output buffer is available.
641 // Except during start we don't know after how many input packets the decoder
642 // is going to return the first buffer, and we can't distinguish decoder
643 // being busy from decoder waiting for input. So just poll at the start and
644 // keep feeding new data to the buffer.
645 // We are pretty sure the decoder will produce output if we sent more input
646 // frames than what a h264 decoder could logically delay. This avoids too
647 // excessive buffering.
648 // We also wait if we sent eos, but didn't receive it yet (think of decoding
649 // stream with a very low number of frames).
650 if (avpriv_atomic_int_get(&ctx->packets_buffered) > MAX_DELAYED_FRAMES ||
651 (ctx->packets_sent && ctx->eos_sent)) {
652 // MMAL will ignore broken input packets, which means the frame we
653 // expect here may never arrive. Dealing with this correctly is
654 // complicated, so here's a hack to avoid that it freezes forever
655 // in this unlikely situation.
656 buffer = mmal_queue_timedwait(ctx->queue_decoded_frames, 100);
657 if (!buffer) {
658 av_log(avctx, AV_LOG_ERROR, "Did not get output frame from MMAL.\n");
659 ret = AVERROR_UNKNOWN;
660 goto done;
661 }
662 } else {
663 buffer = mmal_queue_get(ctx->queue_decoded_frames);
664 if (!buffer)
665 goto done;
666 }
667
668 ctx->eos_received |= !!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS);
669 if (ctx->eos_received)
670 goto done;
671
672 if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED) {
673 MMAL_COMPONENT_T *decoder = ctx->decoder;
674 MMAL_EVENT_FORMAT_CHANGED_T *ev = mmal_event_format_changed_get(buffer);
675 MMAL_BUFFER_HEADER_T *stale_buffer;
676
677 av_log(avctx, AV_LOG_INFO, "Changing output format.\n");
678
679 if ((status = mmal_port_disable(decoder->output[0])))
680 goto done;
681
682 while ((stale_buffer = mmal_queue_get(ctx->queue_decoded_frames)))
683 mmal_buffer_header_release(stale_buffer);
684
685 mmal_format_copy(decoder->output[0]->format, ev->format);
686
687 if ((ret = ffmal_update_format(avctx)) < 0)
688 goto done;
689
690 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
691 goto done;
692
693 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
694 goto done;
695
696 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
697 goto done;
698
699 mmal_buffer_header_release(buffer);
700 continue;
701 } else if (buffer->cmd) {
702 char s[20];
703 av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
704 av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n", s);
705 goto done;
706 } else if (buffer->length == 0) {
707 // Unused output buffer that got drained after format change.
708 mmal_buffer_header_release(buffer);
709 continue;
710 }
711
712 ctx->frames_output++;
713
714 if ((ret = ffmal_copy_frame(avctx, frame, buffer)) < 0)
715 goto done;
716
717 *got_frame = 1;
718 break;
719 }
720
721 done:
722 if (buffer)
723 mmal_buffer_header_release(buffer);
724 if (status && ret >= 0)
725 ret = AVERROR_UNKNOWN;
726 return ret;
727 }
728
729 static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame,
730 AVPacket *avpkt)
731 {
732 MMALDecodeContext *ctx = avctx->priv_data;
733 AVFrame *frame = data;
734 int ret = 0;
735
736 if (avctx->extradata_size && !ctx->extradata_sent) {
737 AVPacket pkt = {0};
738 av_init_packet(&pkt);
739 pkt.data = avctx->extradata;
740 pkt.size = avctx->extradata_size;
741 ctx->extradata_sent = 1;
742 if ((ret = ffmmal_add_packet(avctx, &pkt, 1)) < 0)
743 return ret;
744 }
745
746 if ((ret = ffmmal_add_packet(avctx, avpkt, 0)) < 0)
747 return ret;
748
749 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
750 return ret;
751
752 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
753 return ret;
754
755 if ((ret = ffmmal_read_frame(avctx, frame, got_frame)) < 0)
756 return ret;
757
758 // ffmmal_read_frame() can block for a while. Since the decoder is
759 // asynchronous, it's a good idea to fill the ports again.
760
761 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
762 return ret;
763
764 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
765 return ret;
766
767 return ret;
768 }
769
770 AVHWAccel ff_h264_mmal_hwaccel = {
771 .name = "h264_mmal",
772 .type = AVMEDIA_TYPE_VIDEO,
773 .id = AV_CODEC_ID_H264,
774 .pix_fmt = AV_PIX_FMT_MMAL,
775 };
776
777 AVHWAccel ff_mpeg2_mmal_hwaccel = {
778 .name = "mpeg2_mmal",
779 .type = AVMEDIA_TYPE_VIDEO,
780 .id = AV_CODEC_ID_MPEG2VIDEO,
781 .pix_fmt = AV_PIX_FMT_MMAL,
782 };
783
784 AVHWAccel ff_vc1_mmal_hwaccel = {
785 .name = "vc1_mmal",
786 .type = AVMEDIA_TYPE_VIDEO,
787 .id = AV_CODEC_ID_VC1,
788 .pix_fmt = AV_PIX_FMT_MMAL,
789 };
790
791 static const AVOption options[]={
792 {"extra_buffers", "extra buffers", offsetof(MMALDecodeContext, extra_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
793 {NULL}
794 };
795
796 #define FFMMAL_DEC_CLASS(NAME) \
797 static const AVClass ffmmal_##NAME##_dec_class = { \
798 .class_name = "mmal_" #NAME "_dec", \
799 .option = options, \
800 .version = LIBAVUTIL_VERSION_INT, \
801 };
802
803 #define FFMMAL_DEC(NAME, ID) \
804 FFMMAL_DEC_CLASS(NAME) \
805 AVCodec ff_##NAME##_mmal_decoder = { \
806 .name = #NAME "_mmal", \
807 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (mmal)"), \
808 .type = AVMEDIA_TYPE_VIDEO, \
809 .id = ID, \
810 .priv_data_size = sizeof(MMALDecodeContext), \
811 .init = ffmmal_init_decoder, \
812 .close = ffmmal_close_decoder, \
813 .decode = ffmmal_decode, \
814 .flush = ffmmal_flush, \
815 .priv_class = &ffmmal_##NAME##_dec_class, \
816 .capabilities = AV_CODEC_CAP_DELAY, \
817 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
818 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL, \
819 AV_PIX_FMT_YUV420P, \
820 AV_PIX_FMT_NONE}, \
821 };
822
823 FFMMAL_DEC(h264, AV_CODEC_ID_H264)
824 FFMMAL_DEC(mpeg2, AV_CODEC_ID_MPEG2VIDEO)
825 FFMMAL_DEC(vc1, AV_CODEC_ID_VC1)