331a01791083d60e74f742042da09bac8336c57c
[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_H264:
360 default:
361 format_in->encoding = MMAL_ENCODING_H264;
362 av_log(avctx, AV_LOG_DEBUG, "Use MMAL H264 encoding\n");
363 break;
364 }
365 format_in->es->video.width = FFALIGN(avctx->width, 32);
366 format_in->es->video.height = FFALIGN(avctx->height, 16);
367 format_in->es->video.crop.width = avctx->width;
368 format_in->es->video.crop.height = avctx->height;
369 format_in->es->video.frame_rate.num = 24000;
370 format_in->es->video.frame_rate.den = 1001;
371 format_in->es->video.par.num = avctx->sample_aspect_ratio.num;
372 format_in->es->video.par.den = avctx->sample_aspect_ratio.den;
373 format_in->flags = MMAL_ES_FORMAT_FLAG_FRAMED;
374
375 if ((status = mmal_port_format_commit(decoder->input[0])))
376 goto fail;
377
378 decoder->input[0]->buffer_num =
379 FFMAX(decoder->input[0]->buffer_num_min, 20);
380 decoder->input[0]->buffer_size =
381 FFMAX(decoder->input[0]->buffer_size_min, 512 * 1024);
382 ctx->pool_in = mmal_pool_create(decoder->input[0]->buffer_num, 0);
383 if (!ctx->pool_in) {
384 ret = AVERROR(ENOMEM);
385 goto fail;
386 }
387
388 if ((ret = ffmal_update_format(avctx)) < 0)
389 goto fail;
390
391 ctx->queue_decoded_frames = mmal_queue_create();
392 if (!ctx->queue_decoded_frames)
393 goto fail;
394
395 decoder->input[0]->userdata = (void*)avctx;
396 decoder->output[0]->userdata = (void*)avctx;
397 decoder->control->userdata = (void*)avctx;
398
399 if ((status = mmal_port_enable(decoder->control, control_port_cb)))
400 goto fail;
401 if ((status = mmal_port_enable(decoder->input[0], input_callback)))
402 goto fail;
403 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
404 goto fail;
405
406 if ((status = mmal_component_enable(decoder)))
407 goto fail;
408
409 return 0;
410
411 fail:
412 ffmmal_close_decoder(avctx);
413 return ret < 0 ? ret : AVERROR_UNKNOWN;
414 }
415
416 static void ffmmal_flush(AVCodecContext *avctx)
417 {
418 MMALDecodeContext *ctx = avctx->priv_data;
419 MMAL_COMPONENT_T *decoder = ctx->decoder;
420 MMAL_STATUS_T status;
421
422 ffmmal_stop_decoder(avctx);
423
424 if ((status = mmal_port_enable(decoder->control, control_port_cb)))
425 goto fail;
426 if ((status = mmal_port_enable(decoder->input[0], input_callback)))
427 goto fail;
428 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
429 goto fail;
430
431 return;
432
433 fail:
434 av_log(avctx, AV_LOG_ERROR, "MMAL flush error: %i\n", (int)status);
435 }
436
437 // Split packets and add them to the waiting_buffers list. We don't queue them
438 // immediately, because it can happen that the decoder is temporarily blocked
439 // (due to us not reading/returning enough output buffers) and won't accept
440 // new input. (This wouldn't be an issue if MMAL input buffers always were
441 // complete frames - then the input buffer just would have to be big enough.)
442 // If is_extradata is set, send it as MMAL_BUFFER_HEADER_FLAG_CONFIG.
443 static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt,
444 int is_extradata)
445 {
446 MMALDecodeContext *ctx = avctx->priv_data;
447 AVBufferRef *buf = NULL;
448 int size = 0;
449 uint8_t *data = (uint8_t *)"";
450 uint8_t *start;
451 int ret = 0;
452
453 if (avpkt->size) {
454 if (avpkt->buf) {
455 buf = av_buffer_ref(avpkt->buf);
456 size = avpkt->size;
457 data = avpkt->data;
458 } else {
459 buf = av_buffer_alloc(avpkt->size);
460 if (buf) {
461 memcpy(buf->data, avpkt->data, avpkt->size);
462 size = buf->size;
463 data = buf->data;
464 }
465 }
466 if (!buf) {
467 ret = AVERROR(ENOMEM);
468 goto done;
469 }
470 if (!is_extradata)
471 ctx->packets_sent++;
472 } else {
473 if (!ctx->packets_sent) {
474 // Short-cut the flush logic to avoid upsetting MMAL.
475 ctx->eos_sent = 1;
476 ctx->eos_received = 1;
477 goto done;
478 }
479 }
480
481 start = data;
482
483 do {
484 FFBufferEntry *buffer = av_mallocz(sizeof(*buffer));
485 if (!buffer) {
486 ret = AVERROR(ENOMEM);
487 goto done;
488 }
489
490 buffer->data = data;
491 buffer->length = FFMIN(size, ctx->decoder->input[0]->buffer_size);
492
493 if (is_extradata)
494 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_CONFIG;
495
496 if (data == start)
497 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
498
499 data += buffer->length;
500 size -= buffer->length;
501
502 buffer->pts = avpkt->pts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->pts;
503 buffer->dts = avpkt->dts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->dts;
504
505 if (!size) {
506 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
507 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, 1);
508 }
509
510 if (!buffer->length) {
511 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
512 ctx->eos_sent = 1;
513 }
514
515 if (buf) {
516 buffer->ref = av_buffer_ref(buf);
517 if (!buffer->ref) {
518 av_free(buffer);
519 ret = AVERROR(ENOMEM);
520 goto done;
521 }
522 }
523
524 // Insert at end of the list
525 if (!ctx->waiting_buffers)
526 ctx->waiting_buffers = buffer;
527 if (ctx->waiting_buffers_tail)
528 ctx->waiting_buffers_tail->next = buffer;
529 ctx->waiting_buffers_tail = buffer;
530 } while (size);
531
532 done:
533 av_buffer_unref(&buf);
534 return ret;
535 }
536
537 // Move prepared/split packets from waiting_buffers to the MMAL decoder.
538 static int ffmmal_fill_input_port(AVCodecContext *avctx)
539 {
540 MMALDecodeContext *ctx = avctx->priv_data;
541
542 while (ctx->waiting_buffers) {
543 MMAL_BUFFER_HEADER_T *mbuffer;
544 FFBufferEntry *buffer;
545 MMAL_STATUS_T status;
546
547 mbuffer = mmal_queue_get(ctx->pool_in->queue);
548 if (!mbuffer)
549 return 0;
550
551 buffer = ctx->waiting_buffers;
552
553 mmal_buffer_header_reset(mbuffer);
554 mbuffer->cmd = 0;
555 mbuffer->pts = buffer->pts;
556 mbuffer->dts = buffer->dts;
557 mbuffer->flags = buffer->flags;
558 mbuffer->data = buffer->data;
559 mbuffer->length = buffer->length;
560 mbuffer->user_data = buffer;
561 mbuffer->alloc_size = ctx->decoder->input[0]->buffer_size;
562
563 // Remove from start of the list
564 ctx->waiting_buffers = buffer->next;
565 if (ctx->waiting_buffers_tail == buffer)
566 ctx->waiting_buffers_tail = NULL;
567
568 if ((status = mmal_port_send_buffer(ctx->decoder->input[0], mbuffer))) {
569 mmal_buffer_header_release(mbuffer);
570 av_buffer_unref(&buffer->ref);
571 if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
572 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, -1);
573 av_free(buffer);
574 }
575
576 if (status) {
577 av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending input\n", (int)status);
578 return AVERROR_UNKNOWN;
579 }
580 }
581
582 return 0;
583 }
584
585 static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame,
586 MMAL_BUFFER_HEADER_T *buffer)
587 {
588 MMALDecodeContext *ctx = avctx->priv_data;
589 int ret = 0;
590
591 if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
592 if (!ctx->pool_out)
593 return AVERROR_UNKNOWN; // format change code failed with OOM previously
594
595 if ((ret = ff_decode_frame_props(avctx, frame)) < 0)
596 goto done;
597
598 if ((ret = ffmmal_set_ref(frame, ctx->pool_out, buffer)) < 0)
599 goto done;
600 } else {
601 int w = FFALIGN(avctx->width, 32);
602 int h = FFALIGN(avctx->height, 16);
603 uint8_t *src[4];
604 int linesize[4];
605
606 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
607 goto done;
608
609 av_image_fill_arrays(src, linesize,
610 buffer->data + buffer->type->video.offset[0],
611 avctx->pix_fmt, w, h, 1);
612 av_image_copy(frame->data, frame->linesize, src, linesize,
613 avctx->pix_fmt, avctx->width, avctx->height);
614 }
615
616 frame->pkt_pts = buffer->pts == MMAL_TIME_UNKNOWN ? AV_NOPTS_VALUE : buffer->pts;
617 frame->pkt_dts = AV_NOPTS_VALUE;
618
619 done:
620 return ret;
621 }
622
623 // Fetch a decoded buffer and place it into the frame parameter.
624 static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
625 {
626 MMALDecodeContext *ctx = avctx->priv_data;
627 MMAL_BUFFER_HEADER_T *buffer = NULL;
628 MMAL_STATUS_T status = 0;
629 int ret = 0;
630
631 if (ctx->eos_received)
632 goto done;
633
634 while (1) {
635 // To ensure decoding in lockstep with a constant delay between fed packets
636 // and output frames, we always wait until an output buffer is available.
637 // Except during start we don't know after how many input packets the decoder
638 // is going to return the first buffer, and we can't distinguish decoder
639 // being busy from decoder waiting for input. So just poll at the start and
640 // keep feeding new data to the buffer.
641 // We are pretty sure the decoder will produce output if we sent more input
642 // frames than what a h264 decoder could logically delay. This avoids too
643 // excessive buffering.
644 // We also wait if we sent eos, but didn't receive it yet (think of decoding
645 // stream with a very low number of frames).
646 if (avpriv_atomic_int_get(&ctx->packets_buffered) > MAX_DELAYED_FRAMES ||
647 (ctx->packets_sent && ctx->eos_sent)) {
648 // MMAL will ignore broken input packets, which means the frame we
649 // expect here may never arrive. Dealing with this correctly is
650 // complicated, so here's a hack to avoid that it freezes forever
651 // in this unlikely situation.
652 buffer = mmal_queue_timedwait(ctx->queue_decoded_frames, 100);
653 if (!buffer) {
654 av_log(avctx, AV_LOG_ERROR, "Did not get output frame from MMAL.\n");
655 ret = AVERROR_UNKNOWN;
656 goto done;
657 }
658 } else {
659 buffer = mmal_queue_get(ctx->queue_decoded_frames);
660 if (!buffer)
661 goto done;
662 }
663
664 ctx->eos_received |= !!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS);
665 if (ctx->eos_received)
666 goto done;
667
668 if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED) {
669 MMAL_COMPONENT_T *decoder = ctx->decoder;
670 MMAL_EVENT_FORMAT_CHANGED_T *ev = mmal_event_format_changed_get(buffer);
671 MMAL_BUFFER_HEADER_T *stale_buffer;
672
673 av_log(avctx, AV_LOG_INFO, "Changing output format.\n");
674
675 if ((status = mmal_port_disable(decoder->output[0])))
676 goto done;
677
678 while ((stale_buffer = mmal_queue_get(ctx->queue_decoded_frames)))
679 mmal_buffer_header_release(stale_buffer);
680
681 mmal_format_copy(decoder->output[0]->format, ev->format);
682
683 if ((ret = ffmal_update_format(avctx)) < 0)
684 goto done;
685
686 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
687 goto done;
688
689 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
690 goto done;
691
692 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
693 goto done;
694
695 mmal_buffer_header_release(buffer);
696 continue;
697 } else if (buffer->cmd) {
698 char s[20];
699 av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
700 av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n", s);
701 goto done;
702 } else if (buffer->length == 0) {
703 // Unused output buffer that got drained after format change.
704 mmal_buffer_header_release(buffer);
705 continue;
706 }
707
708 ctx->frames_output++;
709
710 if ((ret = ffmal_copy_frame(avctx, frame, buffer)) < 0)
711 goto done;
712
713 *got_frame = 1;
714 break;
715 }
716
717 done:
718 if (buffer)
719 mmal_buffer_header_release(buffer);
720 if (status && ret >= 0)
721 ret = AVERROR_UNKNOWN;
722 return ret;
723 }
724
725 static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame,
726 AVPacket *avpkt)
727 {
728 MMALDecodeContext *ctx = avctx->priv_data;
729 AVFrame *frame = data;
730 int ret = 0;
731
732 if (avctx->extradata_size && !ctx->extradata_sent) {
733 AVPacket pkt = {0};
734 av_init_packet(&pkt);
735 pkt.data = avctx->extradata;
736 pkt.size = avctx->extradata_size;
737 ctx->extradata_sent = 1;
738 if ((ret = ffmmal_add_packet(avctx, &pkt, 1)) < 0)
739 return ret;
740 }
741
742 if ((ret = ffmmal_add_packet(avctx, avpkt, 0)) < 0)
743 return ret;
744
745 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
746 return ret;
747
748 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
749 return ret;
750
751 if ((ret = ffmmal_read_frame(avctx, frame, got_frame)) < 0)
752 return ret;
753
754 // ffmmal_read_frame() can block for a while. Since the decoder is
755 // asynchronous, it's a good idea to fill the ports again.
756
757 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
758 return ret;
759
760 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
761 return ret;
762
763 return ret;
764 }
765
766 AVHWAccel ff_h264_mmal_hwaccel = {
767 .name = "h264_mmal",
768 .type = AVMEDIA_TYPE_VIDEO,
769 .id = AV_CODEC_ID_H264,
770 .pix_fmt = AV_PIX_FMT_MMAL,
771 };
772
773 AVHWAccel ff_mpeg2_mmal_hwaccel = {
774 .name = "mpeg2_mmal",
775 .type = AVMEDIA_TYPE_VIDEO,
776 .id = AV_CODEC_ID_MPEG2VIDEO,
777 .pix_fmt = AV_PIX_FMT_MMAL,
778 };
779
780 static const AVOption options[]={
781 {"extra_buffers", "extra buffers", offsetof(MMALDecodeContext, extra_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
782 {NULL}
783 };
784
785 #define FFMMAL_DEC_CLASS(NAME) \
786 static const AVClass ffmmal_##NAME##_dec_class = { \
787 .class_name = "mmal_" #NAME "_dec", \
788 .option = options, \
789 .version = LIBAVUTIL_VERSION_INT, \
790 };
791
792 #define FFMMAL_DEC(NAME, ID) \
793 FFMMAL_DEC_CLASS(NAME) \
794 AVCodec ff_##NAME##_mmal_decoder = { \
795 .name = #NAME "_mmal", \
796 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (mmal)"), \
797 .type = AVMEDIA_TYPE_VIDEO, \
798 .id = ID, \
799 .priv_data_size = sizeof(MMALDecodeContext), \
800 .init = ffmmal_init_decoder, \
801 .close = ffmmal_close_decoder, \
802 .decode = ffmmal_decode, \
803 .flush = ffmmal_flush, \
804 .priv_class = &ffmmal_##NAME##_dec_class, \
805 .capabilities = AV_CODEC_CAP_DELAY, \
806 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
807 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL, \
808 AV_PIX_FMT_YUV420P, \
809 AV_PIX_FMT_NONE}, \
810 };
811
812 FFMMAL_DEC(h264, AV_CODEC_ID_H264)
813 FFMMAL_DEC(mpeg2, AV_CODEC_ID_MPEG2VIDEO)