diff --git a/modules/hw/mmal/codec.c b/modules/hw/mmal/codec.c
index c31ba0cf356c23b41c1bdd609ab730f27d94b4ac..5294c61e2fadd02902f0ea37396b39e2fd98aab4 100644
--- a/modules/hw/mmal/codec.c
+++ b/modules/hw/mmal/codec.c
@@ -669,6 +669,8 @@ static void output_port_cb(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
         if (sys->opaque)
             format->encoding = MMAL_ENCODING_OPAQUE;
 
+        if (sys->output_format)
+            mmal_format_free(sys->output_format);
         sys->output_format = format;
 
         mmal_buffer_header_release(buffer);