77838dc509e189dccdc3d8fb85fcd7a3f00db476
[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->packets_sent) {
481 // Short-cut the flush logic to avoid upsetting MMAL.
482 ctx->eos_sent = 1;
483 ctx->eos_received = 1;
484 goto done;
485 }
486 }
487
488 start = data;
489
490 do {
491 FFBufferEntry *buffer = av_mallocz(sizeof(*buffer));
492 if (!buffer) {
493 ret = AVERROR(ENOMEM);
494 goto done;
495 }
496
497 buffer->data = data;
498 buffer->length = FFMIN(size, ctx->decoder->input[0]->buffer_size);
499
500 if (is_extradata)
501 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_CONFIG;
502
503 if (data == start)
504 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
505
506 data += buffer->length;
507 size -= buffer->length;
508
509 buffer->pts = avpkt->pts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->pts;
510 buffer->dts = avpkt->dts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->dts;
511
512 if (!size) {
513 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
514 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, 1);
515 }
516
517 if (!buffer->length) {
518 buffer->flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
519 ctx->eos_sent = 1;
520 }
521
522 if (buf) {
523 buffer->ref = av_buffer_ref(buf);
524 if (!buffer->ref) {
525 av_free(buffer);
526 ret = AVERROR(ENOMEM);
527 goto done;
528 }
529 }
530
531 // Insert at end of the list
532 if (!ctx->waiting_buffers)
533 ctx->waiting_buffers = buffer;
534 if (ctx->waiting_buffers_tail)
535 ctx->waiting_buffers_tail->next = buffer;
536 ctx->waiting_buffers_tail = buffer;
537 } while (size);
538
539 done:
540 av_buffer_unref(&buf);
541 return ret;
542 }
543
544 // Move prepared/split packets from waiting_buffers to the MMAL decoder.
545 static int ffmmal_fill_input_port(AVCodecContext *avctx)
546 {
547 MMALDecodeContext *ctx = avctx->priv_data;
548
549 while (ctx->waiting_buffers) {
550 MMAL_BUFFER_HEADER_T *mbuffer;
551 FFBufferEntry *buffer;
552 MMAL_STATUS_T status;
553
554 mbuffer = mmal_queue_get(ctx->pool_in->queue);
555 if (!mbuffer)
556 return 0;
557
558 buffer = ctx->waiting_buffers;
559
560 mmal_buffer_header_reset(mbuffer);
561 mbuffer->cmd = 0;
562 mbuffer->pts = buffer->pts;
563 mbuffer->dts = buffer->dts;
564 mbuffer->flags = buffer->flags;
565 mbuffer->data = buffer->data;
566 mbuffer->length = buffer->length;
567 mbuffer->user_data = buffer;
568 mbuffer->alloc_size = ctx->decoder->input[0]->buffer_size;
569
570 // Remove from start of the list
571 ctx->waiting_buffers = buffer->next;
572 if (ctx->waiting_buffers_tail == buffer)
573 ctx->waiting_buffers_tail = NULL;
574
575 if ((status = mmal_port_send_buffer(ctx->decoder->input[0], mbuffer))) {
576 mmal_buffer_header_release(mbuffer);
577 av_buffer_unref(&buffer->ref);
578 if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
579 avpriv_atomic_int_add_and_fetch(&ctx->packets_buffered, -1);
580 av_free(buffer);
581 }
582
583 if (status) {
584 av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending input\n", (int)status);
585 return AVERROR_UNKNOWN;
586 }
587 }
588
589 return 0;
590 }
591
592 static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame,
593 MMAL_BUFFER_HEADER_T *buffer)
594 {
595 MMALDecodeContext *ctx = avctx->priv_data;
596 int ret = 0;
597
598 if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
599 if (!ctx->pool_out)
600 return AVERROR_UNKNOWN; // format change code failed with OOM previously
601
602 if ((ret = ff_decode_frame_props(avctx, frame)) < 0)
603 goto done;
604
605 if ((ret = ffmmal_set_ref(frame, ctx->pool_out, buffer)) < 0)
606 goto done;
607 } else {
608 int w = FFALIGN(avctx->width, 32);
609 int h = FFALIGN(avctx->height, 16);
610 uint8_t *src[4];
611 int linesize[4];
612
613 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
614 goto done;
615
616 av_image_fill_arrays(src, linesize,
617 buffer->data + buffer->type->video.offset[0],
618 avctx->pix_fmt, w, h, 1);
619 av_image_copy(frame->data, frame->linesize, src, linesize,
620 avctx->pix_fmt, avctx->width, avctx->height);
621 }
622
623 frame->pkt_pts = buffer->pts == MMAL_TIME_UNKNOWN ? AV_NOPTS_VALUE : buffer->pts;
624 frame->pkt_dts = AV_NOPTS_VALUE;
625
626 done:
627 return ret;
628 }
629
630 // Fetch a decoded buffer and place it into the frame parameter.
631 static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
632 {
633 MMALDecodeContext *ctx = avctx->priv_data;
634 MMAL_BUFFER_HEADER_T *buffer = NULL;
635 MMAL_STATUS_T status = 0;
636 int ret = 0;
637
638 if (ctx->eos_received)
639 goto done;
640
641 while (1) {
642 // To ensure decoding in lockstep with a constant delay between fed packets
643 // and output frames, we always wait until an output buffer is available.
644 // Except during start we don't know after how many input packets the decoder
645 // is going to return the first buffer, and we can't distinguish decoder
646 // being busy from decoder waiting for input. So just poll at the start and
647 // keep feeding new data to the buffer.
648 // We are pretty sure the decoder will produce output if we sent more input
649 // frames than what a h264 decoder could logically delay. This avoids too
650 // excessive buffering.
651 // We also wait if we sent eos, but didn't receive it yet (think of decoding
652 // stream with a very low number of frames).
653 if (avpriv_atomic_int_get(&ctx->packets_buffered) > MAX_DELAYED_FRAMES ||
654 (ctx->packets_sent && ctx->eos_sent)) {
655 // MMAL will ignore broken input packets, which means the frame we
656 // expect here may never arrive. Dealing with this correctly is
657 // complicated, so here's a hack to avoid that it freezes forever
658 // in this unlikely situation.
659 buffer = mmal_queue_timedwait(ctx->queue_decoded_frames, 100);
660 if (!buffer) {
661 av_log(avctx, AV_LOG_ERROR, "Did not get output frame from MMAL.\n");
662 ret = AVERROR_UNKNOWN;
663 goto done;
664 }
665 } else {
666 buffer = mmal_queue_get(ctx->queue_decoded_frames);
667 if (!buffer)
668 goto done;
669 }
670
671 ctx->eos_received |= !!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS);
672 if (ctx->eos_received)
673 goto done;
674
675 if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED) {
676 MMAL_COMPONENT_T *decoder = ctx->decoder;
677 MMAL_EVENT_FORMAT_CHANGED_T *ev = mmal_event_format_changed_get(buffer);
678 MMAL_BUFFER_HEADER_T *stale_buffer;
679
680 av_log(avctx, AV_LOG_INFO, "Changing output format.\n");
681
682 if ((status = mmal_port_disable(decoder->output[0])))
683 goto done;
684
685 while ((stale_buffer = mmal_queue_get(ctx->queue_decoded_frames)))
686 mmal_buffer_header_release(stale_buffer);
687
688 mmal_format_copy(decoder->output[0]->format, ev->format);
689
690 if ((ret = ffmal_update_format(avctx)) < 0)
691 goto done;
692
693 if ((status = mmal_port_enable(decoder->output[0], output_callback)))
694 goto done;
695
696 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
697 goto done;
698
699 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
700 goto done;
701
702 mmal_buffer_header_release(buffer);
703 continue;
704 } else if (buffer->cmd) {
705 char s[20];
706 av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
707 av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n", s);
708 goto done;
709 } else if (buffer->length == 0) {
710 // Unused output buffer that got drained after format change.
711 mmal_buffer_header_release(buffer);
712 continue;
713 }
714
715 ctx->frames_output++;
716
717 if ((ret = ffmal_copy_frame(avctx, frame, buffer)) < 0)
718 goto done;
719
720 *got_frame = 1;
721 break;
722 }
723
724 done:
725 if (buffer)
726 mmal_buffer_header_release(buffer);
727 if (status && ret >= 0)
728 ret = AVERROR_UNKNOWN;
729 return ret;
730 }
731
732 static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame,
733 AVPacket *avpkt)
734 {
735 MMALDecodeContext *ctx = avctx->priv_data;
736 AVFrame *frame = data;
737 int ret = 0;
738
739 if (avctx->extradata_size && !ctx->extradata_sent) {
740 AVPacket pkt = {0};
741 av_init_packet(&pkt);
742 pkt.data = avctx->extradata;
743 pkt.size = avctx->extradata_size;
744 ctx->extradata_sent = 1;
745 if ((ret = ffmmal_add_packet(avctx, &pkt, 1)) < 0)
746 return ret;
747 }
748
749 if ((ret = ffmmal_add_packet(avctx, avpkt, 0)) < 0)
750 return ret;
751
752 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
753 return ret;
754
755 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
756 return ret;
757
758 if ((ret = ffmmal_read_frame(avctx, frame, got_frame)) < 0)
759 return ret;
760
761 // ffmmal_read_frame() can block for a while. Since the decoder is
762 // asynchronous, it's a good idea to fill the ports again.
763
764 if ((ret = ffmmal_fill_output_port(avctx)) < 0)
765 return ret;
766
767 if ((ret = ffmmal_fill_input_port(avctx)) < 0)
768 return ret;
769
770 return ret;
771 }
772
773 AVHWAccel ff_h264_mmal_hwaccel = {
774 .name = "h264_mmal",
775 .type = AVMEDIA_TYPE_VIDEO,
776 .id = AV_CODEC_ID_H264,
777 .pix_fmt = AV_PIX_FMT_MMAL,
778 };
779
780 AVHWAccel ff_mpeg2_mmal_hwaccel = {
781 .name = "mpeg2_mmal",
782 .type = AVMEDIA_TYPE_VIDEO,
783 .id = AV_CODEC_ID_MPEG2VIDEO,
784 .pix_fmt = AV_PIX_FMT_MMAL,
785 };
786
787 AVHWAccel ff_vc1_mmal_hwaccel = {
788 .name = "vc1_mmal",
789 .type = AVMEDIA_TYPE_VIDEO,
790 .id = AV_CODEC_ID_VC1,
791 .pix_fmt = AV_PIX_FMT_MMAL,
792 };
793
794 static const AVOption options[]={
795 {"extra_buffers", "extra buffers", offsetof(MMALDecodeContext, extra_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
796 {NULL}
797 };
798
799 #define FFMMAL_DEC_CLASS(NAME) \
800 static const AVClass ffmmal_##NAME##_dec_class = { \
801 .class_name = "mmal_" #NAME "_dec", \
802 .option = options, \
803 .version = LIBAVUTIL_VERSION_INT, \
804 };
805
806 #define FFMMAL_DEC(NAME, ID) \
807 FFMMAL_DEC_CLASS(NAME) \
808 AVCodec ff_##NAME##_mmal_decoder = { \
809 .name = #NAME "_mmal", \
810 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (mmal)"), \
811 .type = AVMEDIA_TYPE_VIDEO, \
812 .id = ID, \
813 .priv_data_size = sizeof(MMALDecodeContext), \
814 .init = ffmmal_init_decoder, \
815 .close = ffmmal_close_decoder, \
816 .decode = ffmmal_decode, \
817 .flush = ffmmal_flush, \
818 .priv_class = &ffmmal_##NAME##_dec_class, \
819 .capabilities = AV_CODEC_CAP_DELAY, \
820 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
821 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL, \
822 AV_PIX_FMT_YUV420P, \
823 AV_PIX_FMT_NONE}, \
824 };
825
826 FFMMAL_DEC(h264, AV_CODEC_ID_H264)
827 FFMMAL_DEC(mpeg2, AV_CODEC_ID_MPEG2VIDEO)
828 FFMMAL_DEC(vc1, AV_CODEC_ID_VC1)