changeset 5301:d72c3169a343

Improved MP4 parsing (finally)
author atmos4
date Sun, 24 Mar 2002 02:25:41 +0000
parents 5d7bd47b46ef
children 6dc0ac26eb0e
files libmpdemux/Makefile libmpdemux/demux_mov.c libmpdemux/parse_mp4.c libmpdemux/parse_mp4.h libmpdemux/stheader.h libmpdemux/stream.h
diffstat 6 files changed, 230 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/libmpdemux/Makefile	Sun Mar 24 01:09:08 2002 +0000
+++ b/libmpdemux/Makefile	Sun Mar 24 02:25:41 2002 +0000
@@ -3,7 +3,7 @@
 
 include ../config.mak
 
-SRCS = mp3_hdr.c video.c mpeg_hdr.c cache2.c asfheader.c aviheader.c aviprint.c aviwrite.c demux_asf.c demux_avi.c demux_mov.c demux_mpg.c demux_viv.c demuxer.c dvdauth.c open.c parse_es.c stream.c tv.c tvi_dummy.c tvi_v4l.c tvi_bsdbt848.c frequencies.c demux_fli.c demux_real.c demux_y4m.c yuv4mpeg.c yuv4mpeg_ratio.c demux_nuv.c demux_film.c demux_roq.c mf.c demux_mf.c demux_audio.c demux_demuxers.c opt-reg.c mpdemux.c demux_ogg.c demux_bmp.c
+SRCS = mp3_hdr.c video.c mpeg_hdr.c cache2.c asfheader.c aviheader.c aviprint.c aviwrite.c demux_asf.c demux_avi.c demux_mov.c parse_mp4.c demux_mpg.c demux_viv.c demuxer.c dvdauth.c open.c parse_es.c stream.c tv.c tvi_dummy.c tvi_v4l.c tvi_bsdbt848.c frequencies.c demux_fli.c demux_real.c demux_y4m.c yuv4mpeg.c yuv4mpeg_ratio.c demux_nuv.c demux_film.c demux_roq.c mf.c demux_mf.c demux_audio.c demux_demuxers.c opt-reg.c mpdemux.c demux_ogg.c demux_bmp.c
 ifeq ($(STREAMING),yes)
 SRCS += asf_streaming.c url.c http.c network.c rtp.c
 endif
--- a/libmpdemux/demux_mov.c	Sun Mar 24 01:09:08 2002 +0000
+++ b/libmpdemux/demux_mov.c	Sun Mar 24 02:25:41 2002 +0000
@@ -30,6 +30,7 @@
 #include "bswap.h"
 
 #include "qtpalette.h"
+#include "parse_mp4.h" // MP3 specific stuff
 
 #ifdef HAVE_ZLIB
 #include <zlib.h>
@@ -40,8 +41,14 @@
 #define BE_16(x) (be2me_16(*(unsigned short *)(x)))
 #define BE_32(x) (be2me_32(*(unsigned int *)(x)))
 
+#ifndef WORDS_BIGENDIAN
 #define char2short(x,y) ((x[y]<<8)|x[y+1])
 #define char2int(x,y) ((x[y]<<24)|(x[y+1]<<16)|(x[y+2]<<8)|x[y+3])
+#else
+#warning Check the implementation of char2short and char2int on BIGENDIAN!!!
+#define char2short(x,y) (x[y]|(x[y+1]<<8))
+#define char2int(x,y) (x[y]|(x[y+1]<<8)|(x[y+2]<<16)|(x[y+3]<<24))
+#endif
 
 typedef struct {
     unsigned int pts; // duration
@@ -671,7 +678,7 @@
 		    trak->durmap[0].num, trak->timescale/trak->durmap[0].dur,
 		    char2short(trak->stdata,24)/trak->durmap[0].dur);*/
 		sh->samplerate=char2short(trak->stdata,24);
