]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mmaldec.c
Merge commit '2651352988212531038326c44754ece1728c4a3b'
[ffmpeg] / libavcodec / mmaldec.c
index 3e480ab49abe170e90b5a92fff9b64f77b879905..647a22ef7c07cc559de9f0d0ca2ad65d68cb7cf0 100644 (file)
@@ -2,20 +2,20 @@
  * MMAL Video Decoder
  * Copyright (c) 2015 Rodger Combs
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -34,6 +34,7 @@
 #include <stdatomic.h>
 
 #include "avcodec.h"
+#include "hwaccel.h"
 #include "internal.h"
 #include "libavutil/avassert.h"
 #include "libavutil/buffer.h"
@@ -88,6 +89,8 @@ typedef struct MMALDecodeContext {
     int eos_received;
     int eos_sent;
     int extradata_sent;
+    int interlaced_frame;
+    int top_field_first;
 } MMALDecodeContext;
 
 // Assume decoder is guaranteed to produce output after at least this many
@@ -226,9 +229,8 @@ static void control_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
         status = *(uint32_t *)buffer->data;
         av_log(avctx, AV_LOG_ERROR, "MMAL error %d on control port\n", (int)status);
     } else {
-        char s[20];
-        av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
-        av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on control port\n", s);
+        av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on control port\n",
+               av_fourcc2str(buffer->cmd));
     }
 
     mmal_buffer_header_release(buffer);
@@ -275,6 +277,7 @@ static int ffmal_update_format(AVCodecContext *avctx)
     int ret = 0;
     MMAL_COMPONENT_T *decoder = ctx->decoder;
     MMAL_ES_FORMAT_T *format_out = decoder->output[0]->format;
+    MMAL_PARAMETER_VIDEO_INTERLACE_TYPE_T interlace_type;
 
     ffmmal_poolref_unref(ctx->pool_out);
     if (!(ctx->pool_out = av_mallocz(sizeof(*ctx->pool_out)))) {
@@ -301,6 +304,16 @@ static int ffmal_update_format(AVCodecContext *avctx)
     if ((status = mmal_port_format_commit(decoder->output[0])))
         goto fail;
 
+    interlace_type.hdr.id = MMAL_PARAMETER_VIDEO_INTERLACE_TYPE;
+    interlace_type.hdr.size = sizeof(MMAL_PARAMETER_VIDEO_INTERLACE_TYPE_T);
+    status = mmal_port_parameter_get(decoder->output[0], &interlace_type.hdr);
+    if (status != MMAL_SUCCESS) {
+        av_log(avctx, AV_LOG_ERROR, "Cannot read MMAL interlace information!\n");
+    } else {
+        ctx->interlaced_frame = (interlace_type.eMode != MMAL_InterlaceProgressive);
+        ctx->top_field_first = (interlace_type.eMode == MMAL_InterlaceFieldsInterleavedUpperFirst);
+    }
+
     if ((ret = ff_set_dimensions(avctx, format_out->es->video.crop.x + format_out->es->video.crop.width,
                                         format_out->es->video.crop.y + format_out->es->video.crop.height)) < 0)
         goto fail;
@@ -309,6 +322,10 @@ static int ffmal_update_format(AVCodecContext *avctx)
         avctx->sample_aspect_ratio.num = format_out->es->video.par.num;
         avctx->sample_aspect_ratio.den = format_out->es->video.par.den;
     }
+    if (format_out->es->video.frame_rate.num && format_out->es->video.frame_rate.den) {
+        avctx->framerate.num = format_out->es->video.frame_rate.num;
+        avctx->framerate.den = format_out->es->video.frame_rate.den;
+    }
 
     avctx->colorspace = ffmmal_csp_to_av_csp(format_out->es->video.color_space);
 
@@ -335,7 +352,6 @@ static av_cold int ffmmal_init_decoder(AVCodecContext *avctx)
     MMAL_STATUS_T status;
     MMAL_ES_FORMAT_T *format_in;
     MMAL_COMPONENT_T *decoder;
-    char tmp[32];
     int ret = 0;
 
     bcm_host_init();
