[FFmpeg-devel] [PATCH 1/2] Set subtitle track dimensions for mov_text to display the subtitles properly

Niklesh Lalwani niklesh.lalwani at iitb.ac.in
Thu Jun 11 06:45:43 CEST 2015


From: Niklesh <niklesh.lalwani at iitb.ac.in>

This patch mostly replicates the concept patch posted by Philip https://ffmpeg.org/pipermail/ffmpeg-devel/2013-March/140299.html. This sets the proper dimensions for the subtitle tracks, by making use of the height and width of the video stream. The patch is exlained in detail in the link.

Signed-off-by: Niklesh <niklesh.lalwani at iitb.ac.in>
---
 libavformat/movenc.c | 38 ++++++++++++++++++++++++++++++++++----
 libavformat/movenc.h |  4 ++++
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index dc9570b..c55219e 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1505,8 +1505,20 @@ static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
 
     if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
         mov_write_esds_tag(pb, track);
-    else if (track->enc->extradata_size)
+    else if (track->enc->extradata_size) {
+        if (track->enc->extradata_size >= 18) {
+            // Rewrite text box dimensions to match video stream.
+            uint8_t *ed = track->enc->extradata;
+            uint16_t width = track->video_width;
+            uint16_t height = track->video_height;
+            height /= 10;
+            ed[14] = height >> 8;
+            ed[15] = height & 0xFF;
+            ed[16] = width >> 8;
+            ed[17] = width & 0xFF;
+       }
         avio_write(pb, track->enc->extradata, track->enc->extradata_size);
+    }
 
     return update_size(pb, pos);
 }
@@ -2335,7 +2347,7 @@ static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov,
 
     avio_wb32(pb, 0); /* reserved */
     avio_wb32(pb, 0); /* reserved */
-    avio_wb16(pb, 0); /* layer */
+    avio_wb16(pb, -1); /* layer */
     avio_wb16(pb, group); /* alternate group) */
     /* Volume, only for audio */
     if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
@@ -2352,6 +2364,8 @@ static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov,
     if (display_matrix) {
         for (i = 0; i < 9; i++)
             avio_wb32(pb, display_matrix[i]);
+    } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
+        write_matrix(pb, 1, 0, 0, 1, 0, (track->video_height*9)/10);
     } else if (rotation == 90) {
         write_matrix(pb,  0,  1, -1,  0, track->enc->height, 0);
     } else if (rotation == 180) {
@@ -2362,8 +2376,7 @@ static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov,
         write_matrix(pb,  1,  0,  0,  1, 0, 0);
     }
     /* Track width and height, for visual only */
-    if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
-               track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
+    if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)) {
         if (track->mode == MODE_MOV) {
             avio_wb32(pb, track->enc->width << 16);
             avio_wb32(pb, track->height << 16);
@@ -2374,6 +2387,9 @@ static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov,
             avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
             avio_wb32(pb, track->height * 0x10000);
         }
+    } else if (st && track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
+        avio_wb32(pb, track->video_width * 0x10000);
+        avio_wb32(pb, (track->video_height * 0x10000) / 10);
     } else {
         avio_wb32(pb, 0);
         avio_wb32(pb, 0);
@@ -5216,6 +5232,20 @@ static int mov_write_header(AVFormatContext *s)
         }
     }
 
+    for (i = 0; i < mov->nb_streams; i++) {
+        MOVTrack *track = &mov->tracks[i];
+        if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
+            int j;
+            for (j = 0; j < mov->nb_streams; j++) {
+                if (mov->tracks[j].enc->codec_type == AVMEDIA_TYPE_VIDEO) {
+                    track->video_width = mov->tracks[j].enc->width;
+                    track->video_height = mov->tracks[j].enc->height;
+                    break;
+                }
+            }
+        }
+    }
+
     enable_tracks(s);
 
 
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 744d14e..c89a59e 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -148,6 +148,10 @@ typedef struct MOVTrack {
         int     slices;
     } vc1_info;
 
+    // For subtitle tracks.
+    uint16_t video_width;
+    uint16_t video_height;
+
     void       *eac3_priv;
 } MOVTrack;
 
-- 
1.9.1



More information about the ffmpeg-devel mailing list