1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-21 10:55:51 +02:00

mov: Read QuickTime chapters

Originally committed as revision 22928 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
David Conrad 2010-04-21 06:36:09 +00:00
parent dc75e4e3b3
commit 7221579b0c
2 changed files with 79 additions and 0 deletions

View File

@ -138,6 +138,7 @@ typedef struct MOVContext {
MOVTrackExt *trex_data;
unsigned trex_count;
int itunes_metadata; ///< metadata are itunes style
int chapter_track;
} MOVContext;
int ff_mp4_read_descr_len(ByteIOContext *pb);

View File

@ -1953,6 +1953,12 @@ static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
return 0;
}
static int mov_read_chap(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
{
c->chapter_track = get_be32(pb);
return 0;
}
static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
{
MOVTrackExt *trex;
@ -2200,6 +2206,8 @@ static const MOVParseTableEntry mov_default_parse_table[] = {
{ MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
{ MKTAG('t','r','a','k'), mov_read_trak },
{ MKTAG('t','r','a','f'), mov_read_default },
{ MKTAG('t','r','e','f'), mov_read_default },
{ MKTAG('c','h','a','p'), mov_read_chap },
{ MKTAG('t','r','e','x'), mov_read_trex },
{ MKTAG('t','r','u','n'), mov_read_trun },
{ MKTAG('u','d','t','a'), mov_read_default },
@ -2255,6 +2263,73 @@ static int mov_probe(AVProbeData *p)
return score;
}
// must be done after parsing all trak because there's no order requirement
static void mov_read_chapters(AVFormatContext *s)
{
MOVContext *mov = s->priv_data;
AVStream *st = NULL;
MOVStreamContext *sc;
int64_t cur_pos;
uint8_t *title = NULL;
int i, len, i8, i16;
for (i = 0; i < s->nb_streams; i++)
if (s->streams[i]->id == mov->chapter_track) {
st = s->streams[i];
break;
}
if (!st) {
av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
return;
}
st->discard = AVDISCARD_ALL;
sc = st->priv_data;
cur_pos = url_ftell(sc->pb);
for (i = 0; i < st->nb_index_entries; i++) {
AVIndexEntry *sample = &st->index_entries[i];
int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
goto finish;
}
title = av_malloc(sample->size+2);
get_buffer(sc->pb, title, sample->size);
// the first two bytes are the length of the title
len = AV_RB16(title);
if (len > sample->size-2)
continue;
// The samples could theoretically be in any encoding if there's an encd
// atom following, but in practice are only utf-8 or utf-16, distinguished
// instead by the presence of a BOM
if (AV_RB16(title+2) == 0xfeff) {
uint8_t *utf8 = av_malloc(2*len+3);
i8 = i16 = 0;
while (i16 < len) {
uint32_t ch;
uint8_t tmp;
GET_UTF16(ch, i16 < len ? AV_RB16(title + (i16+=2)) : 0, break;)
PUT_UTF8(ch, tmp, if (i8 < 2*len) utf8[2+i8++] = tmp;)
}
utf8[2+i8] = 0;
av_freep(&title);
title = utf8;
}
ff_new_chapter(s, i, st->time_base, sample->timestamp, end, title+2);
av_freep(&title);
}
finish:
av_free(title);
url_fseek(sc->pb, cur_pos, SEEK_SET);
}
static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
MOVContext *mov = s->priv_data;
@ -2280,6 +2355,9 @@ static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
}
dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
if (!url_is_streamed(pb) && mov->chapter_track > 0)
mov_read_chapters(s);
return 0;
}