FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mmaldec.c
Go to the documentation of this file.
1 /*
2  * MMAL Video Decoder
3  * Copyright (c) 2015 Rodger Combs
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
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/util/mmal_util.h>
30 #include <interface/mmal/util/mmal_util_params.h>
31 #include <interface/mmal/util/mmal_default_components.h>
32 #include <interface/mmal/vc/mmal_vc_api.h>
33 
34 #include "avcodec.h"
35 #include "internal.h"
36 #include "libavutil/atomic.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/buffer.h"
39 #include "libavutil/common.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/log.h"
42 
43 typedef struct FFBufferEntry {
45  void *data;
46  size_t length;
47  int64_t pts, dts;
48  int flags;
51 
52 // MMAL_POOL_T destroys all of its MMAL_BUFFER_HEADER_Ts. If we want correct
53 // refcounting for AVFrames, we can free the MMAL_POOL_T only after all AVFrames
54 // have been unreferenced.
55 typedef struct FFPoolRef {
56  volatile int refcount;
57  MMAL_POOL_T *pool;
58 } FFPoolRef;
59 
60 typedef struct FFBufferRef {
61  MMAL_BUFFER_HEADER_T *buffer;
63 } FFBufferRef;
64 
65 typedef struct MMALDecodeContext {
68 
70 
71  MMAL_COMPONENT_T *decoder;
72  MMAL_QUEUE_T *queue_decoded_frames;
73  MMAL_POOL_T *pool_in;
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.
83 
84  int64_t packets_sent;
85  int64_t frames_output;
87  int eos_sent;
89 
90 // Assume decoder is guaranteed to produce output after at least this many
91 // packets (where each packet contains 1 frame).
92 #define MAX_DELAYED_FRAMES 16
93 
95 {
96  if (ref && avpriv_atomic_int_add_and_fetch(&ref->refcount, -1) == 0) {
97  mmal_pool_destroy(ref->pool);
98  av_free(ref);
99  }
100 }
101 
102 static void ffmmal_release_frame(void *opaque, uint8_t *data)
103 {
104  FFBufferRef *ref = (void *)data;
105 
106  mmal_buffer_header_release(ref->buffer);
108 
109  av_free(ref);
110 }
111 
112 // Setup frame with a new reference to buffer. The buffer must have been
113 // allocated from the given pool.
115  MMAL_BUFFER_HEADER_T *buffer)
116 {
117  FFBufferRef *ref = av_mallocz(sizeof(*ref));
118  if (!ref)
119  return AVERROR(ENOMEM);
120 
121  ref->pool = pool;
122  ref->buffer = buffer;
123 
124  frame->buf[0] = av_buffer_create((void *)ref, sizeof(*ref),
127  if (!frame->buf[0]) {
128  av_free(ref);
129  return AVERROR(ENOMEM);
130  }
131 
133  mmal_buffer_header_acquire(buffer);
134 
135  frame->format = AV_PIX_FMT_MMAL;
136  frame->data[3] = (uint8_t *)ref->buffer;
137  return 0;
138 }
139 
141 {
142  MMALDecodeContext *ctx = avctx->priv_data;
143  MMAL_COMPONENT_T *decoder = ctx->decoder;
144  MMAL_BUFFER_HEADER_T *buffer;
145 
146  mmal_port_disable(decoder->input[0]);
147  mmal_port_disable(decoder->output[0]);
148  mmal_port_disable(decoder->control);
149 
150  mmal_port_flush(decoder->input[0]);
151  mmal_port_flush(decoder->output[0]);
152  mmal_port_flush(decoder->control);
153 
154  while ((buffer = mmal_queue_get(ctx->queue_decoded_frames)))
155  mmal_buffer_header_release(buffer);
156 
157  while (ctx->waiting_buffers) {
158  FFBufferEntry *buffer = ctx->waiting_buffers;
159 
160  ctx->waiting_buffers = buffer->next;
161 
162  av_buffer_unref(&buffer->ref);
163  av_free(buffer);
164  }
165  ctx->waiting_buffers_tail = NULL;
166 
167  ctx->frames_output = ctx->eos_received = ctx->eos_sent = ctx->packets_sent = 0;
168 }
169 
171 {
172  MMALDecodeContext *ctx = avctx->priv_data;
173 
174  if (ctx->decoder)
175  ffmmal_stop_decoder(avctx);
176 
177  mmal_component_destroy(ctx->decoder);
178  ctx->decoder = NULL;
179  mmal_queue_destroy(ctx->queue_decoded_frames);
180  mmal_pool_destroy(ctx->pool_in);
182 
183  if (ctx->bsfc)
185 
186  mmal_vc_deinit();
187 
188  return 0;
189 }
190 
191 static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
192 {
193  if (!buffer->cmd) {
194  AVBufferRef *buf = buffer->user_data;
195  av_buffer_unref(&buf);
196  }
197  mmal_buffer_header_release(buffer);
198 }
199 
200 static void output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
201 {
202  AVCodecContext *avctx = (AVCodecContext*)port->userdata;
203  MMALDecodeContext *ctx = avctx->priv_data;
204 
205  mmal_queue_put(ctx->queue_decoded_frames, buffer);
206 }
207 
208 static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
209 {
210  AVCodecContext *avctx = (AVCodecContext*)port->userdata;
211  MMAL_STATUS_T status;
212 
213  if (buffer->cmd == MMAL_EVENT_ERROR) {
214  status = *(uint32_t *)buffer->data;
215  av_log(avctx, AV_LOG_ERROR, "MMAL error %d on control port\n", (int)status);
216  } else {
217  char s[20];
218  av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
219  av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on control port\n", s);
220  }
221 
222  mmal_buffer_header_release(buffer);
223 }
224 
225 // Feed free output buffers to the decoder.
227 {
228  MMALDecodeContext *ctx = avctx->priv_data;
229  MMAL_BUFFER_HEADER_T *buffer;
230  MMAL_STATUS_T status;
231 
232  if (!ctx->pool_out)
233  return AVERROR_UNKNOWN; // format change code failed with OOM previously
234 
235  while ((buffer = mmal_queue_get(ctx->pool_out->pool->queue))) {
236  if ((status = mmal_port_send_buffer(ctx->decoder->output[0], buffer))) {
237  mmal_buffer_header_release(buffer);
238  av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending output buffer.\n", (int)status);
239  return AVERROR_UNKNOWN;
240  }
241  }
242 
243  return 0;
244 }
245 
246 static enum AVColorSpace ffmmal_csp_to_av_csp(MMAL_FOURCC_T fourcc)
247 {
248  switch (fourcc) {
249  case MMAL_COLOR_SPACE_BT470_2_BG:
250  case MMAL_COLOR_SPACE_BT470_2_M:
251  case MMAL_COLOR_SPACE_ITUR_BT601: return AVCOL_SPC_BT470BG;
252  case MMAL_COLOR_SPACE_ITUR_BT709: return AVCOL_SPC_BT709;
253  case MMAL_COLOR_SPACE_FCC: return AVCOL_SPC_FCC;
254  case MMAL_COLOR_SPACE_SMPTE240M: return AVCOL_SPC_SMPTE240M;
255  default: return AVCOL_SPC_UNSPECIFIED;
256  }
257 }
258 
260 {
261  MMALDecodeContext *ctx = avctx->priv_data;
262  MMAL_STATUS_T status;
263  int ret = 0;
264  MMAL_COMPONENT_T *decoder = ctx->decoder;
265  MMAL_ES_FORMAT_T *format_out = decoder->output[0]->format;
266 
268  if (!(ctx->pool_out = av_mallocz(sizeof(*ctx->pool_out)))) {
269  ret = AVERROR(ENOMEM);
270  goto fail;
271  }
272  ctx->pool_out->refcount = 1;
273 
274  if (!format_out)
275  goto fail;
276 
277  if ((status = mmal_port_parameter_set_uint32(decoder->output[0], MMAL_PARAMETER_EXTRA_BUFFERS, ctx->extra_buffers)))
278  goto fail;
279 
280  if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
281  format_out->encoding = MMAL_ENCODING_OPAQUE;
282  } else {
283  format_out->encoding_variant = format_out->encoding = MMAL_ENCODING_I420;
284  }
285 
286  if ((status = mmal_port_format_commit(decoder->output[0])))
287  goto fail;
288 
289  if ((ret = ff_set_dimensions(avctx, format_out->es->video.crop.x + format_out->es->video.crop.width,
290  format_out->es->video.crop.y + format_out->es->video.crop.height)) < 0)
291  goto fail;
292 
293  if (format_out->es->video.par.num && format_out->es->video.par.den) {
294  avctx->sample_aspect_ratio.num = format_out->es->video.par.num;
295  avctx->sample_aspect_ratio.den = format_out->es->video.par.den;
296  }
297 
298  avctx->colorspace = ffmmal_csp_to_av_csp(format_out->es->video.color_space);
299 
300  decoder->output[0]->buffer_size =
301  FFMAX(decoder->output[0]->buffer_size_min, decoder->output[0]->buffer_size_recommended);
302  decoder->output[0]->buffer_num =
303  FFMAX(decoder->output[0]->buffer_num_min, decoder->output[0]->buffer_num_recommended) + ctx->extra_buffers;
304  ctx->pool_out->pool = mmal_pool_create(decoder->output[0]->buffer_num,
305  decoder->output[0]->buffer_size);
306  if (!ctx->pool_out->pool) {
307  ret = AVERROR(ENOMEM);
308  goto fail;
309  }
310 
311  return 0;
312 
313 fail:
314  return ret < 0 ? ret : AVERROR_UNKNOWN;
315 }
316 
318 {
319  MMALDecodeContext *ctx = avctx->priv_data;
320  MMAL_STATUS_T status;
321  MMAL_ES_FORMAT_T *format_in;
322  MMAL_COMPONENT_T *decoder;
323  int ret = 0;
324 
325  bcm_host_init();
326 
327  if (mmal_vc_init()) {
328  av_log(avctx, AV_LOG_ERROR, "Cannot initialize MMAL VC driver!\n");
329  return AVERROR(ENOSYS);
330  }
331 
332  if ((ret = ff_get_format(avctx, avctx->codec->pix_fmts)) < 0)
333  return ret;
334 
335  avctx->pix_fmt = ret;
336 
337  if ((status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, &ctx->decoder)))
338  goto fail;
339 
340  decoder = ctx->decoder;
341 
342  format_in = decoder->input[0]->format;
343  format_in->type = MMAL_ES_TYPE_VIDEO;
344  format_in->encoding = MMAL_ENCODING_H264;
345  format_in->es->video.width = FFALIGN(avctx->width, 32);
346  format_in->es->video.height = FFALIGN(avctx->height, 16);
347  format_in->es->video.crop.width = avctx->width;
348  format_in->es->video.crop.height = avctx->height;
349  format_in->es->video.frame_rate.num = 24000;
350  format_in->es->video.frame_rate.den = 1001;
351  format_in->es->video.par.num = avctx->sample_aspect_ratio.num;
352  format_in->es->video.par.den = avctx->sample_aspect_ratio.den;
353  format_in->flags = MMAL_ES_FORMAT_FLAG_FRAMED;
354 
355  if (avctx->codec->id == AV_CODEC_ID_H264 && avctx->extradata && avctx->extradata[0] == 1) {
356  uint8_t *dummy_p;
357  int dummy_int;
358  ctx->bsfc = av_bitstream_filter_init("h264_mp4toannexb");
359  if (!ctx->bsfc) {
360  av_log(avctx, AV_LOG_ERROR, "Cannot open the h264_mp4toannexb BSF!\n");
361  ret = AVERROR(ENOSYS);
362  goto fail;
363  }
364  av_bitstream_filter_filter(ctx->bsfc, avctx, NULL, &dummy_p, &dummy_int, NULL, 0, 0);
365  }
366 
367  if (avctx->extradata_size) {
368  if ((status = mmal_format_extradata_alloc(format_in, avctx->extradata_size)))
369  goto fail;
370  format_in->extradata_size = avctx->extradata_size;
371  memcpy(format_in->extradata, avctx->extradata, format_in->extradata_size);
372  }
373 
374  if ((status = mmal_port_format_commit(decoder->input[0])))
375  goto fail;
376 
377  decoder->input[0]->buffer_num =
378  FFMAX(decoder->input[0]->buffer_num_min, 20);
379  decoder->input[0]->buffer_size =
380  FFMAX(decoder->input[0]->buffer_size_min, 512 * 1024);
381  ctx->pool_in = mmal_pool_create(decoder->input[0]->buffer_num, 0);
382  if (!ctx->pool_in) {
383  ret = AVERROR(ENOMEM);
384  goto fail;
385  }
386 
387  if ((ret = ffmal_update_format(avctx)) < 0)
388  goto fail;
389 
390  ctx->queue_decoded_frames = mmal_queue_create();
391  if (!ctx->queue_decoded_frames)
392  goto fail;
393 
394  decoder->input[0]->userdata = (void*)avctx;
395  decoder->output[0]->userdata = (void*)avctx;
396  decoder->control->userdata = (void*)avctx;
397 
398  if ((status = mmal_port_enable(decoder->control, control_port_cb)))
399  goto fail;
400  if ((status = mmal_port_enable(decoder->input[0], input_callback)))
401  goto fail;
402  if ((status = mmal_port_enable(decoder->output[0], output_callback)))
403  goto fail;
404 
405  if ((status = mmal_component_enable(decoder)))
406  goto fail;
407 
408  return 0;
409 
410 fail:
411  ffmmal_close_decoder(avctx);
412  return ret < 0 ? ret : AVERROR_UNKNOWN;
413 }
414 
415 static void ffmmal_flush(AVCodecContext *avctx)
416 {
417  MMALDecodeContext *ctx = avctx->priv_data;
418  MMAL_COMPONENT_T *decoder = ctx->decoder;
419  MMAL_STATUS_T status;
420 
421  ffmmal_stop_decoder(avctx);
422 
423  if ((status = mmal_port_enable(decoder->control, control_port_cb)))
424  goto fail;
425  if ((status = mmal_port_enable(decoder->input[0], input_callback)))
426  goto fail;
427  if ((status = mmal_port_enable(decoder->output[0], output_callback)))
428  goto fail;
429 
430  return;
431 
432 fail:
433  av_log(avctx, AV_LOG_ERROR, "MMAL flush error: %i\n", (int)status);
434 }
435 
436 // Split packets and add them to the waiting_buffers list. We don't queue them
437 // immediately, because it can happen that the decoder is temporarily blocked
438 // (due to us not reading/returning enough output buffers) and won't accept
439 // new input. (This wouldn't be an issue if MMAL input buffers always were
440 // complete frames - then the input buffer just would have to be big enough.)
441 static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt)
442 {
443  MMALDecodeContext *ctx = avctx->priv_data;
444  AVBufferRef *buf = NULL;
445  int size = 0;
446  uint8_t *data = (uint8_t *)"";
447  uint8_t *start;
448  int ret = 0;
449 
450  ctx->packets_sent++;
451 
452  if (avpkt->size) {
453  if (ctx->bsfc) {
454  uint8_t *tmp_data;
455  int tmp_size;
456  if ((ret = av_bitstream_filter_filter(ctx->bsfc, avctx, NULL,
457  &tmp_data, &tmp_size,
458  avpkt->data, avpkt->size,
459  avpkt->flags & AV_PKT_FLAG_KEY)) < 0)
460  goto done;
461  buf = av_buffer_create(tmp_data, tmp_size, NULL, NULL, 0);
462  } else {
463  if (avpkt->buf) {
464  buf = av_buffer_ref(avpkt->buf);
465  } else {
466  buf = av_buffer_alloc(avpkt->size);
467  if (buf)
468  memcpy(buf->data, avpkt->data, avpkt->size);
469  }
470  }
471  if (!buf) {
472  ret = AVERROR(ENOMEM);
473  goto done;
474  }
475  size = buf->size;
476  data = buf->data;
477  }
478 
479  start = data;
480 
481  do {
482  FFBufferEntry *buffer = av_mallocz(sizeof(*buffer));
483  if (!buffer) {
484  ret = AVERROR(ENOMEM);
485  goto done;
486  }
487 
488  buffer->data = data;
489  buffer->length = FFMIN(size, ctx->decoder->input[0]->buffer_size);
490 
491  if (data == start)
492  buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
493 
494  data += buffer->length;
495  size -= buffer->length;
496 
497  buffer->pts = avpkt->pts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->pts;
498  buffer->dts = avpkt->dts == AV_NOPTS_VALUE ? MMAL_TIME_UNKNOWN : avpkt->dts;
499 
500  if (!size)
501  buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
502 
503  if (!buffer->length) {
504  buffer->flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
505  ctx->eos_sent = 1;
506  }
507 
508  if (buf) {
509  buffer->ref = av_buffer_ref(buf);
510  if (!buffer->ref) {
511  av_free(buffer);
512  ret = AVERROR(ENOMEM);
513  goto done;
514  }
515  }
516 
517  // Insert at end of the list
518  if (!ctx->waiting_buffers)
519  ctx->waiting_buffers = buffer;
520  if (ctx->waiting_buffers_tail)
523  } while (size);
524 
525 done:
526  av_buffer_unref(&buf);
527  return ret;
528 }
529 
530 // Move prepared/split packets from waiting_buffers to the MMAL decoder.
532 {
533  MMALDecodeContext *ctx = avctx->priv_data;
534 
535  while (ctx->waiting_buffers) {
536  MMAL_BUFFER_HEADER_T *mbuffer;
538  MMAL_STATUS_T status;
539 
540  mbuffer = mmal_queue_get(ctx->pool_in->queue);
541  if (!mbuffer)
542  return 0;
543 
544  buffer = ctx->waiting_buffers;
545 
546  mmal_buffer_header_reset(mbuffer);
547  mbuffer->cmd = 0;
548  mbuffer->pts = buffer->pts;
549  mbuffer->dts = buffer->dts;
550  mbuffer->flags = buffer->flags;
551  mbuffer->data = buffer->data;
552  mbuffer->length = buffer->length;
553  mbuffer->user_data = buffer->ref;
554  mbuffer->alloc_size = ctx->decoder->input[0]->buffer_size;
555 
556  if ((status = mmal_port_send_buffer(ctx->decoder->input[0], mbuffer))) {
557  mmal_buffer_header_release(mbuffer);
558  av_buffer_unref(&buffer->ref);
559  }
560 
561  // Remove from start of the list
562  ctx->waiting_buffers = buffer->next;
563  if (ctx->waiting_buffers_tail == buffer)
564  ctx->waiting_buffers_tail = NULL;
565  av_free(buffer);
566 
567  if (status) {
568  av_log(avctx, AV_LOG_ERROR, "MMAL error %d when sending input\n", (int)status);
569  return AVERROR_UNKNOWN;
570  }
571  }
572 
573  return 0;
574 }
575 
577  MMAL_BUFFER_HEADER_T *buffer)
578 {
579  MMALDecodeContext *ctx = avctx->priv_data;
580  int ret = 0;
581 
582  if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
583  if (!ctx->pool_out)
584  return AVERROR_UNKNOWN; // format change code failed with OOM previously
585 
586  if ((ret = ff_decode_frame_props(avctx, frame)) < 0)
587  goto done;
588 
589  if ((ret = ffmmal_set_ref(frame, ctx->pool_out, buffer)) < 0)
590  goto done;
591  } else {
592  int w = FFALIGN(avctx->width, 32);
593  int h = FFALIGN(avctx->height, 16);
594  char *ptr;
595  int plane;
596  int i;
597 
598  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
599  goto done;
600 
601  ptr = buffer->data + buffer->type->video.offset[0];
602  for (i = 0; i < avctx->height; i++)
603  memcpy(frame->data[0] + frame->linesize[0] * i, ptr + w * i, avctx->width);
604 
605  ptr += w * h;
606 
607  for (plane = 1; plane < 3; plane++) {
608  for (i = 0; i < avctx->height / 2; i++)
609  memcpy(frame->data[plane] + frame->linesize[plane] * i, ptr + w / 2 * i, (avctx->width + 1) / 2);
610  ptr += w / 2 * h / 2;
611  }
612  }
613 
614  if (buffer->pts != MMAL_TIME_UNKNOWN) {
615  frame->pkt_pts = buffer->pts;
616  frame->pts = buffer->pts;
617  }
618 
619 done:
620  return ret;
621 }
622 
623 // Fetch a decoded buffer and place it into the frame parameter.
624 static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
625 {
626  MMALDecodeContext *ctx = avctx->priv_data;
627  MMAL_BUFFER_HEADER_T *buffer = NULL;
628  MMAL_STATUS_T status = 0;
629  int ret = 0;
630 
631  if (ctx->eos_received)
632  goto done;
633 
634  while (1) {
635  // To ensure decoding in lockstep with a constant delay between fed packets
636  // and output frames, we always wait until an output buffer is available.
637  // Except during start we don't know after how many input packets the decoder
638  // is going to return the first buffer, and we can't distinguish decoder
639  // being busy from decoder waiting for input. So just poll at the start and
640  // keep feeding new data to the buffer.
641  // We are pretty sure the decoder will produce output if we sent more input
642  // frames than what a h264 decoder could logically delay. This avoids too
643  // excessive buffering.
644  // We also wait if we sent eos, but didn't receive it yet (think of decoding
645  // stream with a very low number of frames).
646  if (ctx->frames_output || ctx->packets_sent > MAX_DELAYED_FRAMES || ctx->eos_sent) {
647  buffer = mmal_queue_wait(ctx->queue_decoded_frames);
648  } else {
649  buffer = mmal_queue_get(ctx->queue_decoded_frames);
650  }
651  if (!buffer)
652  goto done;
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  AVFrame *frame = data;
719  int ret = 0;
720 
721  if ((ret = ffmmal_add_packet(avctx, avpkt)) < 0)
722  return ret;
723 
724  if ((ret = ffmmal_fill_input_port(avctx)) < 0)
725  return ret;
726 
727  if ((ret = ffmmal_fill_output_port(avctx)) < 0)
728  return ret;
729 
730  if ((ret = ffmmal_read_frame(avctx, frame, got_frame)) < 0)
731  return ret;
732 
733  // ffmmal_read_frame() can block for a while. Since the decoder is
734  // asynchronous, it's a good idea to fill the ports again.
735 
736  if ((ret = ffmmal_fill_output_port(avctx)) < 0)
737  return ret;
738 
739  if ((ret = ffmmal_fill_input_port(avctx)) < 0)
740  return ret;
741 
742  return ret;
743 }
744 
746  .name = "h264_mmal",
747  .type = AVMEDIA_TYPE_VIDEO,
748  .id = AV_CODEC_ID_H264,
749  .pix_fmt = AV_PIX_FMT_MMAL,
750 };
751 
752 static const AVOption options[]={
753  {"extra_buffers", "extra buffers", offsetof(MMALDecodeContext, extra_buffers), AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
754  {NULL}
755 };
756 
757 static const AVClass ffmmaldec_class = {
758  .class_name = "mmaldec",
759  .option = options,
760  .version = LIBAVUTIL_VERSION_INT,
761 };
762 
764  .name = "h264_mmal",
765  .long_name = NULL_IF_CONFIG_SMALL("h264 (mmal)"),
766  .type = AVMEDIA_TYPE_VIDEO,
767  .id = AV_CODEC_ID_H264,
768  .priv_data_size = sizeof(MMALDecodeContext),
770  .close = ffmmal_close_decoder,
772  .flush = ffmmal_flush,
773  .priv_class = &ffmmaldec_class,
774  .capabilities = CODEC_CAP_DELAY,
775  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL,
778 };
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:502
int plane
Definition: avisynth_c.h:291
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1250
#define avpriv_atomic_int_add_and_fetch
Definition: atomic_gcc.h:50
const char * s
Definition: avisynth_c.h:631
MMAL_POOL_T * pool
Definition: mmaldec.c:57
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:124
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:229
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:441
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:506
FFPoolRef * pool
Definition: mmaldec.c:62
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1163
AVHWAccel ff_h264_mmal_hwaccel
Definition: mmaldec.c:745
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1623
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
static int ffmmal_fill_output_port(AVCodecContext *avctx)
Definition: mmaldec.c:226
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
Put a string representing the codec tag codec_tag in buf.
Definition: utils.c:2990
int64_t dts
Definition: mmaldec.c:47
AVCodec.
Definition: avcodec.h:3181
static int ffmmal_fill_input_port(AVCodecContext *avctx)
Definition: mmaldec.c:531
static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mmaldec.c:715
#define FFALIGN(x, a)
Definition: common.h:71
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
if()
Definition: avfilter.c:975
uint8_t
#define av_cold
Definition: attributes.h:74
AVOptions.
MMAL_BUFFER_HEADER_T * buffer
Definition: mmaldec.c:61
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:500
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data.
Definition: utils.c:868
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1355
static AVFrame * frame
static void ffmmal_flush(AVCodecContext *avctx)
Definition: mmaldec.c:415
uint8_t * data
Definition: avcodec.h:1162
static av_cold int ffmmal_init_decoder(AVCodecContext *avctx)
Definition: mmaldec.c:317
static const AVOption options[]
Definition: mmaldec.c:752
static av_cold int ffmmal_close_decoder(AVCodecContext *avctx)
Definition: mmaldec.c:170
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:113
MMAL_POOL_T * pool_in
Definition: mmaldec.c:73
ptrdiff_t size
Definition: opengl_enc.c:101
static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt)
Definition: mmaldec.c:441
volatile int refcount
Definition: mmaldec.c:56
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1208
enum AVCodecID id
Definition: avcodec.h:3195
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
Definition: mmaldec.c:200
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:824
#define AVERROR(e)
Definition: error.h:43
static void ffmmal_poolref_unref(FFPoolRef *ref)
Definition: mmaldec.c:94
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void av_bitstream_filter_close(AVBitStreamFilterContext *bsf)
Release bitstream filter context.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1145
simple assert() macros that are a bit more flexible than ISO C assert().
static int ffmmal_set_ref(AVFrame *frame, FFPoolRef *pool, MMAL_BUFFER_HEADER_T *buffer)
Definition: mmaldec.c:114
FFBufferEntry * waiting_buffers_tail
Definition: mmaldec.c:82
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
#define FFMAX(a, b)
Definition: common.h:64
Libavcodec external API header.
static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
Definition: mmaldec.c:208
AVBufferRef * ref
Definition: mmaldec.c:44
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1168
size_t length
Definition: mmaldec.c:46
static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame, MMAL_BUFFER_HEADER_T *buffer)
Definition: mmaldec.c:576
AVClass * av_class
Definition: mmaldec.c:66
static enum AVColorSpace ffmmal_csp_to_av_csp(MMAL_FOURCC_T fourcc)
Definition: mmaldec.c:246
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3202
FFPoolRef * pool_out
Definition: mmaldec.c:74
static const AVClass ffmmaldec_class
Definition: mmaldec.c:757
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3294
#define FFMIN(a, b)
Definition: common.h:66
FFBufferEntry * waiting_buffers
Definition: mmaldec.c:82
int64_t frames_output
Definition: mmaldec.c:85
ret
Definition: avfilter.c:974
static const chunk_decoder decoder[8]
Definition: dfa.c:327
int width
picture width / height.
Definition: avcodec.h:1414
AVBitStreamFilterContext * av_bitstream_filter_init(const char *name)
Create and initialize a bitstream filter context given a bitstream filter name.
#define MAX_DELAYED_FRAMES
Definition: mmaldec.c:92
AVCodec ff_h264_mmal_decoder
Definition: mmaldec.c:763
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:505
static void ffmmal_stop_decoder(AVCodecContext *avctx)
Definition: mmaldec.c:140
MMAL_QUEUE_T * queue_decoded_frames
Definition: mmaldec.c:72
static void flush(AVCodecContext *avctx)
Definition: aacdec.c:514
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: utils.c:1198
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
Filter bitstream.
struct FFBufferEntry * next
Definition: mmaldec.c:49
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:66
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1241
uint8_t * data
The data buffer.
Definition: buffer.h:89
void * data
Definition: mmaldec.c:45
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1035
static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
Definition: mmaldec.c:191
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1356
Describe the class of an AVClass context structure.
Definition: log.h:67
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1953
refcounted data buffer API
int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
Definition: frame.h:262
int size
Size of data in bytes.
Definition: buffer.h:93
static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
Definition: mmaldec.c:624
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
int64_t packets_sent
Definition: mmaldec.c:84
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:522
A reference to a data buffer.
Definition: buffer.h:81
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
common internal api header.
common internal and external API header
static int ffmal_update_format(AVCodecContext *avctx)
Definition: mmaldec.c:259
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
int den
denominator
Definition: rational.h:45
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
static void ffmmal_release_frame(void *opaque, uint8_t *data)
Definition: mmaldec.c:102
void * priv_data
Definition: avcodec.h:1283
#define av_free(p)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1161
void INT64 start
Definition: avisynth_c.h:553
AVBitStreamFilterContext * bsfc
Definition: mmaldec.c:69
HW acceleration though MMAL, data[3] contains a pointer to the MMAL_BUFFER_HEADER_T structure...
Definition: pixfmt.h:254
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1139
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:250
MMAL_COMPONENT_T * decoder
Definition: mmaldec.c:71
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1155
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241
GLuint buffer
Definition: opengl_enc.c:102
int64_t pts
Definition: mmaldec.c:47