-		if((sh->samplerate < 8000) && trak->durmap) {
+		if((sh->samplerate < 7000) && trak->durmap) {
 		  switch(char2short(trak->stdata,24)/trak->durmap[0].dur) {
 		    // TODO: add more cases.
 		    case 31:
@@ -696,13 +703,26 @@
 		if((trak->stdata[9]==0) && trak->stdata_len >= 36) { // version 0 with extra atoms
 		    int atom_len = char2int(trak->stdata,28);
 		    switch(char2int(trak->stdata,32)) { // atom type
-		      case MOV_FOURCC('e','s','d','s'):
+		      case MOV_FOURCC('e','s','d','s'): {
+			esds_t *esds; 				  
 			mp_msg(MSGT_DEMUX, MSGL_INFO, "MOV: Found MPEG4 audio Elementary Stream Descriptor atom (%d)!\n", atom_len);
-			if(atom_len >= 28)
-			  mp_msg(MSGT_DEMUX, MSGL_INFO, "Audio compressed datarate: %dkbit/s\n",
-			      char2int(trak->stdata,62)/1000);
-			  sh->i_bps=char2int(trak->stdata,62)/8;
-			break;
+			if(atom_len >= 8) {
+			  if(!mp4_parse_esds(&trak->stdata[36], atom_len-8, esds)) {
+			    
+			    sh->i_bps = esds->avgBitrate/8; 
+
+			    // dump away the codec specific configuration for the AAC decoder
+			    sh->codecdata_len = esds->decoderConfigLen;
+			    sh->codecdata = (unsigned char *)malloc(sh->codecdata_len);
+			    memcpy(sh->codecdata, esds->decoderConfig, sh->codecdata_len);
+			  }
+#if 0
+	  		  { FILE* f=fopen("esds.dat","wb");
+			  fwrite(&trak->stdata[36],atom_len-8,1,f);
+			  fclose(f); }
+#endif			  
+			}
+		      } break;
 		      default:
 			mp_msg(MSGT_DEMUX, MSGL_INFO, "MOV: Found unknown audio atom %c%c%c%c (%d)!\n",
 			    trak->stdata[32],trak->stdata[33],trak->stdata[34],trak->stdata[35],
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmpdemux/parse_mp4.c	Sun Mar 24 02:25:41 2002 +0000
@@ -0,0 +1,114 @@
+/* parse_mp4.c - MP4 file format parser code
+ * This file is part of MPlayer, see http://mplayerhq.hu/ for info.  
+ * (c)2002 by Felix Buenemann <atmosfear at users.sourceforge.net>
+ * File licensed under the GPL, see http://www.fsf.org/ for more info.
+ * Code inspired by libmp4 from http://mpeg4ip.sourceforge.net/.
+ */
+   
+#include <stdio.h>
+#include <inttypes.h>
+#include <malloc.h>
+#include "parse_mp4.h"
+#include "mp_msg.h"
+#include "stream.h"
+
+#define MP4_DL MSGL_V
+
+int mp4_read_descr_len(stream_t *s) {
+  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);
+
+  return length;
+}
+
+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);
+  uint8_t tag;
+  uint8_t len;
+
+  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 */
+  tag = stream_read_char(s);
+  if (tag == MP4ESDescrTag) {
+    /* read length */
+    if ((len = mp4_read_descr_len(s)) < 5 + 15) {
+      return 1;
+    }
+    esds->ESId = stream_read_word(s);
+    esds->streamPriority = stream_read_char(s);
+  } else {
+#if 1 /* 1 == guessed */
+    esds->ESId = stream_read_word(s);
+#else    
+    /* skip 2 bytes */
+    stream_skip(s, 2);
+#endif
+  }
+  mp_msg(MSGT_DEMUX, MP4_DL,
+      "ESDS MPEG4 ES Descriptor (%dBytes):\n"
+      " -> ESId: %d\n"
+      " -> streamPriority: %d\n",
+      len, esds->ESId, esds->streamPriority);
+
+  /* get and verify DecoderConfigDescrTab */
+  if (stream_read_char(s) != MP4DecConfigDescrTag) {
+    return 1;
+  }
+
+  /* read length */
+  if ((len = mp4_read_descr_len(s)) < 15) {
+    return 1;
+  }
+
+  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);
+
+  /* get and verify DecSpecificInfoTag */
+  if (stream_read_char(s) != MP4DecSpecificDescrTag) {
+    return 1;
+  }
+
+  /* read length */
+  esds->decoderConfigLen = len = mp4_read_descr_len(s); 
+
+  free(esds->decoderConfig);
+  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);
+
+  /* will skip the remainder of the atom */
+  return 0;
+
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmpdemux/parse_mp4.h	Sun Mar 24 02:25:41 2002 +0000
@@ -0,0 +1,78 @@
+/* parse_mp4.h - Headerfile for MP4 file format parser code
+ * This file is part of MPlayer, see http://mplayerhq.hu/ for info.  
+ * (c)2002 by Felix Buenemann <atmosfear at users.sourceforge.net>
+ * File licensed under the GPL, see http://www.fsf.org/ for more info.
+ */
+
+#ifndef __PARSE_MP4_H
+#define __PARSE_MP4_H 1
+
+#include <inttypes.h>
+
+/* one byte tag identifiers */
+#define MP4ODescrTag			0x01 
+#define MP4IODescrTag			0x02 
+#define MP4ESDescrTag			0x03 
+#define MP4DecConfigDescrTag		0x04 
+#define MP4DecSpecificDescrTag		0x05 
+#define MP4SLConfigDescrTag		0x06 
+#define MP4ContentIdDescrTag		0x07 
+#define MP4SupplContentIdDescrTag	0x08 
+#define MP4IPIPtrDescrTag		0x09 
+#define MP4IPMPPtrDescrTag		0x0A 
+#define MP4IPMPDescrTag			0x0B 
+#define MP4RegistrationDescrTag		0x0D 
+#define MP4ESIDIncDescrTag		0x0E 
+#define MP4ESIDRefDescrTag		0x0F 
+#define MP4FileIODescrTag		0x10 
+#define MP4FileODescrTag		0x11 
+#define MP4ExtProfileLevelDescrTag	0x13 
+#define MP4ExtDescrTagsStart		0x80 
+#define MP4ExtDescrTagsEnd		0xFE 
+
+/* I define uint24 here for better understanding */
+#ifndef uint24_t
+#define uint24_t uint32_t
+#endif
+
+/* esds_t */
+typedef struct {
+  uint8_t  version;
+  uint24_t flags;
+  
+  /* 0x03 ESDescrTag */
+  uint16_t ESId;
+  uint8_t  streamPriority;
+  
+  /* 0x04 DecConfigDescrTag */
+  uint8_t  objectTypeId;
+  uint8_t  streamType;
+  /* XXX: really streamType is
+   * only 6bit, followed by:
+   * 1bit  upStream
+   * 1bit  reserved
+   */  
+  uint24_t bufferSizeDB;
+  uint32_t maxBitrate;
+  uint32_t avgBitrate;
+
+  /* 0x05 DecSpecificDescrTag */
+  uint8_t  decoderConfigLen;
+  uint8_t *decoderConfig;
+
+  /* 0x06 SLConfigDescrTag */
+  uint8_t  SLConfigLen;
+  uint8_t *SLConfig;
+
+  /* TODO: add the missing tags,
+   * I currently have no specs
+   * for them and doubt they
+   * are currently needed ::atmos
+   */
+  
+} esds_t;
+
+int mp4_parse_esds(unsigned char *data, int datalen, esds_t *esds);
+
+#endif /* !__PARSE_MP4_H */
+
--- a/libmpdemux/stheader.h	Sun Mar 24 01:09:08 2002 +0000
+++ b/libmpdemux/stheader.h	Sun Mar 24 02:25:41 2002 +0000
@@ -46,6 +46,8 @@
 #ifdef HAVE_OGGVORBIS
   struct ov_struct_st *ov; // should be assigned on init  TODO: use *context
 #endif
+  unsigned char *codecdata;
+  int codecdata_len;
 } sh_audio_t;
 
 typedef struct {
--- a/libmpdemux/stream.h	Sun Mar 24 01:09:08 2002 +0000
+++ b/libmpdemux/stream.h	Sun Mar 24 02:25:41 2002 +0000
@@ -118,6 +118,14 @@
   return y;
 }
 
+inline static unsigned int stream_read_int24(stream_t *s){
+  unsigned int y;
+  y = stream_read_char(s);
+  y=(y<<8)|stream_read_char(s);
+  y=(y<<8)|stream_read_char(s);
+  return y;
+}
+
 inline static int stream_read(stream_t *s,char* mem,int total){
   int len=total;
   while(len>0){