mmaldec: send only a single EOS packet on flushing
[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 if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
165 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, -1);
166
167 av_buffer_unref(&buffer->ref);
168 av_free(buffer);
169 }
170 ctx->waiting_buffers_tail = NULL;
171
172 av_assert0(avpriv_atomic_int_get(&ctx->packets_buffered) == 0);
173
174 ctx->frames_output = ctx->eos_received = ctx->eos_sent = ctx->packets_sent = ctx->extradata_sent = 0;
175 }
176
177 static av_cold int ffmmal_close_decoder(AVCodecContext *avctx)
178 {
179 MMALDecodeContext *ctx = avctx->priv_data;
180
181 if (ctx->decoder)
182 ffmmal_stop_decoder(avctx);
183
184 mmal_component_destroy(ctx->decoder);
185 ctx->decoder = NULL;
186 mmal_queue_destroy(ctx->queue_decoded_frames);
187 mmal_pool_destroy(ctx->pool_in);
188 ffmmal_poolref_unref(ctx->pool_out);
189
190 mmal_vc_deinit();
191
192 return 0;
193 }
194
195 static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
196 {
197 AVCodecContext *avctx = (AVCodecContext*)port->userdata;
198 MMALDecodeContext *ctx = avctx->priv_data;
199
200 if (!buffer->cmd) {
201 FFBufferEntry *entry = buffer->user_data;
202 av_buffer_unref(&entry->ref);
203 if (entry->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
204 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, -1);
205 av_free(entry);
206 }
207 mmal_buffer_header_release(buffer);
208 }
209
210 static void output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
211 {
212 AVCodecContext *avctx = (AVCodecContext*)port->userdata;
213 MMALDecodeContext *ctx = avctx->priv_data;
214
215 mmal_queue_put(ctx->queue_decoded_frames, buffer);
216 }
217
218 static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
219 {
220 AVCodecContext *avctx = (AVCodecContext*)port->userdata;
221 MMAL_STATUS_T status;
222
223 if (buffer->cmd == MMAL_EVENT_ERROR) {
224 status = *(uint32_t *)buffer->data;
225 av_log(avctx, AV_LOG_ERROR, "MMAL error %d on control port\n", (int)status);
226 } else {
227 char s[20];
228 av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
229 av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on control port\n", s);
230 }
231
232 mmal_buffer_header_release(buffer);
233 }
234
235 // Feed free output buffers to the decoder.
236 static int ffmmal_fill_output_port(AVCodecContext *avctx)
237 {
238 MMALDecodeContext *ctx = avctx->priv_data;
239 MMAL_BUFFER_HEADER_T *buffer;
240 MMAL_STATUS_T status;
241
242 if (!ctx->pool_out)
243 return AVERROR_UNKNOWN; // format change code failed with OOM previously
244
245 while ((buffer = mmal_queue_get(ctx->pool_out->pool->queue))) {
246 if ((status = mmal_port_send_buffer(ctx->decoder->output[0], buffer))) {
247 mmal_buffer_header_release(buffer);
248 av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending output buffer.\n", (int)status);
249 return AVERROR_UNKNOWN;
250 }
251 }
252
253 return 0;
254 }
255
256 static enum AVColorSpace ffmmal_csp_to_av_csp(MMAL_FOURCC_T fourcc)
257 {
258 switch (fourcc) {
259 case MMAL_COLOR_SPACE_BT470_2_BG:
260 case MMAL_COLOR_SPACE_BT470_2_M:
261 case MMAL_COLOR_SPACE_ITUR_BT601: return AVCOL_SPC_BT470BG;
262 case MMAL_COLOR_SPACE_ITUR_BT709: return AVCOL_SPC_BT709;
263 case MMAL_COLOR_SPACE_FCC: return AVCOL_SPC_FCC;
264 case MMAL_COLOR_SPACE_SMPTE240M: return AVCOL_SPC_SMPTE240M;
265 default: return AVCOL_SPC_UNSPECIFIED;
266 }
267 }
268
269 static int ffmal_update_format(AVCodecContext *avctx)
270 {
271 MMALDecodeContext *ctx = avctx->priv_data;
272 MMAL_STATUS_T status;
273 int ret = 0;
274 MMAL_COMPONENT_T *decoder = ctx->decoder;
275 MMAL_ES_FORMAT_T *format_out = decoder->output[0]->format;
276
277 ffmmal_poolref_unref(ctx->pool_out);
278 if (!(ctx->pool_out = av_mallocz(sizeof(*ctx->pool_out)))) {
279 ret = AVERROR(ENOMEM);
280 goto fail;
281 }
282 ctx->pool_out->refcount = 1;
283
284 if (!format_out)
285 goto fail;
286
287 if ((status = mmal_port_parameter_set_uint32(decoder->output[0], MMAL_PARAMETER_EXTRA_BUFFERS, ctx->extra_buffers)))
288 goto fail;
289
290 if ((status = mmal_port_parameter_set_boolean(decoder->output[0], MMAL_PARAMETER_VIDEO_INTERPOLATE_TIMESTAMPS, 0)))
291 goto fail;
292
293 if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
294 format_out->encoding = MMAL_ENCODING_OPAQUE;
295 } else {
296 format_out->encoding_variant = format_out->encoding = MMAL_ENCODING_I420;
297 }
298
299 if ((status = mmal_port_format_commit(decoder->output[0])))
300 goto fail;
301
302 if ((ret = ff_set_dimensions(avctx, format_out->es->video.crop.x + format_out->es->video.crop.width,
303 format_out->es->video.crop.y + format_out->es->video.crop.height)) < 0)
304 goto fail;
305
306 if (format_out->es->video.par.num && format_out->es->video.par.den) {
307 avctx->sample_aspect_ratio.num = format_out->es->video.par.num;
308 avctx->sample_aspect_ratio.den = format_out->es->video.par.den;
309 }
310
311 avctx->colorspace = ffmmal_csp_to_av_csp(format_out->es->video.color_space);
312
313 decoder->output[0]->buffer_size =
314 FFMAX(decoder->output[0]->buffer_size_min, decoder->output[0]->buffer_size_recommended);
315 decoder->output[0]->buffer_num =
316 FFMAX(decoder->output[0]->buffer_num_min, decoder->output[0]->buffer_num_recommended) + ctx->extra_buffers;
317 ctx->pool_out->pool = mmal_pool_create(decoder->output[0]->buffer_num,
318 decoder->output[0]->buffer_size);
319 if (!ctx->pool_out->pool) {
320 ret = AVERROR(ENOMEM);
321 goto fail;
322 }
323
324 return 0;
325
326 fail:
327 return ret < 0 ? ret : AVERROR_UNKNOWN;
328 }
329
330 static av_cold int ffmmal_init_decoder(AVCodecContext *avctx)
331 {
332 MMALDecodeContext *ctx = avctx->priv_data;
333 MMAL_STATUS_T status;
334 MMAL_ES_FORMAT_T *format_in;
335 MMAL_COMPONENT_T *decoder;
336 int ret = 0;
337
338 bcm_host_init();
339
340 if (mmal_vc_init()) {
341 av_log(avctx, AV_LOG_ERROR, "Cannot initialize MMAL VC driver!\n");
342 return AVERROR(ENOSYS);
343 }
344
345 if ((ret = ff_get_format(avctx, avctx->codec->pix_fmts)) < 0)
346 return ret;
347
348 avctx->pix_fmt = ret;
349
350 if ((status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, &ctx->decoder)))
351 goto fail;
352
353 decoder = ctx->decoder;
354
355 format_in = decoder->input[0]->format;
356 format_in->type = MMAL_ES_TYPE_VIDEO;
357 switch (avctx->codec_id) {
358 case AV_CODEC_ID_MPEG2VIDEO:
359 format_in->encoding = MMAL_ENCODING_MP2V;
360 av_log(avctx, AV_LOG_DEBUG, "Use MMAL MP2V encoding\n");
361 break;
362 case AV_CODEC_ID_VC1:
363 format_in->encoding = MMAL_ENCODING_WVC1;
364 av_log(avctx, AV_LOG_DEBUG, "Use MMAL WVC1 encoding\n");
365 break;
366 case AV_CODEC_ID_H264:
367 default:
368 format_in->encoding = MMAL_ENCODING_H264;
369 av_log(avctx, AV_LOG_DEBUG, "Use MMAL H264 encoding\n");
370 break;
371 }
372 format_in->es->video.width = FFALIGN(avctx->width, 32);
373 format_in->es->video.height = FFALIGN(avctx->height, 16);
374 format_in->es->video.crop.width = avctx->width;
375 format_in->es->video.crop.height = avctx->height;
376 format_in->es->video.frame_rate.num = 24000;
377 format_in->es->video.frame_rate.den = 1001;
378 format_in->es->video.par.num = avctx->sample_aspect_ratio.num;
379 format_in->es->video.par.den = avctx->sample_aspect_ratio.den;
380 format_in->flags = MMAL_ES_FORMAT_FLAG_FRAMED;
381
382 if ((status = mmal_port_format_commit(decoder->input[0])))
383 goto fail;
384
385 decoder->input[0]->buffer_num =
386 FFMAX(decoder->input[0]->buffer_num_min, 20);
387 decoder->input[0]->buffer_size =
388 FFMAX(decoder->input[0]->buffer_size_min, 512 * 1024);
389 ctx->pool_in = mmal_pool_create(decoder->input[0]->buffer_num, 0);
390 if (!ctx->pool_in) {
391 ret = AVERROR(ENOMEM);
392 goto fail;
393 }
394
395 if ((ret = ffmal_update_format(avctx)) < 0)
396 goto fail;
397
398 ctx->queue_decoded_frames = mmal_queue_create();
399 if (!ctx->queue_decoded_frames)
400 goto fail;
401
402 decoder->input[0]->userdata = (void*)avctx;
403 decoder->output[0]->userdata = (void*)avctx;
404 decoder->control->userdata = (void*)avctx;
405
406 if ((status = mmal_port_enable(decoder->control, control_port_cb)))
407 goto fail;
408 if ((status = mmal_port_enable(decoder->input[0], input_callback)))
409 goto fail;
410 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
411 goto fail;
412
413 if ((status = mmal_component_enable(decoder)))
414 goto fail;
415
416 return 0;
417
418 fail:
419 ffmmal_close_decoder(avctx);
420 return ret < 0 ? ret : AVERROR_UNKNOWN;
421 }
422
423 static void ffmmal_flush(AVCodecContext *avctx)
424 {
425 MMALDecodeContext *ctx = avctx->priv_data;
426 MMAL_COMPONENT_T *decoder = ctx->decoder;
427 MMAL_STATUS_T status;
428
429 ffmmal_stop_decoder(avctx);
430
431 if ((status = mmal_port_enable(decoder->control, control_port_cb)))
432 goto fail;
433 if ((status = mmal_port_enable(decoder->input[0], input_callback)))
434 goto fail;
435 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
436 goto fail;
437
438 return;
439
440 fail:
441 av_log(avctx, AV_LOG_ERROR, "MMAL flush error: %i\n", (int)status);
442 }
443
444 // Split packets and add them to the waiting_buffers list. We don't queue them
445 // immediately, because it can happen that the decoder is temporarily blocked
446 // (due to us not reading/returning enough output buffers) and won't accept
447 // new input. (This wouldn't be an issue if MMAL input buffers always were
448 // complete frames - then the input buffer just would have to be big enough.)
449 // If is_extradata is set, send it as MMAL_BUFFER_HEADER_FLAG_CONFIG.
450 static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt,
451 int is_extradata)
452 {
453 MMALDecodeContext *ctx = avctx->priv_data;
454 AVBufferRef *buf = NULL;
455 int size = 0;
456 uint8_t *data = (uint8_t *)"";
457 uint8_t *start;
458 int ret = 0;
459
460 if (avpkt->size) {
461 if (avpkt->buf) {
462 buf = av_buffer_ref(avpkt->buf);
463 size = avpkt->size;
464 data = avpkt->data;
465 } else {
466 buf = av_buffer_alloc(avpkt->size);
467 if (buf) {
468 memcpy(buf->data, avpkt->data, avpkt->size);
469 size = buf->size;
470 data = buf->data;
471 }
472 }
473 if (!buf) {
474 ret = AVERROR(ENOMEM);
475 goto done;
476 }
477 if (!is_extradata)
478 ctx->packets_sent++;
479 } else {
480 if (ctx->eos_sent)
481 goto done;
482 if (!ctx->packets_sent) {
483 // Short-cut the flush logic to avoid upsetting MMAL.
484 ctx->eos_sent = 1;
485 ctx->eos_received = 1;
486 goto done;
487 }
488 }
489
490 start = data;
491
492 do {
493 FFBufferEntry *buffer = av_mallocz(sizeof(*buffer));
494 if (!buffer) {
495 ret = AVERROR(ENOMEM);
496 goto done;
497 }
498
499 buffer->data = data;
500 buffer->length = FFMIN(size, ctx->decoder->input[0]->buffer_size);
501
502 if (is_extradata)
503 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_CONFIG;
504
505 if (data == start)
506 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
507
508 data += buffer->length;
509 size -= buffer->length;
510
511 buffer->pts = avpkt->pts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->pts;
512 buffer->dts = avpkt->dts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->dts;
513
514 if (!size) {
515 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
516 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, 1);
517 }
518
519 if (!buffer->length) {
520 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
521 ctx->eos_sent = 1;
522 }
523
524 if (buf) {
525 buffer->ref = av_buffer_ref(buf);
526 if (!buffer->ref) {
527 av_free(buffer);
528 ret = AVERROR(ENOMEM);
529 goto done;
530 }
531 }
532
533 // Insert at end of the list
534 if (!ctx->waiting_buffers)
535 ctx->waiting_buffers = buffer;
536 if (ctx->waiting_buffers_tail)
537 ctx->waiting_buffers_tail->next = buffer;
538 ctx->waiting_buffers_tail = buffer;
539 } while (size);
540
541 done:
542 av_buffer_unref(&buf);
543 return ret;
544 }
545
546 // Move prepared/split packets from waiting_buffers to the MMAL decoder.
547 static int ffmmal_fill_input_port(AVCodecContext *avctx)
548 {
549 MMALDecodeContext *ctx = avctx->priv_data;
550
551 while (ctx->waiting_buffers) {
552 MMAL_BUFFER_HEADER_T *mbuffer;
553 FFBufferEntry *buffer;
554 MMAL_STATUS_T status;
555
556 mbuffer = mmal_queue_get(ctx->pool_in->queue);
557 if (!mbuffer)
558 return 0;
559
560 buffer = ctx->waiting_buffers;
561
562 mmal_buffer_header_reset(mbuffer);
563 mbuffer->cmd = 0;
564 mbuffer->pts = buffer->pts;
565 mbuffer->dts = buffer->dts;
566 mbuffer->flags = buffer->flags;
567 mbuffer->data = buffer->data;
568 mbuffer->length = buffer->length;
569 mbuffer->user_data = buffer;
570 mbuffer->alloc_size = ctx->decoder->input[0]->buffer_size;
571
572 // Remove from start of the list
573 ctx->waiting_buffers = buffer->next;
574 if (ctx->waiting_buffers_tail == buffer)
575 ctx->waiting_buffers_tail = NULL;
576
577 if ((status = mmal_port_send_buffer(ctx->decoder->input[0], mbuffer))) {
578 mmal_buffer_header_release(mbuffer);
579 av_buffer_unref(&buffer->ref);
580 if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
581 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, -1);
582 av_free(buffer);
583 }
584
585 if (status) {
586 av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending input\n", (int)status);
587 return AVERROR_UNKNOWN;
588 }
589 }
590
591 return 0;
592 }
593
594 static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame,
595 MMAL_BUFFER_HEADER_T *buffer)
596 {
597 MMALDecodeContext *ctx = avctx->priv_data;
598 int ret = 0;
599
600 if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
601 if (!ctx->pool_out)
602 return AVERROR_UNKNOWN; // format change code failed with OOM previously
603
604 if ((ret = ff_decode_frame_props(avctx, frame)) < 0)
605 goto done;
606
607 if ((ret = ffmmal_set_ref(frame, ctx->pool_out, buffer)) < 0)
608 goto done;
609 } else {
610 int w = FFALIGN(avctx->width, 32);
611 int h = FFALIGN(avctx->height, 16);
612 uint8_t *src[4];
613 int linesize[4];
614
615 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
616 goto done;
617
618 av_image_fill_arrays(src, linesize,
619 buffer->data + buffer->type->video.offset[0],
620 avctx->pix_fmt, w, h, 1);
621 av_image_copy(frame->data, frame->linesize, src, linesize,
622 avctx->pix_fmt, avctx->width, avctx->height);
623 }
624
625 frame->pkt_pts = buffer->pts == MMAL_TIME_UNKNOWN ? AV_NOPTS_VALUE : buffer->pts;
626 frame->pkt_dts = AV_NOPTS_VALUE;
627
628 done:
629 return ret;
630 }
631
632 // Fetch a decoded buffer and place it into the frame parameter.
633 static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
634 {
635 MMALDecodeContext *ctx = avctx->priv_data;
636 MMAL_BUFFER_HEADER_T *buffer = NULL;
637 MMAL_STATUS_T status = 0;
638 int ret = 0;
639
640 if (ctx->eos_received)
641 goto done;
642
643 while (1) {
644 // To ensure decoding in lockstep with a constant delay between fed packets
645 // and output frames, we always wait until an output buffer is available.
646 // Except during start we don't know after how many input packets the decoder
647 // is going to return the first buffer, and we can't distinguish decoder
648 // being busy from decoder waiting for input. So just poll at the start and
649 // keep feeding new data to the buffer.
650 // We are pretty sure the decoder will produce output if we sent more input
651 // frames than what a h264 decoder could logically delay. This avoids too
652 // excessive buffering.
653 // We also wait if we sent eos, but didn't receive it yet (think of decoding
654 // stream with a very low number of frames).
655 if (avpriv_atomic_int_get(&ctx->packets_buffered) > MAX_DELAYED_FRAMES ||
656 (ctx->packets_sent && ctx->eos_sent)) {
657 // MMAL will ignore broken input packets, which means the frame we
658 // expect here may never arrive. Dealing with this correctly is
659 // complicated, so here's a hack to avoid that it freezes forever
660 // in this unlikely situation.
661 buffer = mmal_queue_timedwait(ctx->queue_decoded_frames, 100);
662 if (!buffer) {
663 av_log(avctx, AV_LOG_ERROR, "Did not get output frame from MMAL.\n");
664 ret = AVERROR_UNKNOWN;
665 goto done;
666 }
667 } else {
668 buffer = mmal_queue_get(ctx->queue_decoded_frames);
669 if (!buffer)
670 goto done;
671 }
672
673 ctx->eos_received |= !!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS);
674 if (ctx->eos_received)
675 goto done;
676
677 if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED) {
678 MMAL_COMPONENT_T *decoder = ctx->decoder;
679 MMAL_EVENT_FORMAT_CHANGED_T *ev = mmal_event_format_changed_get(buffer);
680 MMAL_BUFFER_HEADER_T *stale_buffer;
681
682 av_log(avctx, AV_LOG_INFO, "Changing output format.\n");
683
684 if ((status = mmal_port_disable(decoder->output[0])))
685 goto done;
686
687 while ((stale_buffer = mmal_queue_get(ctx->queue_decoded_frames)))
688 mmal_buffer_header_release(stale_buffer);
689
690 mmal_format_copy(decoder->output[0]->format, ev->format);
691
692 if ((ret = ffmal_update_format(avctx)) < 0)
693 goto done;
694
695 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
696 goto done;
697
698 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
699 goto done;
700
701 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
702 goto done;
703
704 mmal_buffer_header_release(buffer);
705 continue;
706 } else if (buffer->cmd) {
707 char s[20];
708 av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
709 av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n", s);
710 goto done;
711 } else if (buffer->length == 0) {
712 // Unused output buffer that got drained after format change.
713 mmal_buffer_header_release(buffer);
714 continue;
715 }
716
717 ctx->frames_output++;
718
719 if ((ret = ffmal_copy_frame(avctx, frame, buffer)) < 0)
720 goto done;
721
722 *got_frame = 1;
723 break;
724 }
725
726 done:
727 if (buffer)
728 mmal_buffer_header_release(buffer);
729 if (status && ret >= 0)
730 ret = AVERROR_UNKNOWN;
731 return ret;
732 }
733
734 static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame,
735 AVPacket *avpkt)
736 {
737 MMALDecodeContext *ctx = avctx->priv_data;
738 AVFrame *frame = data;
739 int ret = 0;
740
741 if (avctx->extradata_size && !ctx->extradata_sent) {
742 AVPacket pkt = {0};
743 av_init_packet(&pkt);
744 pkt.data = avctx->extradata;
745 pkt.size = avctx->extradata_size;
746 ctx->extradata_sent = 1;
747 if ((ret = ffmmal_add_packet(avctx, &pkt, 1)) < 0)
748 return ret;
749 }
750
751 if ((ret = ffmmal_add_packet(avctx, avpkt, 0)) < 0)
752 return ret;
753
754 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
755 return ret;
756
757 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
758 return ret;
759
760 if ((ret = ffmmal_read_frame(avctx, frame, got_frame)) < 0)
761 return ret;
762
763 // ffmmal_read_frame() can block for a while. Since the decoder is
764 // asynchronous, it's a good idea to fill the ports again.
765
766 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
767 return ret;
768
769 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
770 return ret;
771
772 return ret;
773 }
774
775 AVHWAccel ff_h264_mmal_hwaccel = {
776 .name = "h264_mmal",
777 .type = AVMEDIA_TYPE_VIDEO,
778 .id = AV_CODEC_ID_H264,
779 .pix_fmt = AV_PIX_FMT_MMAL,
780 };
781
782 AVHWAccel ff_mpeg2_mmal_hwaccel = {
783 .name = "mpeg2_mmal",
784 .type = AVMEDIA_TYPE_VIDEO,
785 .id = AV_CODEC_ID_MPEG2VIDEO,
786 .pix_fmt = AV_PIX_FMT_MMAL,
787 };
788
789 AVHWAccel ff_vc1_mmal_hwaccel = {
790 .name = "vc1_mmal",
791 .type = AVMEDIA_TYPE_VIDEO,
792 .id = AV_CODEC_ID_VC1,
793 .pix_fmt = AV_PIX_FMT_MMAL,
794 };
795
796 static const AVOption options[]={
797 {"extra_buffers", "extra buffers", offsetof(MMALDecodeContext, extra_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
798 {NULL}
799 };
800
801 #define FFMMAL_DEC_CLASS(NAME) \
802 static const AVClass ffmmal_##NAME##_dec_class = { \
803 .class_name = "mmal_" #NAME "_dec", \
804 .option = options, \
805 .version = LIBAVUTIL_VERSION_INT, \
806 };
807
808 #define FFMMAL_DEC(NAME, ID) \
809 FFMMAL_DEC_CLASS(NAME) \
810 AVCodec ff_##NAME##_mmal_decoder = { \
811 .name = #NAME "_mmal", \
812 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (mmal)"), \
813 .type = AVMEDIA_TYPE_VIDEO, \
814 .id = ID, \
815 .priv_data_size = sizeof(MMALDecodeContext), \
816 .init = ffmmal_init_decoder, \
817 .close = ffmmal_close_decoder, \
818 .decode = ffmmal_decode, \
819 .flush = ffmmal_flush, \
820 .priv_class = &ffmmal_##NAME##_dec_class, \
821 .capabilities = AV_CODEC_CAP_DELAY, \
822 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
823 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL, \
824 AV_PIX_FMT_YUV420P, \
825 AV_PIX_FMT_NONE}, \
826 };
827
828 FFMMAL_DEC(h264, AV_CODEC_ID_H264)
829 FFMMAL_DEC(mpeg2, AV_CODEC_ID_MPEG2VIDEO)
830 FFMMAL_DEC(vc1, AV_CODEC_ID_VC1)