[FFmpeg-devel,v2] avformat/matroskaenc: Improve cues in case of no-video

Submitted by Andreas Rheinhardt on Nov. 27, 2019, 5:46 a.m.

Details

Message ID 20191127054613.20114-1-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt Nov. 27, 2019, 5:46 a.m.
The Matroska muxer currently only adds CuePoints in three cases:
a) For video keyframes. b) For the first audio frame in a new cluster if
in dash-mode. c) For subtitles. This means that ordinary Matroska audio
files won't have any cues which impedes seeking.

This commit changes this. For every track in a file without video track
it is checked and tracked whether a cue entry has already been added
for said track for the current cluster. This is used to add a cue entry
for each first packet of each track in each cluster.

Implements #3149.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
Resending because of updated FATE-tests.

 libavformat/matroskaenc.c            | 21 ++++++++++++---------
 tests/ref/fate/aac-autobsf-adtstoasc |  4 ++--
 tests/ref/lavf/mka                   |  4 ++--
 3 files changed, 16 insertions(+), 13 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index c684603575..ec96e1e5f0 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -2147,6 +2147,10 @@  static void mkv_end_cluster(AVFormatContext *s)
     MatroskaMuxContext *mkv = s->priv_data;
 
     end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv);
+    if (!mkv->have_video) {
+        for (int i = 0; i < s->nb_streams; i++)
+            mkv->tracks[i].has_cue = 0;
+    }
     mkv->cluster_pos = -1;
     avio_flush(s->pb);
 }
@@ -2259,7 +2263,7 @@  static int mkv_check_new_extra_data(AVFormatContext *s, AVPacket *pkt)
     return 0;
 }
 
-static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_cue)
+static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
 {
     MatroskaMuxContext *mkv = s->priv_data;
     AVIOContext *pb;
@@ -2303,10 +2307,12 @@  static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_
 
     if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) {
         mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe);
-        if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && (par->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue)) {
+        if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && keyframe &&
+            (par->codec_type == AVMEDIA_TYPE_VIDEO || !mkv->have_video && !track->has_cue)) {
             ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts,
                                    mkv->cluster_pos, relative_packet_pos, -1);
             if (ret < 0) return ret;
+            track->has_cue = 1;
         }
     } else {
         if (par->codec_id == AV_CODEC_ID_WEBVTT) {
@@ -2371,8 +2377,7 @@  static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
         // on seeing key frames.
         start_new_cluster = keyframe;
     } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
-               (mkv->cluster_pos == -1 ||
-                cluster_time > mkv->cluster_time_limit)) {
+               cluster_time > mkv->cluster_time_limit) {
         // For DASH audio, we create a Cluster based on cluster_time_limit
         start_new_cluster = 1;
     } else if (!mkv->is_dash &&
@@ -2396,9 +2401,7 @@  static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
 
     // check if we have an audio packet cached
     if (mkv->cur_audio_pkt.size > 0) {
-        // for DASH audio, a CuePoint has to be added when there is a new cluster.
-        ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt,
-                                        mkv->is_dash ? start_new_cluster : 0);
+        ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
         av_packet_unref(&mkv->cur_audio_pkt);
         if (ret < 0) {
             av_log(s, AV_LOG_ERROR,
@@ -2413,7 +2416,7 @@  static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
         if (pkt->size > 0)
             ret = av_packet_ref(&mkv->cur_audio_pkt, pkt);
     } else
-        ret = mkv_write_packet_internal(s, pkt, 0);
+        ret = mkv_write_packet_internal(s, pkt);
     return ret;
 }
 
@@ -2442,7 +2445,7 @@  static int mkv_write_trailer(AVFormatContext *s)
 
     // check if we have an audio packet cached
     if (mkv->cur_audio_pkt.size > 0) {
-        ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt, 0);
+        ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
         if (ret < 0) {
             av_log(s, AV_LOG_ERROR,
                    "Could not write cached audio packet ret:%d\n", ret);
diff --git a/tests/ref/fate/aac-autobsf-adtstoasc b/tests/ref/fate/aac-autobsf-adtstoasc
index 311af5d5fa..6071094b32 100644
--- a/tests/ref/fate/aac-autobsf-adtstoasc
+++ b/tests/ref/fate/aac-autobsf-adtstoasc
@@ -1,5 +1,5 @@ 
-64fef0158181534b35c716e89663df3a *tests/data/fate/aac-autobsf-adtstoasc.matroska
-6625 tests/data/fate/aac-autobsf-adtstoasc.matroska
+846cf1175327be8dfab5bc815c127d8b *tests/data/fate/aac-autobsf-adtstoasc.matroska
+6653 tests/data/fate/aac-autobsf-adtstoasc.matroska
 #extradata 0:        2, 0x0030001c
 #tb 0: 1/1000
 #media_type 0: audio
diff --git a/tests/ref/lavf/mka b/tests/ref/lavf/mka
index 39b5cb691e..a2d93b2fcb 100644
--- a/tests/ref/lavf/mka
+++ b/tests/ref/lavf/mka
@@ -1,3 +1,3 @@ 
-60eab6452def8ae6357fcc93f9e1c34b *tests/data/lavf/lavf.mka
-43589 tests/data/lavf/lavf.mka
+c05fd38ca616b83b10dfddd78afade41 *tests/data/lavf/lavf.mka
+43617 tests/data/lavf/lavf.mka
 tests/data/lavf/lavf.mka CRC=0x3a1da17e