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