28 #include <interface/mmal/mmal.h>
29 #include <interface/mmal/mmal_parameters_video.h>
30 #include <interface/mmal/util/mmal_util.h>
31 #include <interface/mmal/util/mmal_util_params.h>
32 #include <interface/mmal/util/mmal_default_components.h>
33 #include <interface/mmal/vc/mmal_vc_api.h>
34 #include <stdatomic.h>
97 #define MAX_DELAYED_FRAMES 16
103 mmal_pool_destroy(ref->
pool);
112 mmal_buffer_header_release(ref->
buffer);
121 MMAL_BUFFER_HEADER_T *
buffer)
133 if (!frame->
buf[0]) {
139 mmal_buffer_header_acquire(buffer);
150 MMAL_BUFFER_HEADER_T *
buffer;
152 mmal_port_disable(decoder->input[0]);
153 mmal_port_disable(decoder->output[0]);
154 mmal_port_disable(decoder->control);
156 mmal_port_flush(decoder->input[0]);
157 mmal_port_flush(decoder->output[0]);
158 mmal_port_flush(decoder->control);
161 mmal_buffer_header_release(buffer);
168 if (buffer->
flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
188 mmal_component_destroy(ctx->
decoder);
191 mmal_pool_destroy(ctx->
pool_in);
207 if (entry->
flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
211 mmal_buffer_header_release(buffer);
219 mmal_queue_put(
ctx->queue_decoded_frames, buffer);
225 MMAL_STATUS_T status;
227 if (buffer->cmd == MMAL_EVENT_ERROR) {
228 status = *(uint32_t *)buffer->data;
235 mmal_buffer_header_release(buffer);
242 MMAL_BUFFER_HEADER_T *
buffer;
243 MMAL_STATUS_T status;
248 while ((buffer = mmal_queue_get(ctx->
pool_out->
pool->queue))) {
249 if ((status = mmal_port_send_buffer(ctx->
decoder->output[0], buffer))) {
250 mmal_buffer_header_release(buffer);
251 av_log(avctx,
AV_LOG_ERROR,
"MMAL error %d when sending output buffer.\n", (
int)status);
262 case MMAL_COLOR_SPACE_BT470_2_BG:
263 case MMAL_COLOR_SPACE_BT470_2_M:
275 MMAL_STATUS_T status;
278 MMAL_ES_FORMAT_T *format_out = decoder->output[0]->format;
279 MMAL_PARAMETER_VIDEO_INTERLACE_TYPE_T interlace_type;
291 if ((status = mmal_port_parameter_set_uint32(decoder->output[0], MMAL_PARAMETER_EXTRA_BUFFERS, ctx->
extra_buffers)))
294 if ((status = mmal_port_parameter_set_boolean(decoder->output[0], MMAL_PARAMETER_VIDEO_INTERPOLATE_TIMESTAMPS, 0)))
298 format_out->encoding = MMAL_ENCODING_OPAQUE;
300 format_out->encoding_variant = format_out->encoding = MMAL_ENCODING_I420;
303 if ((status = mmal_port_format_commit(decoder->output[0])))
306 interlace_type.hdr.id = MMAL_PARAMETER_VIDEO_INTERLACE_TYPE;
307 interlace_type.hdr.size =
sizeof(MMAL_PARAMETER_VIDEO_INTERLACE_TYPE_T);
308 status = mmal_port_parameter_get(decoder->output[0], &interlace_type.hdr);
309 if (status != MMAL_SUCCESS) {
312 ctx->
interlaced_frame = (interlace_type.eMode != MMAL_InterlaceProgressive);
313 ctx->
top_field_first = (interlace_type.eMode == MMAL_InterlaceFieldsInterleavedUpperFirst);
317 format_out->es->video.crop.y + format_out->es->video.crop.height)) < 0)
320 if (format_out->es->video.par.num && format_out->es->video.par.den) {
324 if (format_out->es->video.frame_rate.num && format_out->es->video.frame_rate.den) {
325 avctx->
framerate.
num = format_out->es->video.frame_rate.num;
326 avctx->
framerate.
den = format_out->es->video.frame_rate.den;
331 decoder->output[0]->buffer_size =
332 FFMAX(decoder->output[0]->buffer_size_min, decoder->output[0]->buffer_size_recommended);
333 decoder->output[0]->buffer_num =
334 FFMAX(decoder->output[0]->buffer_num_min, decoder->output[0]->buffer_num_recommended) + ctx->
extra_buffers;
335 ctx->
pool_out->
pool = mmal_pool_create(decoder->output[0]->buffer_num,
336 decoder->output[0]->buffer_size);
351 MMAL_STATUS_T status;
352 MMAL_ES_FORMAT_T *format_in;
358 if (mmal_vc_init()) {
368 if ((status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, &ctx->
decoder)))
373 format_in = decoder->input[0]->format;
374 format_in->type = MMAL_ES_TYPE_VIDEO;
377 format_in->encoding = MMAL_ENCODING_MP2V;
380 format_in->encoding = MMAL_ENCODING_MP4V;
383 format_in->encoding = MMAL_ENCODING_WVC1;
387 format_in->encoding = MMAL_ENCODING_H264;
392 format_in->es->video.crop.width = avctx->
width;
393 format_in->es->video.crop.height = avctx->
height;
394 format_in->es->video.frame_rate.num = 24000;
395 format_in->es->video.frame_rate.den = 1001;
398 format_in->flags = MMAL_ES_FORMAT_FLAG_FRAMED;
403 #if HAVE_MMAL_PARAMETER_VIDEO_MAX_NUM_CALLBACKS
404 if (mmal_port_parameter_set_uint32(decoder->input[0], MMAL_PARAMETER_VIDEO_MAX_NUM_CALLBACKS,
410 if ((status = mmal_port_format_commit(decoder->input[0])))
413 decoder->input[0]->buffer_num =
414 FFMAX(decoder->input[0]->buffer_num_min, 20);
415 decoder->input[0]->buffer_size =
416 FFMAX(decoder->input[0]->buffer_size_min, 512 * 1024);
417 ctx->
pool_in = mmal_pool_create(decoder->input[0]->buffer_num, 0);
430 decoder->input[0]->userdata = (
void*)avctx;
431 decoder->output[0]->userdata = (
void*)avctx;
432 decoder->control->userdata = (
void*)avctx;
436 if ((status = mmal_port_enable(decoder->input[0],
input_callback)))
441 if ((status = mmal_component_enable(decoder)))
455 MMAL_STATUS_T status;
461 if ((status = mmal_port_enable(decoder->input[0],
input_callback)))
531 buffer->
flags |= MMAL_BUFFER_HEADER_FLAG_CONFIG;
534 buffer->
flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
543 buffer->
flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
548 buffer->
flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
580 MMAL_BUFFER_HEADER_T *mbuffer;
582 MMAL_STATUS_T status;
584 mbuffer = mmal_queue_get(ctx->
pool_in->queue);
590 mmal_buffer_header_reset(mbuffer);
592 mbuffer->pts = buffer->
pts;
593 mbuffer->dts = buffer->
dts;
594 mbuffer->flags = buffer->
flags;
595 mbuffer->data = buffer->
data;
596 mbuffer->length = buffer->
length;
597 mbuffer->user_data =
buffer;
598 mbuffer->alloc_size = ctx->
decoder->input[0]->buffer_size;
605 if ((status = mmal_port_send_buffer(ctx->
decoder->input[0], mbuffer))) {
606 mmal_buffer_header_release(mbuffer);
608 if (buffer->
flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
623 MMAL_BUFFER_HEADER_T *
buffer)
650 buffer->data + buffer->type->video.offset[0],
673 MMAL_STATUS_T status = 0;
709 ctx->
eos_received |= !!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS);
713 if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED) {
715 MMAL_EVENT_FORMAT_CHANGED_T *ev = mmal_event_format_changed_get(buffer);
716 MMAL_BUFFER_HEADER_T *stale_buffer;
720 if ((status = mmal_port_disable(decoder->output[0])))
724 mmal_buffer_header_release(stale_buffer);
726 mmal_format_copy(decoder->output[0]->format, ev->format);
740 mmal_buffer_header_release(buffer);
742 }
else if (buffer->cmd) {
746 }
else if (buffer->length == 0) {
748 mmal_buffer_header_release(buffer);
763 mmal_buffer_header_release(buffer);
764 if (status && ret >= 0)
818 .
name =
"mpeg2_mmal",
825 .
name =
"mpeg4_mmal",
840 {
"extra_decoder_buffers",
"extra MMAL internal buffered frames", offsetof(
MMALDecodeContext, extra_decoder_buffers),
AV_OPT_TYPE_INT, {.i64 = 10}, 0, 256, 0},
844 #define FFMMAL_DEC_CLASS(NAME) \
845 static const AVClass ffmmal_##NAME##_dec_class = { \
846 .class_name = "mmal_" #NAME "_dec", \
848 .version = LIBAVUTIL_VERSION_INT, \
851 #define FFMMAL_DEC(NAME, ID) \
852 FFMMAL_DEC_CLASS(NAME) \
853 AVCodec ff_##NAME##_mmal_decoder = { \
854 .name = #NAME "_mmal", \
855 .long_name = NULL_IF_CONFIG_SMALL(#NAME " (mmal)"), \
856 .type = AVMEDIA_TYPE_VIDEO, \
858 .priv_data_size = sizeof(MMALDecodeContext), \
859 .init = ffmmal_init_decoder, \
860 .close = ffmmal_close_decoder, \
861 .decode = ffmmal_decode, \
862 .flush = ffmmal_flush, \
863 .priv_class = &ffmmal_##NAME##_dec_class, \
864 .capabilities = AV_CODEC_CAP_DELAY, \
865 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
866 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL, \
867 AV_PIX_FMT_YUV420P, \
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
const struct AVCodec * codec
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
This structure describes decoded (raw) audio or video data.
#define FFMMAL_DEC(NAME, ID)
ptrdiff_t const GLvoid * data
#define AV_LOG_WARNING
Something somehow does not look correct.
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.
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
atomic_int packets_buffered
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4], const uint8_t *src, enum AVPixelFormat pix_fmt, int width, int height, int align)
Setup the data pointers and linesizes based on the specified image parameters and the provided array...
AVHWAccel ff_h264_mmal_hwaccel
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static int ffmmal_fill_output_port(AVCodecContext *avctx)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data.
static int ffmmal_fill_input_port(AVCodecContext *avctx)
static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
functionally identical to above
#define av_assert0(cond)
assert() equivalent, that is always enabled.
MMAL_BUFFER_HEADER_T * buffer
AVColorSpace
YUV colorspace type.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static void ffmmal_flush(AVCodecContext *avctx)
static av_cold int ffmmal_init_decoder(AVCodecContext *avctx)
static const AVOption options[]
static av_cold int ffmmal_close_decoder(AVCodecContext *avctx)
int interlaced_frame
The content of the picture is interlaced.
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
#define atomic_load(object)
static void ffmmal_poolref_unref(FFPoolRef *ref)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define av_fourcc2str(fourcc)
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
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)
FFBufferEntry * waiting_buffers_tail
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.
int extra_decoder_buffers
static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame, MMAL_BUFFER_HEADER_T *buffer)
static enum AVColorSpace ffmmal_csp_to_av_csp(MMAL_FOURCC_T fourcc)
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
const char * name
Name of the hardware accelerated codec.
FFBufferEntry * waiting_buffers
static const chunk_decoder decoder[8]
int width
picture width / height.
#define MAX_DELAYED_FRAMES
preferred ID for MPEG-1/2 video decoding
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
static void ffmmal_stop_decoder(AVCodecContext *avctx)
MMAL_QUEUE_T * queue_decoded_frames
#define atomic_fetch_add_explicit(object, operand, order)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
#define atomic_fetch_add(object, operand)
AVHWAccel ff_mpeg2_mmal_hwaccel
#define AV_LOG_INFO
Standard information.
struct FFBufferEntry * next
Libavcodec external API header.
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
uint8_t * data
The data buffer.
static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
AVHWAccel ff_vc1_mmal_hwaccel
Describe the class of an AVClass context structure.
enum AVColorSpace colorspace
YUV colorspace type.
refcounted data buffer API
int size
Size of data in bytes.
static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
AVHWAccel ff_mpeg4_mmal_hwaccel
attribute_deprecated int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
A reference to a data buffer.
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
common internal and external API header
static int ref[MAX_W *MAX_W]
static int ffmal_update_format(AVCodecContext *avctx)
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
static void ffmmal_release_frame(void *opaque, uint8_t *data)
#define FF_ENABLE_DEPRECATION_WARNINGS
int top_field_first
If the content is interlaced, is top field displayed first.
static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt, int is_extradata)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
#define atomic_init(obj, value)
HW acceleration though MMAL, data[3] contains a pointer to the MMAL_BUFFER_HEADER_T structure...
This structure stores compressed data.
MMAL_COMPONENT_T * decoder
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.