>From bb76e211dd129539f7f9c861d5815a93029f850e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Filip=20Ros=C3=A9en?= <filip@videolabs.io>
Date: Mon, 23 May 2016 15:37:56 +0200
Subject: [PATCH 2/2] input/decoder.c: fixed code-duplication

DecoderProcessVideo and DecoderProcessAudio had identical implementation
with the only difference being the function they called in order to
decode the passed block.

This patch introduces DecoderDispatchBlock, to which you pass a
pointer-to-function that is responsible for the decoding. This allows
for a shorter (and more maintainable) implementation of the relevant
functionality.

Given that there is no longer a need for DecoderProcessVideo and
DecoderProcessAudio, both functions have been removed.
---
 src/input/decoder.c | 169 +++++++++++++++++++---------------------------------
 1 file changed, 62 insertions(+), 107 deletions(-)

diff --git a/src/input/decoder.c b/src/input/decoder.c
index 113c105..8ce4a0a 100644
--- a/src/input/decoder.c
+++ b/src/input/decoder.c
@@ -985,60 +985,6 @@ static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
     DecoderUpdateStatVideo( p_dec, i_decoded, i_lost );
 }
 
-/* This function process a video block
- */
-static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block )
-{
-    decoder_owner_sys_t *p_owner = p_dec->p_owner;
-
-    if( p_owner->p_packetizer )
-    {
-        block_t *p_packetized_block;
-        block_t **pp_block = p_block ? &p_block : NULL;
-        decoder_t *p_packetizer = p_owner->p_packetizer;
-
-        while( (p_packetized_block =
-                p_packetizer->pf_packetize( p_packetizer, pp_block ) ) )
-        {
-            if( !es_format_IsSimilar( &p_dec->fmt_in, &p_packetizer->fmt_out ) )
-            {
-                msg_Dbg( p_dec, "restarting module due to input format change");
-
-                /* Drain the decoder module */
-                DecoderDecodeVideo( p_dec, NULL );
-                /* Restart the decoder module */
-                UnloadDecoder( p_dec );
-                if( LoadDecoder( p_dec, false, &p_packetizer->fmt_out ) )
-                {
-                    p_dec->b_error = true;
-                    block_ChainRelease( p_packetized_block );
-                    return;
-                }
-            }
-
-            if( p_packetizer->pf_get_cc )
-                DecoderGetCc( p_dec, p_packetizer );
-
-            while( p_packetized_block )
-            {
-                block_t *p_next = p_packetized_block->p_next;
-                p_packetized_block->p_next = NULL;
-
-                DecoderDecodeVideo( p_dec, p_packetized_block );
-
-                p_packetized_block = p_next;
-            }
-        }
-        /* Drain the decoder after the packetizer is drained */
-        if( !pp_block )
-            DecoderDecodeVideo( p_dec, NULL );
-    }
-    else
-    {
-        DecoderDecodeVideo( p_dec, p_block );
-    }
-}
-
 static int DecoderPlayAudio( decoder_t *p_dec, block_t *p_audio,
                              unsigned *restrict pi_lost_sum )
 {
@@ -1163,57 +1109,6 @@ static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
     DecoderUpdateStatAudio( p_dec, decoded, lost );
 }
 
-/* This function process a audio block
- */
-static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block )
-{
-    decoder_owner_sys_t *p_owner = p_dec->p_owner;
-
-    if( p_owner->p_packetizer )
-    {
-        block_t *p_packetized_block;
-        block_t **pp_block = p_block ? &p_block : NULL;
-        decoder_t *p_packetizer = p_owner->p_packetizer;
-
-        while( (p_packetized_block =
-                p_packetizer->pf_packetize( p_packetizer, pp_block ) ) )
-        {
-            if( !es_format_IsSimilar( &p_dec->fmt_in, &p_packetizer->fmt_out ) )
-            {
-                msg_Dbg( p_dec, "restarting module due to input format change");
-
-                /* Drain the decoder module */
-                DecoderDecodeAudio( p_dec, NULL );
-                /* Restart the decoder module */
-                UnloadDecoder( p_dec );
-                if( LoadDecoder( p_dec, false, &p_packetizer->fmt_out ) )
-                {
-                    p_dec->b_error = true;
-                    block_ChainRelease( p_packetized_block );
-                    return;
-                }
-            }
-
-            while( p_packetized_block )
-            {
-                block_t *p_next = p_packetized_block->p_next;
-                p_packetized_block->p_next = NULL;
-
-                DecoderDecodeAudio( p_dec, p_packetized_block );
-
-                p_packetized_block = p_next;
-            }
-        }
-        /* Drain the decoder after the packetizer is drained */
-        if( !pp_block )
-            DecoderDecodeAudio( p_dec, NULL );
-    }
-    else
-    {
-        DecoderDecodeAudio( p_dec, p_block );
-    }
-}
-
 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
@@ -1305,6 +1200,66 @@ static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block )
 }
 
 /**
+ * Dispatch a block to the relevant decode-callback
+ *
+ * \param p_dec the decoder object
+ * \param p_block the block to decode
+ * \param pf_decode pointer-to-function responsible for decoding the passed block
+ **/
+static void DecoderDispatchBlock( decoder_t * p_dec, block_t * p_block,
+  void( *pf_decode )( decoder_t*, block_t* ) )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    if( p_owner->p_packetizer )
+    {
+        block_t *p_packetized_block;
+        block_t **pp_block = p_block ? &p_block : NULL;
+        decoder_t *p_packetizer = p_owner->p_packetizer;
+
+        while( (p_packetized_block =
+                p_packetizer->pf_packetize( p_packetizer, pp_block ) ) )
+        {
+            if( !es_format_IsSimilar( &p_dec->fmt_in, &p_packetizer->fmt_out ) )
+            {
+                msg_Dbg( p_dec, "restarting module due to input format change");
+
+                /* Drain the decoder module */
+                pf_decode ( p_dec, NULL );
+                /* Restart the decoder module */
+                UnloadDecoder( p_dec );
+                if( LoadDecoder( p_dec, false, &p_packetizer->fmt_out ) )
+                {
+                    p_dec->b_error = true;
+                    block_ChainRelease( p_packetized_block );
+                    return;
+                }
+            }
+
+            if( p_packetizer->pf_get_cc )
+                DecoderGetCc( p_dec, p_packetizer );
+
+            while( p_packetized_block )
+            {
+                block_t *p_next = p_packetized_block->p_next;
+                p_packetized_block->p_next = NULL;
+
+                pf_decode ( p_dec, p_packetized_block );
+
+                p_packetized_block = p_next;
+            }
+        }
+        /* Drain the decoder after the packetizer is drained */
+        if( !pp_block )
+            pf_decode ( p_dec, NULL );
+    }
+    else
+    {
+        pf_decode ( p_dec, p_block );
+    }
+}
+
+/**
  * Decode a block
  *
  * \param p_dec the decoder object
@@ -1335,8 +1290,8 @@ static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
 
     switch( p_dec->fmt_out.i_cat )
     {
-        case VIDEO_ES: return DecoderProcessVideo( p_dec, p_block );
-        case AUDIO_ES: return DecoderProcessAudio( p_dec, p_block );
+        case VIDEO_ES: return DecoderDispatchBlock( p_dec, p_block, &DecoderDecodeVideo );
+        case AUDIO_ES: return DecoderDispatchBlock( p_dec, p_block, &DecoderDecodeAudio );
         case   SPU_ES: return DecoderProcessSpu( p_dec, p_block );
 
         default:
-- 
2.8.2

