[FFmpeg-devel] [PATCH] Possible fix for H264 with missing crop rectangle

Nigel Touati-Evans nigel.touatievans
Tue Oct 26 15:41:52 CEST 2010


have some 1080p h264 video inside a mov wrapper which ffmpeg decodes
as 1088 (with a black line along the bottom). Quicktime correctly
decodes as 1080. It seems that the camera does not write a cropping
rectangle to the h264 stream and relies on the Quicktime header.

I have made a patch (attached) to set the cropping rectangle based on
the header height if sensible, which fixes my video. However I'm not
sure this is the correct way to go about it (and I had to change
liibavcodec/utils.c to make an image size test weaker, though the new
condition is consistent with the conditions in the previous lines, so
maybe it is ok).

Nigel.
-------------- next part --------------
Index: libavcodec/utils.c
===================================================================
--- libavcodec/utils.c	(revision 25575)
+++ libavcodec/utils.c	(working copy)
@@ -491,7 +491,7 @@
     else if(avctx->width && avctx->height)
         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
 
-    if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
+    if (avctx->width && avctx->height
         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
Index: libavcodec/h264_ps.c
===================================================================
--- libavcodec/h264_ps.c	(revision 25575)
+++ libavcodec/h264_ps.c	(working copy)
@@ -381,6 +381,16 @@
         sps->crop_right =
         sps->crop_top   =
         sps->crop_bottom= 0;
+        if (h->s.avctx->height && sps->mb_height*16 - h->s.avctx->height == 8)
+        {
+            /* Slight hack to work around some cameras failure to set a cropping rectangle, instead relying on the size set in the file header */
+            av_log(h->s.avctx, AV_LOG_WARNING, "cropping rectangle appears to be missing, adding one to match header size\n");
+            sps->crop = 1;
+            if(sps->frame_mbs_only_flag)
+               sps->crop_bottom = 4;
+            else
+               sps->crop_bottom = 2;
+        }        
     }
 
     sps->vui_parameters_present_flag= get_bits1(&s->gb);
Index: libavformat/mov.c
===================================================================
--- libavformat/mov.c	(revision 25575)
+++ libavformat/mov.c	(working copy)
@@ -1855,8 +1855,7 @@
 #if CONFIG_MPEG4_DECODER
     case CODEC_ID_MPEG4:
 #endif
-        st->codec->width = 0; /* let decoder init width/height */
-        st->codec->height= 0;
+        st->codec->width = 0; /* let decoder init width/height but pass header height through for some 1088 vs 1080 videos */
         break;
     }
 



More information about the ffmpeg-devel mailing list