3 * Copyright (c) 2015 Rodger Combs
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 #include <stdatomic.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"
45 typedef struct FFBufferEntry {
51 struct FFBufferEntry *next;
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 {
62 typedef struct FFBufferRef {
63 MMAL_BUFFER_HEADER_T *buffer;
67 typedef struct MMALDecodeContext {
70 int extra_decoder_buffers;
72 MMAL_COMPONENT_T *decoder;
73 MMAL_QUEUE_T *queue_decoded_frames;
77 // Waiting input packets. Because the libavcodec API requires decoding and
78 // returning packets in lockstep, it can happen that queue_decoded_frames
79 // contains almost all surfaces - then the decoder input queue can quickly
80 // fill up and won't accept new input either. Without consuming input, the
81 // libavcodec API can't return new frames, and we have a logical deadlock.
82 // This is avoided by queuing such buffers here.
83 FFBufferEntry *waiting_buffers, *waiting_buffers_tail;
86 atomic_int packets_buffered;
87 int64_t frames_output;
95 // Assume decoder is guaranteed to produce output after at least this many
96 // packets (where each packet contains 1 frame).
97 #define MAX_DELAYED_FRAMES 16
99 static void ffmmal_poolref_unref(FFPoolRef *ref)
102 atomic_fetch_add_explicit(&ref->refcount, -1, memory_order_acq_rel) == 1) {
103 mmal_pool_destroy(ref->pool);
108 static void ffmmal_release_frame(void *opaque, uint8_t *data)
110 FFBufferRef *ref = (void *)data;
112 mmal_buffer_header_release(ref->buffer);
113 ffmmal_poolref_unref(ref->pool);
118 // Setup frame with a new reference to buffer. The buffer must have been
119 // allocated from the given pool.
120 static int ffmmal_set_ref(AVFrame *frame, FFPoolRef *pool,
121 MMAL_BUFFER_HEADER_T *buffer)
123 FFBufferRef *ref = av_mallocz(sizeof(*ref));
125 return AVERROR(ENOMEM);
128 ref->buffer = buffer;
130 frame->buf[0] = av_buffer_create((void *)ref, sizeof(*ref),
131 ffmmal_release_frame, NULL,
132 AV_BUFFER_FLAG_READONLY);
133 if (!frame->buf[0]) {
135 return AVERROR(ENOMEM);
138 atomic_fetch_add_explicit(&ref->pool->refcount, 1, memory_order_relaxed);
139 mmal_buffer_header_acquire(buffer);
141 frame->format = AV_PIX_FMT_MMAL;
142 frame->data[3] = (uint8_t *)ref->buffer;
146 static void ffmmal_stop_decoder(AVCodecContext *avctx)
148 MMALDecodeContext *ctx = avctx->priv_data;
149 MMAL_COMPONENT_T *decoder = ctx->decoder;
150 MMAL_BUFFER_HEADER_T *buffer;
152 mmal_port_disable(decoder->input[0]);
153 mmal_port_disable(decoder->output[0]);
154 mmal_port_disable(decoder->control);
156 mmal_port_flush(decoder->input[0]);
157 mmal_port_flush(decoder->output[0]);
158 mmal_port_flush(decoder->control);
160 while ((buffer = mmal_queue_get(ctx->queue_decoded_frames)))
161 mmal_buffer_header_release(buffer);
163 while (ctx->waiting_buffers) {
164 FFBufferEntry *buffer = ctx->waiting_buffers;
166 ctx->waiting_buffers = buffer->next;
168 if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
169 atomic_fetch_add(&ctx->packets_buffered, -1);
171 av_buffer_unref(&buffer->ref);
174 ctx->waiting_buffers_tail = NULL;
176 av_assert0(atomic_load(&ctx->packets_buffered) == 0);
178 ctx->frames_output = ctx->eos_received = ctx->eos_sent = ctx->packets_sent = ctx->extradata_sent = 0;
181 static av_cold int ffmmal_close_decoder(AVCodecContext *avctx)
183 MMALDecodeContext *ctx = avctx->priv_data;
186 ffmmal_stop_decoder(avctx);
188 mmal_component_destroy(ctx->decoder);
190 mmal_queue_destroy(ctx->queue_decoded_frames);
191 mmal_pool_destroy(ctx->pool_in);
192 ffmmal_poolref_unref(ctx->pool_out);
199 static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
201 AVCodecContext *avctx = (AVCodecContext*)port->userdata;
202 MMALDecodeContext *ctx = avctx->priv_data;
205 FFBufferEntry *entry = buffer->user_data;
206 av_buffer_unref(&entry->ref);
207 if (entry->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
208 atomic_fetch_add(&ctx->packets_buffered, -1);
211 mmal_buffer_header_release(buffer);
214 static void output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
216 AVCodecContext *avctx = (AVCodecContext*)port->userdata;
217 MMALDecodeContext *ctx = avctx->priv_data;
219 mmal_queue_put(ctx->queue_decoded_frames, buffer);
222 static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
224 AVCodecContext *avctx = (AVCodecContext*)port->userdata;
225 MMAL_STATUS_T status;
227 if (buffer->cmd == MMAL_EVENT_ERROR) {
228 status = *(uint32_t *)buffer->data;
229 av_log(avctx, AV_LOG_ERROR, "MMAL error %d on control port\n", (int)status);
232 av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
233 av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on control port\n", s);
236 mmal_buffer_header_release(buffer);
239 // Feed free output buffers to the decoder.
240 static int ffmmal_fill_output_port(AVCodecContext *avctx)
242 MMALDecodeContext *ctx = avctx->priv_data;
243 MMAL_BUFFER_HEADER_T *buffer;
244 MMAL_STATUS_T status;
247 return AVERROR_UNKNOWN; // format change code failed with OOM previously
249 while ((buffer = mmal_queue_get(ctx->pool_out->pool->queue))) {
250 if ((status = mmal_port_send_buffer(ctx->decoder->output[0], buffer))) {
251 mmal_buffer_header_release(buffer);
252 av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending output buffer.\n", (int)status);
253 return AVERROR_UNKNOWN;
260 static enum AVColorSpace ffmmal_csp_to_av_csp(MMAL_FOURCC_T fourcc)
263 case MMAL_COLOR_SPACE_BT470_2_BG:
264 case MMAL_COLOR_SPACE_BT470_2_M:
265 case MMAL_COLOR_SPACE_ITUR_BT601: return AVCOL_SPC_BT470BG;
266 case MMAL_COLOR_SPACE_ITUR_BT709: return AVCOL_SPC_BT709;
267 case MMAL_COLOR_SPACE_FCC: return AVCOL_SPC_FCC;
268 case MMAL_COLOR_SPACE_SMPTE240M: return AVCOL_SPC_SMPTE240M;
269 default: return AVCOL_SPC_UNSPECIFIED;
273 static int ffmal_update_format(AVCodecContext *avctx)
275 MMALDecodeContext *ctx = avctx->priv_data;
276 MMAL_STATUS_T status;
278 MMAL_COMPONENT_T *decoder = ctx->decoder;
279 MMAL_ES_FORMAT_T *format_out = decoder->output[0]->format;
280 MMAL_PARAMETER_VIDEO_INTERLACE_TYPE_T interlace_type;
282 ffmmal_poolref_unref(ctx->pool_out);
283 if (!(ctx->pool_out = av_mallocz(sizeof(*ctx->pool_out)))) {
284 ret = AVERROR(ENOMEM);
287 atomic_store(&ctx->pool_out->refcount, 1);
292 if ((status = mmal_port_parameter_set_uint32(decoder->output[0], MMAL_PARAMETER_EXTRA_BUFFERS, ctx->extra_buffers)))
295 if ((status = mmal_port_parameter_set_boolean(decoder->output[0], MMAL_PARAMETER_VIDEO_INTERPOLATE_TIMESTAMPS, 0)))
298 if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
299 format_out->encoding = MMAL_ENCODING_OPAQUE;
301 format_out->encoding_variant = format_out->encoding = MMAL_ENCODING_I420;
304 if ((status = mmal_port_format_commit(decoder->output[0])))
307 interlace_type.hdr.id = MMAL_PARAMETER_VIDEO_INTERLACE_TYPE;
308 interlace_type.hdr.size = sizeof(MMAL_PARAMETER_VIDEO_INTERLACE_TYPE_T);
309 status = mmal_port_parameter_get(decoder->output[0], &interlace_type.hdr);
310 if (status != MMAL_SUCCESS) {
311 av_log(avctx, AV_LOG_ERROR, "Cannot read MMAL interlace information!\n");
313 ctx->interlaced_frame = (interlace_type.eMode != MMAL_InterlaceProgressive);
314 ctx->top_field_first = (interlace_type.eMode == MMAL_InterlaceFieldsInterleavedUpperFirst);
317 if ((ret = ff_set_dimensions(avctx, format_out->es->video.crop.x + format_out->es->video.crop.width,
318 format_out->es->video.crop.y + format_out->es->video.crop.height)) < 0)
321 if (format_out->es->video.par.num && format_out->es->video.par.den) {
322 avctx->sample_aspect_ratio.num = format_out->es->video.par.num;
323 avctx->sample_aspect_ratio.den = format_out->es->video.par.den;
325 if (format_out->es->video.frame_rate.num && format_out->es->video.frame_rate.den) {
326 avctx->framerate.num = format_out->es->video.frame_rate.num;
327 avctx->framerate.den = format_out->es->video.frame_rate.den;
330 avctx->colorspace = ffmmal_csp_to_av_csp(format_out->es->video.color_space);
332 decoder->output[0]->buffer_size =
333 FFMAX(decoder->output[0]->buffer_size_min, decoder->output[0]->buffer_size_recommended);
334 decoder->output[0]->buffer_num =
335 FFMAX(decoder->output[0]->buffer_num_min, decoder->output[0]->buffer_num_recommended) + ctx->extra_buffers;
336 ctx->pool_out->pool = mmal_pool_create(decoder->output[0]->buffer_num,
337 decoder->output[0]->buffer_size);
338 if (!ctx->pool_out->pool) {
339 ret = AVERROR(ENOMEM);
346 return ret < 0 ? ret : AVERROR_UNKNOWN;
349 static av_cold int ffmmal_init_decoder(AVCodecContext *avctx)
351 MMALDecodeContext *ctx = avctx->priv_data;
352 MMAL_STATUS_T status;
353 MMAL_ES_FORMAT_T *format_in;
354 MMAL_COMPONENT_T *decoder;
360 if (mmal_vc_init()) {
361 av_log(avctx, AV_LOG_ERROR, "Cannot initialize MMAL VC driver!\n");
362 return AVERROR(ENOSYS);
365 if ((ret = ff_get_format(avctx, avctx->codec->pix_fmts)) < 0)
368 avctx->pix_fmt = ret;
370 if ((status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, &ctx->decoder)))
373 decoder = ctx->decoder;
375 format_in = decoder->input[0]->format;
376 format_in->type = MMAL_ES_TYPE_VIDEO;
377 switch (avctx->codec_id) {
378 case AV_CODEC_ID_MPEG2VIDEO:
379 format_in->encoding = MMAL_ENCODING_MP2V;
381 case AV_CODEC_ID_MPEG4:
382 format_in->encoding = MMAL_ENCODING_MP4V;
384 case AV_CODEC_ID_VC1:
385 format_in->encoding = MMAL_ENCODING_WVC1;
387 case AV_CODEC_ID_H264:
389 format_in->encoding = MMAL_ENCODING_H264;
392 format_in->es->video.width = FFALIGN(avctx->width, 32);
393 format_in->es->video.height = FFALIGN(avctx->height, 16);
394 format_in->es->video.crop.width = avctx->width;
395 format_in->es->video.crop.height = avctx->height;
396 format_in->es->video.frame_rate.num = 24000;
397 format_in->es->video.frame_rate.den = 1001;
398 format_in->es->video.par.num = avctx->sample_aspect_ratio.num;
399 format_in->es->video.par.den = avctx->sample_aspect_ratio.den;
400 format_in->flags = MMAL_ES_FORMAT_FLAG_FRAMED;
402 av_get_codec_tag_string(tmp, sizeof(tmp), format_in->encoding);
403 av_log(avctx, AV_LOG_DEBUG, "Using MMAL %s encoding.\n", tmp);
405 #if HAVE_MMAL_PARAMETER_VIDEO_MAX_NUM_CALLBACKS
406 if (mmal_port_parameter_set_uint32(decoder->input[0], MMAL_PARAMETER_VIDEO_MAX_NUM_CALLBACKS,
407 -1 - ctx->extra_decoder_buffers)) {
408 av_log(avctx, AV_LOG_WARNING, "Could not set input buffering limit.\n");
412 if ((status = mmal_port_format_commit(decoder->input[0])))
415 decoder->input[0]->buffer_num =
416 FFMAX(decoder->input[0]->buffer_num_min, 20);
417 decoder->input[0]->buffer_size =
418 FFMAX(decoder->input[0]->buffer_size_min, 512 * 1024);
419 ctx->pool_in = mmal_pool_create(decoder->input[0]->buffer_num, 0);
421 ret = AVERROR(ENOMEM);
425 if ((ret = ffmal_update_format(avctx)) < 0)
428 ctx->queue_decoded_frames = mmal_queue_create();
429 if (!ctx->queue_decoded_frames)
432 decoder->input[0]->userdata = (void*)avctx;
433 decoder->output[0]->userdata = (void*)avctx;
434 decoder->control->userdata = (void*)avctx;
436 if ((status = mmal_port_enable(decoder->control, control_port_cb)))
438 if ((status = mmal_port_enable(decoder->input[0], input_callback)))
440 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
443 if ((status = mmal_component_enable(decoder)))
449 ffmmal_close_decoder(avctx);
450 return ret < 0 ? ret : AVERROR_UNKNOWN;
453 static void ffmmal_flush(AVCodecContext *avctx)
455 MMALDecodeContext *ctx = avctx->priv_data;
456 MMAL_COMPONENT_T *decoder = ctx->decoder;
457 MMAL_STATUS_T status;
459 ffmmal_stop_decoder(avctx);
461 if ((status = mmal_port_enable(decoder->control, control_port_cb)))
463 if ((status = mmal_port_enable(decoder->input[0], input_callback)))
465 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
471 av_log(avctx, AV_LOG_ERROR, "MMAL flush error: %i\n", (int)status);
474 // Split packets and add them to the waiting_buffers list. We don't queue them
475 // immediately, because it can happen that the decoder is temporarily blocked
476 // (due to us not reading/returning enough output buffers) and won't accept
477 // new input. (This wouldn't be an issue if MMAL input buffers always were
478 // complete frames - then the input buffer just would have to be big enough.)
479 // If is_extradata is set, send it as MMAL_BUFFER_HEADER_FLAG_CONFIG.
480 static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt,
483 MMALDecodeContext *ctx = avctx->priv_data;
484 AVBufferRef *buf = NULL;
486 uint8_t *data = (uint8_t *)"";
492 buf = av_buffer_ref(avpkt->buf);
496 buf = av_buffer_alloc(avpkt->size);
498 memcpy(buf->data, avpkt->data, avpkt->size);
504 ret = AVERROR(ENOMEM);
512 if (!ctx->packets_sent) {
513 // Short-cut the flush logic to avoid upsetting MMAL.
515 ctx->eos_received = 1;
523 FFBufferEntry *buffer = av_mallocz(sizeof(*buffer));
525 ret = AVERROR(ENOMEM);
530 buffer->length = FFMIN(size, ctx->decoder->input[0]->buffer_size);
533 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_CONFIG;
536 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
538 data += buffer->length;
539 size -= buffer->length;
541 buffer->pts = avpkt->pts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->pts;
542 buffer->dts = avpkt->dts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->dts;
545 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
546 atomic_fetch_add(&ctx->packets_buffered, 1);
549 if (!buffer->length) {
550 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
555 buffer->ref = av_buffer_ref(buf);
558 ret = AVERROR(ENOMEM);
563 // Insert at end of the list
564 if (!ctx->waiting_buffers)
565 ctx->waiting_buffers = buffer;
566 if (ctx->waiting_buffers_tail)
567 ctx->waiting_buffers_tail->next = buffer;
568 ctx->waiting_buffers_tail = buffer;
572 av_buffer_unref(&buf);
576 // Move prepared/split packets from waiting_buffers to the MMAL decoder.
577 static int ffmmal_fill_input_port(AVCodecContext *avctx)
579 MMALDecodeContext *ctx = avctx->priv_data;
581 while (ctx->waiting_buffers) {
582 MMAL_BUFFER_HEADER_T *mbuffer;
583 FFBufferEntry *buffer;
584 MMAL_STATUS_T status;
586 mbuffer = mmal_queue_get(ctx->pool_in->queue);
590 buffer = ctx->waiting_buffers;
592 mmal_buffer_header_reset(mbuffer);
594 mbuffer->pts = buffer->pts;
595 mbuffer->dts = buffer->dts;
596 mbuffer->flags = buffer->flags;
597 mbuffer->data = buffer->data;
598 mbuffer->length = buffer->length;
599 mbuffer->user_data = buffer;
600 mbuffer->alloc_size = ctx->decoder->input[0]->buffer_size;
602 // Remove from start of the list
603 ctx->waiting_buffers = buffer->next;
604 if (ctx->waiting_buffers_tail == buffer)
605 ctx->waiting_buffers_tail = NULL;
607 if ((status = mmal_port_send_buffer(ctx->decoder->input[0], mbuffer))) {
608 mmal_buffer_header_release(mbuffer);
609 av_buffer_unref(&buffer->ref);
610 if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
611 atomic_fetch_add(&ctx->packets_buffered, -1);
616 av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending input\n", (int)status);
617 return AVERROR_UNKNOWN;
624 static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame,
625 MMAL_BUFFER_HEADER_T *buffer)
627 MMALDecodeContext *ctx = avctx->priv_data;
630 frame->interlaced_frame = ctx->interlaced_frame;
631 frame->top_field_first = ctx->top_field_first;
633 if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
635 return AVERROR_UNKNOWN; // format change code failed with OOM previously
637 if ((ret = ff_decode_frame_props(avctx, frame)) < 0)
640 if ((ret = ffmmal_set_ref(frame, ctx->pool_out, buffer)) < 0)
643 int w = FFALIGN(avctx->width, 32);
644 int h = FFALIGN(avctx->height, 16);
648 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
651 av_image_fill_arrays(src, linesize,
652 buffer->data + buffer->type->video.offset[0],
653 avctx->pix_fmt, w, h, 1);
654 av_image_copy(frame->data, frame->linesize, src, linesize,
655 avctx->pix_fmt, avctx->width, avctx->height);
658 frame->pts = buffer->pts == MMAL_TIME_UNKNOWN ? AV_NOPTS_VALUE : buffer->pts;
660 FF_DISABLE_DEPRECATION_WARNINGS
661 frame->pkt_pts = frame->pts;
662 FF_ENABLE_DEPRECATION_WARNINGS
664 frame->pkt_dts = AV_NOPTS_VALUE;
670 // Fetch a decoded buffer and place it into the frame parameter.
671 static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
673 MMALDecodeContext *ctx = avctx->priv_data;
674 MMAL_BUFFER_HEADER_T *buffer = NULL;
675 MMAL_STATUS_T status = 0;
678 if (ctx->eos_received)
682 // To ensure decoding in lockstep with a constant delay between fed packets
683 // and output frames, we always wait until an output buffer is available.
684 // Except during start we don't know after how many input packets the decoder
685 // is going to return the first buffer, and we can't distinguish decoder
686 // being busy from decoder waiting for input. So just poll at the start and
687 // keep feeding new data to the buffer.
688 // We are pretty sure the decoder will produce output if we sent more input
689 // frames than what a H.264 decoder could logically delay. This avoids too
690 // excessive buffering.
691 // We also wait if we sent eos, but didn't receive it yet (think of decoding
692 // stream with a very low number of frames).
693 if (atomic_load(&ctx->packets_buffered) > MAX_DELAYED_FRAMES ||
694 (ctx->packets_sent && ctx->eos_sent)) {
695 // MMAL will ignore broken input packets, which means the frame we
696 // expect here may never arrive. Dealing with this correctly is
697 // complicated, so here's a hack to avoid that it freezes forever
698 // in this unlikely situation.
699 buffer = mmal_queue_timedwait(ctx->queue_decoded_frames, 100);
701 av_log(avctx, AV_LOG_ERROR, "Did not get output frame from MMAL.\n");
702 ret = AVERROR_UNKNOWN;
706 buffer = mmal_queue_get(ctx->queue_decoded_frames);
711 ctx->eos_received |= !!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS);
712 if (ctx->eos_received)
715 if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED) {
716 MMAL_COMPONENT_T *decoder = ctx->decoder;
717 MMAL_EVENT_FORMAT_CHANGED_T *ev = mmal_event_format_changed_get(buffer);
718 MMAL_BUFFER_HEADER_T *stale_buffer;
720 av_log(avctx, AV_LOG_INFO, "Changing output format.\n");
722 if ((status = mmal_port_disable(decoder->output[0])))
725 while ((stale_buffer = mmal_queue_get(ctx->queue_decoded_frames)))
726 mmal_buffer_header_release(stale_buffer);
728 mmal_format_copy(decoder->output[0]->format, ev->format);
730 if ((ret = ffmal_update_format(avctx)) < 0)
733 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
736 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
739 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
742 mmal_buffer_header_release(buffer);
744 } else if (buffer->cmd) {
746 av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
747 av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n", s);
749 } else if (buffer->length == 0) {
750 // Unused output buffer that got drained after format change.
751 mmal_buffer_header_release(buffer);
755 ctx->frames_output++;
757 if ((ret = ffmal_copy_frame(avctx, frame, buffer)) < 0)
766 mmal_buffer_header_release(buffer);
767 if (status && ret >= 0)
768 ret = AVERROR_UNKNOWN;
772 static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame,
775 MMALDecodeContext *ctx = avctx->priv_data;
776 AVFrame *frame = data;
779 if (avctx->extradata_size && !ctx->extradata_sent) {
781 av_init_packet(&pkt);
782 pkt.data = avctx->extradata;
783 pkt.size = avctx->extradata_size;
784 ctx->extradata_sent = 1;
785 if ((ret = ffmmal_add_packet(avctx, &pkt, 1)) < 0)
789 if ((ret = ffmmal_add_packet(avctx, avpkt, 0)) < 0)
792 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
795 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
798 if ((ret = ffmmal_read_frame(avctx, frame, got_frame)) < 0)
801 // ffmmal_read_frame() can block for a while. Since the decoder is
802 // asynchronous, it's a good idea to fill the ports again.
804 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
807 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
813 AVHWAccel ff_h264_mmal_hwaccel = {
815 .type = AVMEDIA_TYPE_VIDEO,
816 .id = AV_CODEC_ID_H264,
817 .pix_fmt = AV_PIX_FMT_MMAL,
820 AVHWAccel ff_mpeg2_mmal_hwaccel = {
821 .name = "mpeg2_mmal",
822 .type = AVMEDIA_TYPE_VIDEO,
823 .id = AV_CODEC_ID_MPEG2VIDEO,
824 .pix_fmt = AV_PIX_FMT_MMAL,
827 AVHWAccel ff_mpeg4_mmal_hwaccel = {
828 .name = "mpeg4_mmal",
829 .type = AVMEDIA_TYPE_VIDEO,
830 .id = AV_CODEC_ID_MPEG4,
831 .pix_fmt = AV_PIX_FMT_MMAL,
834 AVHWAccel ff_vc1_mmal_hwaccel = {
836 .type = AVMEDIA_TYPE_VIDEO,
837 .id = AV_CODEC_ID_VC1,
838 .pix_fmt = AV_PIX_FMT_MMAL,
841 static const AVOption options[]={
842 {"extra_buffers", "extra buffers", offsetof(MMALDecodeContext, extra_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
843 {"extra_decoder_buffers", "extra MMAL internal buffered frames", offsetof(MMALDecodeContext, extra_decoder_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
847 #define FFMMAL_DEC_CLASS(NAME) \
848 static const AVClass ffmmal_##NAME##_dec_class = { \
849 .class_name = "mmal_" #NAME "_dec", \
851 .version = LIBAVUTIL_VERSION_INT, \
854 #define FFMMAL_DEC(NAME, ID) \
855 FFMMAL_DEC_CLASS(NAME) \
856 AVCodec ff_##NAME##_mmal_decoder = { \
857 .name = #NAME "_mmal", \
858 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (mmal)"), \
859 .type = AVMEDIA_TYPE_VIDEO, \
861 .priv_data_size = sizeof(MMALDecodeContext), \
862 .init = ffmmal_init_decoder, \
863 .close = ffmmal_close_decoder, \
864 .decode = ffmmal_decode, \
865 .flush = ffmmal_flush, \
866 .priv_class = &ffmmal_##NAME##_dec_class, \
867 .capabilities = AV_CODEC_CAP_DELAY, \
868 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
869 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL, \
870 AV_PIX_FMT_YUV420P, \
874 FFMMAL_DEC(h264, AV_CODEC_ID_H264)
875 FFMMAL_DEC(mpeg2, AV_CODEC_ID_MPEG2VIDEO)
876 FFMMAL_DEC(mpeg4, AV_CODEC_ID_MPEG4)
877 FFMMAL_DEC(vc1, AV_CODEC_ID_VC1)