@@ -361,6 +377,9 @@ static av_cold int ffmmal_init_decoder(AVCodecContext *avctx)
     case AV_CODEC_ID_MPEG2VIDEO:
         format_in->encoding = MMAL_ENCODING_MP2V;
         break;
+    case AV_CODEC_ID_MPEG4:
+        format_in->encoding = MMAL_ENCODING_MP4V;
+        break;
     case AV_CODEC_ID_VC1:
         format_in->encoding = MMAL_ENCODING_WVC1;
         break;
@@ -379,8 +398,8 @@ static av_cold int ffmmal_init_decoder(AVCodecContext *avctx)
     format_in->es->video.par.den = avctx->sample_aspect_ratio.den;
     format_in->flags = MMAL_ES_FORMAT_FLAG_FRAMED;
 
-    av_get_codec_tag_string(tmp, sizeof(tmp), format_in->encoding);
-    av_log(avctx, AV_LOG_DEBUG, "Using MMAL %s encoding.\n", tmp);
+    av_log(avctx, AV_LOG_DEBUG, "Using MMAL %s encoding.\n",
+           av_fourcc2str(format_in->encoding));
 
 #if HAVE_MMAL_PARAMETER_VIDEO_MAX_NUM_CALLBACKS
     if (mmal_port_parameter_set_uint32(decoder->input[0], MMAL_PARAMETER_VIDEO_MAX_NUM_CALLBACKS,
@@ -607,6 +626,9 @@ static int ffmal_copy_frame(AVCodecContext *avctx,  AVFrame *frame,
     MMALDecodeContext *ctx = avctx->priv_data;
     int ret = 0;
 
+    frame->interlaced_frame = ctx->interlaced_frame;
+    frame->top_field_first = ctx->top_field_first;
+
     if (avctx->pix_fmt == AV_PIX_FMT_MMAL) {
         if (!ctx->pool_out)
             return AVERROR_UNKNOWN; // format change code failed with OOM previously
@@ -719,9 +741,8 @@ static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_fra
             mmal_buffer_header_release(buffer);
             continue;
         } else if (buffer->cmd) {
-            char s[20];
-            av_get_codec_tag_string(s, sizeof(s), buffer->cmd);
-            av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n", s);
+            av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n",
+                   av_fourcc2str(buffer->cmd));
             goto done;
         } else if (buffer->length == 0) {
             // Unused output buffer that got drained after format change.
@@ -787,25 +808,9 @@ static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame,
     return ret;
 }
 
-AVHWAccel ff_h264_mmal_hwaccel = {
-    .name       = "h264_mmal",
-    .type       = AVMEDIA_TYPE_VIDEO,
-    .id         = AV_CODEC_ID_H264,
-    .pix_fmt    = AV_PIX_FMT_MMAL,
-};
-
-AVHWAccel ff_mpeg2_mmal_hwaccel = {
-    .name       = "mpeg2_mmal",
-    .type       = AVMEDIA_TYPE_VIDEO,
-    .id         = AV_CODEC_ID_MPEG2VIDEO,
-    .pix_fmt    = AV_PIX_FMT_MMAL,
-};
-
-AVHWAccel ff_vc1_mmal_hwaccel = {
-    .name       = "vc1_mmal",
-    .type       = AVMEDIA_TYPE_VIDEO,
-    .id         = AV_CODEC_ID_VC1,
-    .pix_fmt    = AV_PIX_FMT_MMAL,
+static const AVCodecHWConfigInternal *mmal_hw_configs[] = {
+    HW_CONFIG_INTERNAL(MMAL),
+    NULL
 };
 
 static const AVOption options[]={
@@ -840,9 +845,11 @@ static const AVOption options[]={
         .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_MMAL, \
                                                          AV_PIX_FMT_YUV420P, \
                                                          AV_PIX_FMT_NONE}, \
+        .hw_configs     = mmal_hw_configs, \
         .wrapper_name   = "mmal", \
     };
 
 FFMMAL_DEC(h264, AV_CODEC_ID_H264)
 FFMMAL_DEC(mpeg2, AV_CODEC_ID_MPEG2VIDEO)
+FFMMAL_DEC(mpeg4, AV_CODEC_ID_MPEG4)
 FFMMAL_DEC(vc1, AV_CODEC_ID_VC1)