From 3a9b0188cd6efa03daa05190ccabebe5211ed4a5 Mon Sep 17 00:00:00 2001 From: Philippe Normand Date: Wed, 11 Feb 2015 15:30:44 +0100 Subject: qtdemux: Initial 'sidx' atom parsing support Parse the 'sidx' atom and update the total duration according to the parser result. The isoff parser code is imported from gst-plugins-bad's dashdemux and a gst_isoff_sidx_parser_add_data() function was factored out of the gst_isoff_sidx_parser_add_buffer() function. https://bugzilla.gnome.org/show_bug.cgi?id=743578 --- gst/isomp4/Makefile.am | 3 +- gst/isomp4/fourcc.h | 1 + gst/isomp4/gstisoff.c | 198 +++++++++++++++++++++++++++++++++++++++++++++++++ gst/isomp4/gstisoff.h | 100 +++++++++++++++++++++++++ gst/isomp4/qtdemux.c | 35 +++++++++ gst/isomp4/qtdemux.h | 1 + 6 files changed, 337 insertions(+), 1 deletion(-) create mode 100644 gst/isomp4/gstisoff.c create mode 100644 gst/isomp4/gstisoff.h diff --git a/gst/isomp4/Makefile.am b/gst/isomp4/Makefile.am index 2755ee8b0..30ef999da 100644 --- a/gst/isomp4/Makefile.am +++ b/gst/isomp4/Makefile.am @@ -15,7 +15,7 @@ libgstisomp4_la_LDFLAGS = ${GST_PLUGIN_LDFLAGS} libgstisomp4_la_SOURCES = isomp4-plugin.c gstrtpxqtdepay.c \ qtdemux.c qtdemux_types.c qtdemux_dump.c qtdemux_lang.c \ gstqtmux.c gstqtmoovrecover.c atoms.c atomsrecovery.c descriptors.c \ - properties.c gstqtmuxmap.c + properties.c gstqtmuxmap.c gstisoff.c libgstisomp4_la_LIBTOOLFLAGS = $(GST_PLUGIN_LIBTOOLFLAGS) noinst_HEADERS = \ @@ -33,6 +33,7 @@ noinst_HEADERS = \ descriptors.h \ properties.h \ fourcc.h \ + gstisoff.h \ gstqtmuxmap.h EXTRA_DIST = \ diff --git a/gst/isomp4/fourcc.h b/gst/isomp4/fourcc.h index 2f8874b01..c361e9827 100644 --- a/gst/isomp4/fourcc.h +++ b/gst/isomp4/fourcc.h @@ -180,6 +180,7 @@ G_BEGIN_DECLS #define FOURCC_sawb GST_MAKE_FOURCC('s','a','w','b') #define FOURCC_sbtl GST_MAKE_FOURCC('s','b','t','l') #define FOURCC_sdp_ GST_MAKE_FOURCC('s','d','p',' ') +#define FOURCC_sidx GST_MAKE_FOURCC('s','i','d','x') #define FOURCC_smhd GST_MAKE_FOURCC('s','m','h','d') #define FOURCC_soaa GST_MAKE_FOURCC('s','o','a','a') #define FOURCC_soal GST_MAKE_FOURCC('s','o','a','l') diff --git a/gst/isomp4/gstisoff.c b/gst/isomp4/gstisoff.c new file mode 100644 index 000000000..e8d308438 --- /dev/null +++ b/gst/isomp4/gstisoff.c @@ -0,0 +1,198 @@ +/* + * ISO File Format parsing library + * + * gstisoff.h + * + * Copyright (C) 2015 Samsung Electronics. All rights reserved. + * Author: Thiago Santos + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library (COPYING); if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "gstisoff.h" +#include + +void +gst_isoff_sidx_parser_init (GstSidxParser * parser) +{ + parser->status = GST_ISOFF_SIDX_PARSER_INIT; + parser->cumulative_entry_size = 0; + parser->sidx.entries = NULL; + parser->sidx.entries_count = 0; +} + +void +gst_isoff_sidx_parser_clear (GstSidxParser * parser) +{ + g_free (parser->sidx.entries); + parser->sidx.entries = NULL; +} + +static void +gst_isoff_parse_sidx_entry (GstSidxBoxEntry * entry, GstByteReader * reader) +{ + guint32 aux; + + aux = gst_byte_reader_get_uint32_be_unchecked (reader); + entry->ref_type = aux >> 31; + entry->size = aux & 0x7FFFFFFF; + entry->duration = gst_byte_reader_get_uint32_be_unchecked (reader); + aux = gst_byte_reader_get_uint32_be_unchecked (reader); + entry->starts_with_sap = aux >> 31; + entry->sap_type = ((aux >> 28) & 0x7); + entry->sap_delta_time = aux & 0xFFFFFFF; +} + +GstIsoffParserResult +gst_isoff_sidx_parser_add_data (GstSidxParser * parser, const guint8 * buffer, + gint length, guint * consumed) +{ + GstIsoffParserResult res = GST_ISOFF_PARSER_OK; + GstByteReader reader; + gsize remaining; + guint32 fourcc; + + gst_byte_reader_init (&reader, buffer, length); + + switch (parser->status) { + case GST_ISOFF_SIDX_PARSER_INIT: + if (gst_byte_reader_get_remaining (&reader) < GST_ISOFF_FULL_BOX_SIZE) { + break; + } + + parser->size = gst_byte_reader_get_uint32_be_unchecked (&reader); + fourcc = gst_byte_reader_get_uint32_le_unchecked (&reader); + if (fourcc != GST_ISOFF_FOURCC_SIDX) { + res = GST_ISOFF_PARSER_UNEXPECTED; + gst_byte_reader_set_pos (&reader, 0); + break; + } + if (parser->size == 1) { + if (gst_byte_reader_get_remaining (&reader) < 12) { + gst_byte_reader_set_pos (&reader, 0); + break; + } + + parser->size = gst_byte_reader_get_uint64_be_unchecked (&reader); + } + if (parser->size == 0) { + res = GST_ISOFF_PARSER_ERROR; + gst_byte_reader_set_pos (&reader, 0); + break; + } + parser->sidx.version = gst_byte_reader_get_uint8_unchecked (&reader); + parser->sidx.flags = gst_byte_reader_get_uint24_le_unchecked (&reader); + + parser->status = GST_ISOFF_SIDX_PARSER_HEADER; + + case GST_ISOFF_SIDX_PARSER_HEADER: + remaining = gst_byte_reader_get_remaining (&reader); + if (remaining < 12 + (parser->sidx.version == 0 ? 8 : 16)) { + break; + } + + parser->sidx.ref_id = gst_byte_reader_get_uint32_be_unchecked (&reader); + parser->sidx.timescale = + gst_byte_reader_get_uint32_be_unchecked (&reader); + if (parser->sidx.version == 0) { + parser->sidx.earliest_pts = + gst_byte_reader_get_uint32_be_unchecked (&reader); + parser->sidx.first_offset = parser->sidx.earliest_pts = + gst_byte_reader_get_uint32_be_unchecked (&reader); + } else { + parser->sidx.earliest_pts = + gst_byte_reader_get_uint64_be_unchecked (&reader); + parser->sidx.first_offset = + gst_byte_reader_get_uint64_be_unchecked (&reader); + } + /* skip 2 reserved bytes */ + gst_byte_reader_skip_unchecked (&reader, 2); + parser->sidx.entries_count = + gst_byte_reader_get_uint16_be_unchecked (&reader); + + GST_LOG ("Timescale: %" G_GUINT32_FORMAT, parser->sidx.timescale); + GST_LOG ("Earliest pts: %" G_GUINT64_FORMAT, parser->sidx.earliest_pts); + GST_LOG ("First offset: %" G_GUINT64_FORMAT, parser->sidx.first_offset); + + parser->cumulative_pts = + gst_util_uint64_scale_int_round (parser->sidx.earliest_pts, + GST_SECOND, parser->sidx.timescale); + + if (parser->sidx.entries_count) { + parser->sidx.entries = + g_malloc (sizeof (GstSidxBoxEntry) * parser->sidx.entries_count); + } + parser->sidx.entry_index = 0; + + parser->status = GST_ISOFF_SIDX_PARSER_DATA; + + case GST_ISOFF_SIDX_PARSER_DATA: + while (parser->sidx.entry_index < parser->sidx.entries_count) { + GstSidxBoxEntry *entry = + &parser->sidx.entries[parser->sidx.entry_index]; + + remaining = gst_byte_reader_get_remaining (&reader);; + if (remaining < 12) + break; + + entry->offset = parser->cumulative_entry_size; + entry->pts = parser->cumulative_pts; + gst_isoff_parse_sidx_entry (entry, &reader); + entry->duration = gst_util_uint64_scale_int_round (entry->duration, + GST_SECOND, parser->sidx.timescale); + parser->cumulative_entry_size += entry->size; + parser->cumulative_pts += entry->duration; + + GST_LOG ("Sidx entry %d) offset: %" G_GUINT64_FORMAT ", pts: %" + GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT " - size %" + G_GUINT32_FORMAT, parser->sidx.entry_index, entry->offset, + GST_TIME_ARGS (entry->pts), GST_TIME_ARGS (entry->duration), + entry->size); + + parser->sidx.entry_index++; + } + + if (parser->sidx.entry_index == parser->sidx.entries_count) + parser->status = GST_ISOFF_SIDX_PARSER_FINISHED; + else + break; + case GST_ISOFF_SIDX_PARSER_FINISHED: + parser->sidx.entry_index = 0; + res = GST_ISOFF_PARSER_DONE; + break; + } + + *consumed = gst_byte_reader_get_pos (&reader); + return res; +} + +GstIsoffParserResult +gst_isoff_sidx_parser_add_buffer (GstSidxParser * parser, GstBuffer * buffer, + guint * consumed) +{ + GstIsoffParserResult res = GST_ISOFF_PARSER_OK; + GstMapInfo info; + + if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) { + *consumed = 0; + return GST_ISOFF_PARSER_ERROR; + } + + res = gst_isoff_sidx_parser_add_data (parser, info.data, info.size, consumed); + + gst_buffer_unmap (buffer, &info); + return res; +} diff --git a/gst/isomp4/gstisoff.h b/gst/isomp4/gstisoff.h new file mode 100644 index 000000000..c3f0b1b05 --- /dev/null +++ b/gst/isomp4/gstisoff.h @@ -0,0 +1,100 @@ +/* + * ISO File Format parsing library + * + * gstisoff.h + * + * Copyright (C) 2015 Samsung Electronics. All rights reserved. + * Author: Thiago Santos + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library (COPYING); if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GST_ISOFF_H__ +#define __GST_ISOFF_H__ + +#include + +G_BEGIN_DECLS + +typedef enum { + GST_ISOFF_PARSER_OK, + GST_ISOFF_PARSER_DONE, + GST_ISOFF_PARSER_UNEXPECTED, + GST_ISOFF_PARSER_ERROR +} GstIsoffParserResult; + +/* this is the minimum size, it can be larger if it + * uses extended size or type */ +#define GST_ISOFF_FULL_BOX_SIZE 12 + +#define GST_ISOFF_FOURCC_SIDX GST_MAKE_FOURCC('s','i','d','x') +typedef struct _GstSidxBoxEntry +{ + gboolean ref_type; + guint32 size; + GstClockTime duration; + gboolean starts_with_sap; + guint8 sap_type; + guint32 sap_delta_time; + + guint64 offset; + GstClockTime pts; +} GstSidxBoxEntry; + +typedef struct _GstSidxBox +{ + guint8 version; + guint32 flags; + + guint32 ref_id; + guint32 timescale; + guint64 earliest_pts; + guint64 first_offset; + + gint entry_index; + gint entries_count; + + GstSidxBoxEntry *entries; +} GstSidxBox; + +typedef enum _GstSidxParserStatus +{ + GST_ISOFF_SIDX_PARSER_INIT, + GST_ISOFF_SIDX_PARSER_HEADER, + GST_ISOFF_SIDX_PARSER_DATA, + GST_ISOFF_SIDX_PARSER_FINISHED +} GstSidxParserStatus; + +typedef struct _GstSidxParser +{ + GstSidxParserStatus status; + + guint64 size; + guint64 cumulative_entry_size; + guint64 cumulative_pts; + + GstSidxBox sidx; +} GstSidxParser; + +void gst_isoff_sidx_parser_init (GstSidxParser * parser); +void gst_isoff_sidx_parser_clear (GstSidxParser * parser); +GstIsoffParserResult gst_isoff_sidx_parser_add_data (GstSidxParser * parser, const guint8 * buffer, gint length, guint * consumed); +GstIsoffParserResult gst_isoff_sidx_parser_add_buffer (GstSidxParser * parser, GstBuffer * buf, guint * consumed); + +G_END_DECLS + +#endif /* __GST_ISOFF_H__ */ + diff --git a/gst/isomp4/qtdemux.c b/gst/isomp4/qtdemux.c index 5f1fffac6..4baf7b7b5 100644 --- a/gst/isomp4/qtdemux.c +++ b/gst/isomp4/qtdemux.c @@ -497,6 +497,7 @@ static void qtdemux_do_allocation (GstQTDemux * qtdemux, QtDemuxStream * stream); static gboolean qtdemux_pull_mfro_mfra (GstQTDemux * qtdemux); +static void check_update_duration (GstQTDemux * qtdemux, GstClockTime duration); static void gst_qtdemux_class_init (GstQTDemuxClass * klass) @@ -2328,6 +2329,24 @@ qtdemux_parse_uuid (GstQTDemux * qtdemux, const guint8 * buffer, gint length) } } +static void +qtdemux_parse_sidx (GstQTDemux * qtdemux, const guint8 * buffer, gint length) +{ + GstSidxParser sidx_parser; + GstIsoffParserResult res; + guint consumed; + + gst_isoff_sidx_parser_init (&sidx_parser); + + res = + gst_isoff_sidx_parser_add_data (&sidx_parser, buffer, length, &consumed); + GST_DEBUG_OBJECT (qtdemux, "sidx parse result: %d", res); + if (res == GST_ISOFF_PARSER_DONE) { + check_update_duration (qtdemux, sidx_parser.cumulative_pts); + } + gst_isoff_sidx_parser_clear (&sidx_parser); +} + /* caller verifies at least 8 bytes in buf */ static void extract_initial_length_and_fourcc (const guint8 * data, guint size, @@ -3358,6 +3377,19 @@ gst_qtdemux_loop_state_header (GstQTDemux * qtdemux) gst_buffer_unref (uuid); break; } + case FOURCC_sidx: + { + GstBuffer *sidx = NULL; + ret = gst_qtdemux_pull_atom (qtdemux, cur_offset, length, &sidx); + if (ret != GST_FLOW_OK) + goto beach; + qtdemux->offset += length; + gst_buffer_map (sidx, &map, GST_MAP_READ); + qtdemux_parse_sidx (qtdemux, map.data, map.size); + gst_buffer_unmap (sidx, &map); + gst_buffer_unref (sidx); + break; + } default: { GstBuffer *unknown = NULL; @@ -5194,6 +5226,9 @@ gst_qtdemux_process_adapter (GstQTDemux * demux, gboolean force) } else if (fourcc == FOURCC_uuid) { GST_DEBUG_OBJECT (demux, "Parsing [uuid]"); qtdemux_parse_uuid (demux, data, demux->neededbytes); + } else if (fourcc == FOURCC_sidx) { + GST_DEBUG_OBJECT (demux, "Parsing [sidx]"); + qtdemux_parse_sidx (demux, data, demux->neededbytes); } else { GST_WARNING_OBJECT (demux, "Unknown fourcc while parsing header : %" GST_FOURCC_FORMAT, diff --git a/gst/isomp4/qtdemux.h b/gst/isomp4/qtdemux.h index 22469b898..6345d9c45 100644 --- a/gst/isomp4/qtdemux.h +++ b/gst/isomp4/qtdemux.h @@ -24,6 +24,7 @@ #include #include #include +#include "gstisoff.h" G_BEGIN_DECLS -- cgit v1.2.3