2008-12-13 12:13:48 +00:00
|
|
|
/*
|
|
|
|
* MP4 file format parser code
|
|
|
|
*
|
2008-12-13 12:31:07 +00:00
|
|
|
* Copyright (C) 2002 Felix Buenemann <atmosfear at users.sourceforge.net>
|
2002-03-24 02:25:41 +00:00
|
|
|
* Code inspired by libmp4 from http://mpeg4ip.sourceforge.net/.
|
2008-12-13 12:13:48 +00:00
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2002-03-24 02:25:41 +00:00
|
|
|
*/
|
2008-12-13 12:13:48 +00:00
|
|
|
|
2002-03-24 02:25:41 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <inttypes.h>
|
2002-03-30 14:42:32 +00:00
|
|
|
#include <stdlib.h>
|
2002-03-24 02:25:41 +00:00
|
|
|
#include "parse_mp4.h"
|
|
|
|
#include "mp_msg.h"
|
2007-03-15 18:36:36 +00:00
|
|
|
#include "stream/stream.h"
|
2002-03-24 02:25:41 +00:00
|
|
|
|
2002-03-25 11:13:14 +00:00
|
|
|
//#define MP4_DUMPATOM
|
|
|
|
|
2002-03-24 02:25:41 +00:00
|
|
|
#define MP4_DL MSGL_V
|
2002-03-24 03:07:18 +00:00
|
|
|
#define freereturn(a,b) free(a); return b
|
2002-03-24 02:25:41 +00:00
|
|
|
|
2010-02-17 22:01:35 +00:00
|
|
|
static int mp4_read_descr_len(stream_t *s)
|
|
|
|
{
|
2002-03-24 02:25:41 +00:00
|
|
|
uint8_t b;
|
|
|
|
uint8_t numBytes = 0;
|
|
|
|
uint32_t length = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
b = stream_read_char(s);
|
|
|
|
numBytes++;
|
|
|
|
length = (length << 7) | (b & 0x7F);
|
|
|
|
} while ((b & 0x80) && numBytes < 4);
|
|
|
|
|
2002-03-25 11:13:14 +00:00
|
|
|
//printf("MP4 read desc len: %d\n", length);
|
2002-03-24 02:25:41 +00:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2002-03-24 06:10:13 +00:00
|
|
|
/* parse the data part of MP4 esds atoms */
|
2002-03-24 02:25:41 +00:00
|
|
|
int mp4_parse_esds(unsigned char *data, int datalen, esds_t *esds) {
|
|
|
|
/* create memory stream from data */
|
|
|
|
stream_t *s = new_memory_stream(data, datalen);
|
2006-06-09 21:29:18 +00:00
|
|
|
uint16_t len;
|
2002-03-25 11:13:14 +00:00
|
|
|
#ifdef MP4_DUMPATOM
|
|
|
|
{int i;
|
2009-05-13 02:58:57 +00:00
|
|
|
printf("ESDS Dump (%dbyte):\n", datalen);
|
2002-03-25 11:13:14 +00:00
|
|
|
for(i = 0; i < datalen; i++)
|
|
|
|
printf("%02X ", data[i]);
|
|
|
|
printf("\nESDS Dumped\n");}
|
2009-05-13 02:58:57 +00:00
|
|
|
#endif
|
2002-03-25 11:13:14 +00:00
|
|
|
memset(esds, 0, sizeof(esds_t));
|
2002-03-24 02:25:41 +00:00
|
|
|
|
|
|
|
esds->version = stream_read_char(s);
|
|
|
|
esds->flags = stream_read_int24(s);
|
|
|
|
mp_msg(MSGT_DEMUX, MP4_DL,
|
|
|
|
"ESDS MPEG4 version: %d flags: 0x%06X\n",
|
|
|
|
esds->version, esds->flags);
|
|
|
|
|
|
|
|
/* get and verify ES_DescrTag */
|
2002-03-24 06:10:13 +00:00
|
|
|
if (stream_read_char(s) == MP4ESDescrTag) {
|
2002-03-24 02:25:41 +00:00
|
|
|
/* read length */
|
2002-03-25 11:13:14 +00:00
|
|
|
len = mp4_read_descr_len(s);
|
|
|
|
|
2002-03-24 02:25:41 +00:00
|
|
|
esds->ESId = stream_read_word(s);
|
|
|
|
esds->streamPriority = stream_read_char(s);
|
2002-03-25 11:13:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX, MP4_DL,
|
|
|
|
"ESDS MPEG4 ES Descriptor (%dBytes):\n"
|
|
|
|
" -> ESId: %d\n"
|
|
|
|
" -> streamPriority: %d\n",
|
|
|
|
len, esds->ESId, esds->streamPriority);
|
|
|
|
|
|
|
|
if (len < (5 + 15)) {
|
|
|
|
freereturn(s,1);
|
|
|
|
}
|
2002-03-24 02:25:41 +00:00
|
|
|
} else {
|
|
|
|
esds->ESId = stream_read_word(s);
|
2002-03-25 11:13:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX, MP4_DL,
|
|
|
|
"ESDS MPEG4 ES Descriptor (%dBytes):\n"
|
|
|
|
" -> ESId: %d\n", 2, esds->ESId);
|
2002-03-24 02:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get and verify DecoderConfigDescrTab */
|
|
|
|
if (stream_read_char(s) != MP4DecConfigDescrTag) {
|
2002-03-24 03:07:18 +00:00
|
|
|
freereturn(s,1);
|
2002-03-24 02:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read length */
|
2002-03-25 11:13:14 +00:00
|
|
|
len = mp4_read_descr_len(s);
|
2002-03-24 02:25:41 +00:00
|
|
|
|
|
|
|
esds->objectTypeId = stream_read_char(s);
|
|
|
|
esds->streamType = stream_read_char(s);
|
|
|
|
esds->bufferSizeDB = stream_read_int24(s);
|
|
|
|
esds->maxBitrate = stream_read_dword(s);
|
|
|
|
esds->avgBitrate = stream_read_dword(s);
|
|
|
|
mp_msg(MSGT_DEMUX, MP4_DL,
|
|
|
|
"ESDS MPEG4 Decoder Config Descriptor (%dBytes):\n"
|
|
|
|
" -> objectTypeId: %d\n"
|
|
|
|
" -> streamType: 0x%02X\n"
|
|
|
|
" -> bufferSizeDB: 0x%06X\n"
|
|
|
|
" -> maxBitrate: %.3fkbit/s\n"
|
|
|
|
" -> avgBitrate: %.3fkbit/s\n",
|
|
|
|
len, esds->objectTypeId, esds->streamType,
|
|
|
|
esds->bufferSizeDB, esds->maxBitrate/1000.0,
|
|
|
|
esds->avgBitrate/1000.0);
|
|
|
|
|
2002-08-05 18:22:51 +00:00
|
|
|
esds->decoderConfigLen=0;
|
|
|
|
|
2002-03-25 11:13:14 +00:00
|
|
|
if (len < 15) {
|
2002-08-05 18:22:51 +00:00
|
|
|
freereturn(s,0);
|
2002-03-25 11:13:14 +00:00
|
|
|
}
|
|
|
|
|
2002-03-24 02:25:41 +00:00
|
|
|
/* get and verify DecSpecificInfoTag */
|
|
|
|
if (stream_read_char(s) != MP4DecSpecificDescrTag) {
|
2002-08-05 18:22:51 +00:00
|
|
|
freereturn(s,0);
|
2002-03-24 02:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read length */
|
2009-05-13 02:58:57 +00:00
|
|
|
esds->decoderConfigLen = len = mp4_read_descr_len(s);
|
2002-03-24 02:25:41 +00:00
|
|
|
|
|
|
|
esds->decoderConfig = malloc(esds->decoderConfigLen);
|
|
|
|
if (esds->decoderConfig) {
|
|
|
|
stream_read(s, esds->decoderConfig, esds->decoderConfigLen);
|
|
|
|
} else {
|
|
|
|
esds->decoderConfigLen = 0;
|
|
|
|
}
|
|
|
|
mp_msg(MSGT_DEMUX, MP4_DL,
|
|
|
|
"ESDS MPEG4 Decoder Specific Descriptor (%dBytes)\n", len);
|
|
|
|
|
2002-03-24 06:10:13 +00:00
|
|
|
/* get and verify SLConfigDescrTag */
|
|
|
|
if(stream_read_char(s) != MP4SLConfigDescrTag) {
|
2002-08-05 18:22:51 +00:00
|
|
|
freereturn(s,0);
|
2002-03-24 06:10:13 +00:00
|
|
|
}
|
|
|
|
|
2002-03-25 11:13:14 +00:00
|
|
|
/* Note: SLConfig is usually constant value 2, size 1Byte */
|
2002-03-25 11:35:21 +00:00
|
|
|
esds->SLConfigLen = len = mp4_read_descr_len(s);
|
2002-03-24 06:10:13 +00:00
|
|
|
esds->SLConfig = malloc(esds->SLConfigLen);
|
|
|
|
if (esds->SLConfig) {
|
|
|
|
stream_read(s, esds->SLConfig, esds->SLConfigLen);
|
|
|
|
} else {
|
|
|
|
esds->SLConfigLen = 0;
|
|
|
|
}
|
|
|
|
mp_msg(MSGT_DEMUX, MP4_DL,
|
|
|
|
"ESDS MPEG4 Sync Layer Config Descriptor (%dBytes)\n"
|
|
|
|
" -> predefined: %d\n", len, esds->SLConfig[0]);
|
|
|
|
|
2002-03-24 02:25:41 +00:00
|
|
|
/* will skip the remainder of the atom */
|
2002-03-24 03:07:18 +00:00
|
|
|
freereturn(s,0);
|
2002-03-24 02:25:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-03-24 06:10:13 +00:00
|
|
|
/* cleanup all mem occupied by mp4_parse_esds */
|
|
|
|
void mp4_free_esds(esds_t *esds) {
|
2002-03-25 11:13:14 +00:00
|
|
|
if(esds->decoderConfigLen)
|
2002-03-24 06:10:13 +00:00
|
|
|
free(esds->decoderConfig);
|
2002-03-25 11:13:14 +00:00
|
|
|
if(esds->SLConfigLen)
|
2002-03-24 06:10:13 +00:00
|
|
|
free(esds->SLConfig);
|
|
|
|
}
|
|
|
|
|
2002-03-24 03:07:18 +00:00
|
|
|
#undef freereturn
|
|
|
|
#undef MP4_DL
|