diff mbox

[FFmpeg-devel,09/18] h264_metadata: Localize code for display orientation

Message ID 20190617034223.21195-10-andreas.rheinhardt@gmail.com
State Accepted
Commit 3c8a2a1180f03ca6b299ebc27eef21ae86635ca0
Headers show

Commit Message

Andreas Rheinhardt June 17, 2019, 3:42 a.m. UTC
The recent changes to h264_metadata (enabled by the recent changes to
ff_cbs_write_packet) made it possible to add side_data to the output
packet at any place, not only after the output packet has been written
and the properties of the input packet copied. This means that one can
now localize the code to add display orientation side-data to the packet
to the place dealing with said display-orientation.

Furthermore, the documentation of av_display_rotation_set states that
the matrix will be fully overwritten by it, so there is no need to
allocate it with av_mallocz.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
That the display orientation code itself is very buggy has not been
fixed yet.

 libavcodec/h264_metadata_bsf.c | 33 ++++++++++++---------------------
 1 file changed, 12 insertions(+), 21 deletions(-)
diff mbox

Patch

diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c
index 18c5ae807d..f7ca1f0f09 100644
--- a/libavcodec/h264_metadata_bsf.c
+++ b/libavcodec/h264_metadata_bsf.c
@@ -289,8 +289,6 @@  static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
     CodedBitstreamFragment *au = &ctx->access_unit;
     int err, i, j, has_sps;
     H264RawAUD aud;
-    uint8_t *displaymatrix_side_data = NULL;
-    size_t displaymatrix_side_data_size = 0;
 
     err = ff_bsf_get_packet_ref(bsf, pkt);
     if (err < 0)
@@ -487,7 +485,7 @@  static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
                     continue;
                 }
 
-                matrix = av_mallocz(9 * sizeof(int32_t));
+                matrix = av_malloc(9 * sizeof(int32_t));
                 if (!matrix) {
                     err = AVERROR(ENOMEM);
                     goto fail;
@@ -499,11 +497,17 @@  static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
                 av_display_matrix_flip(matrix, disp->hor_flip, disp->ver_flip);
 
                 // If there are multiple display orientation messages in an
-                // access unit then ignore all but the first one.
-                av_freep(&displaymatrix_side_data);
-
-                displaymatrix_side_data      = (uint8_t*)matrix;
-                displaymatrix_side_data_size = 9 * sizeof(int32_t);
+                // access unit, then the last one added to the packet (i.e.
+                // the first one in the access unit) will prevail.
+                err = av_packet_add_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX,
+                                              (uint8_t*)matrix,
+                                              9 * sizeof(int32_t));
+                if (err < 0) {
+                    av_log(bsf, AV_LOG_ERROR, "Failed to attach extracted "
+                           "displaymatrix side data to packet.\n");
+                    av_freep(matrix);
+                    goto fail;
+                }
             }
         }
     }
@@ -583,24 +587,11 @@  static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
         goto fail;
     }
 
-    if (displaymatrix_side_data) {
-        err = av_packet_add_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX,
-                                      displaymatrix_side_data,
-                                      displaymatrix_side_data_size);
-        if (err) {
-            av_log(bsf, AV_LOG_ERROR, "Failed to attach extracted "
-                   "displaymatrix side data to packet.\n");
-            goto fail;
-        }
-        displaymatrix_side_data = NULL;
-    }
-
     ctx->done_first_au = 1;
 
     err = 0;
 fail:
     ff_cbs_fragment_reset(ctx->cbc, au);
-    av_freep(&displaymatrix_side_data);
 
     if (err < 0)
         av_packet_unref(pkt);