[FFmpeg-devel] [PATCH] avcodec/h264: Partially decode and display single fields try #2

Michael Niedermayer michaelni at gmx.at
Tue Jan 20 20:54:01 CET 2015


This like the previous attempt does not fully correctly decode this
type of non standard H.264, but it now works fully automatic
requiring no manual filters or flags to be used

See Ticket2254

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
 libavcodec/h264.c       |   31 ++++++++++++++++++++++++++++++-
 libavcodec/h264.h       |    2 ++
 libavcodec/h264_slice.c |    2 ++
 3 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index ab9acde..62a5c2b 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -762,7 +762,10 @@ static void decode_postinit(H264Context *h, int setup_finished)
          * yet, so we assume the worst for now. */
         // if (setup_finished)
         //    ff_thread_finish_setup(h->avctx);
-        return;
+        if (cur->field_poc[0] == INT_MAX && cur->field_poc[1] == INT_MAX)
+            return;
+        if (h->avctx->hwaccel || h->missing_fields <=1)
+            return;
     }
 
     cur->f.interlaced_frame = 0;
@@ -1912,6 +1915,32 @@ static int h264_decode_frame(AVCodecContext *avctx, void *data,
             if (!h->next_output_pic->recovered)
                 h->next_output_pic->f.flags |= AV_FRAME_FLAG_CORRUPT;
 
+            if (!h->avctx->hwaccel &&
+                 (h->next_output_pic->field_poc[0] == INT_MAX ||
+                  h->next_output_pic->field_poc[1] == INT_MAX)
+            ) {
+                int p, y;
+                int h_chroma_shift, v_chroma_shift;
+                AVFrame *f = &h->next_output_pic->f;
+                int field = h->next_output_pic->field_poc[0] == INT_MAX;
+
+                av_pix_fmt_get_chroma_sub_sample(f->format, &h_chroma_shift, &v_chroma_shift);
+                av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
+
+                for(p=0; p<3; p++) {
+                    int h = f->height;
+                    int w = f->width;
+                    if (p) {
+                        w >>= h_chroma_shift;
+                        h >>= v_chroma_shift;
+                    }
+                    for(y=field; y<h && (y^1)<h; y+=2) {
+                        memcpy(&f->data[p][ (y^1)*f->linesize[p] ],
+                            &f->data[p][ y*f->linesize[p] ], w);
+                    }
+                }
+            }
+
             ret = output_frame(h, pict, h->next_output_pic);
             if (ret < 0)
                 return ret;
diff --git a/libavcodec/h264.h b/libavcodec/h264.h
index 2d8e137..cf4998f 100644
--- a/libavcodec/h264.h
+++ b/libavcodec/h264.h
@@ -740,6 +740,8 @@ typedef struct H264Context {
 
     int has_recovery_point;
 
+    int missing_fields;
+
     int luma_weight_flag[2];    ///< 7.4.3.2 luma_weight_lX_flag
     int chroma_weight_flag[2];  ///< 7.4.3.2 chroma_weight_lX_flag
 
diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index 3874d07..6d19c73 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -1663,9 +1663,11 @@ int ff_h264_decode_slice_header(H264Context *h, H264Context *h0)
             if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
                 /* Previous field is unmatched. Don't display it, but let it
                  * remain for reference if marked as such. */
+                h0->missing_fields ++;
                 h0->cur_pic_ptr = NULL;
                 h0->first_field = FIELD_PICTURE(h);
             } else {
+                h0->missing_fields = 0;
                 if (h0->cur_pic_ptr->frame_num != h->frame_num) {
                     ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
                                               h0->picture_structure==PICT_BOTTOM_FIELD);
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list