changeset 15720:e999d58f94fd

added AAC ADTS demuxer
author nicodvb
date Mon, 13 Jun 2005 20:40:29 +0000
parents e040252b8370
children eec6ace22741
files ChangeLog libmpdemux/Makefile libmpdemux/demux_aac.c libmpdemux/demuxer.c libmpdemux/demuxer.h
diffstat 5 files changed, 281 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Jun 13 20:09:05 2005 +0000
+++ b/ChangeLog	Mon Jun 13 20:40:29 2005 +0000
@@ -22,6 +22,7 @@
     * simultaneous audio capture and playback in MPlayer
       (-tv immediatemode=0) fixed in v4l2
     * PVR support
+    * AAC ADTS demuxer
 
     Streaming:
     * fix streaming of RealAudio files over HTTP
--- a/libmpdemux/Makefile	Mon Jun 13 20:09:05 2005 +0000
+++ b/libmpdemux/Makefile	Mon Jun 13 20:40:29 2005 +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 muxer.c muxer_avi.c muxer_mpeg.c demux_asf.c demux_avi.c demux_mov.c parse_mp4.c demux_mpg.c demux_ty.c demux_ty_osd.c demux_pva.c demux_viv.c demuxer.c dvdnav_stream.c open.c parse_es.c stream.c stream_file.c stream_netstream.c stream_vcd.c stream_null.c stream_ftp.c stream_smb.c stream_vstream.c tv.c tvi_dummy.c tvi_v4l.c tvi_v4l2.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 demux_ogg.c cdda.c demux_rawaudio.c demux_rawvideo.c cddb.c cdinfo.c demux_rawdv.c ai_alsa.c ai_alsa1x.c ai_oss.c audio_in.c demux_smjpeg.c demux_lmlm4.c cue_read.c extension.c demux_gif.c demux_ts.c demux_realaud.c url.c muxer_rawvideo.c demux_lavf.c demux_nsv.c demux_vqf.c stream_dvd.c stream_livedotcom.c
+SRCS = mp3_hdr.c video.c mpeg_hdr.c cache2.c asfheader.c aviheader.c aviprint.c muxer.c muxer_avi.c muxer_mpeg.c demux_asf.c demux_avi.c demux_mov.c parse_mp4.c demux_mpg.c demux_ty.c demux_ty_osd.c demux_pva.c demux_viv.c demuxer.c dvdnav_stream.c open.c parse_es.c stream.c stream_file.c stream_netstream.c stream_vcd.c stream_null.c stream_ftp.c stream_smb.c stream_vstream.c tv.c tvi_dummy.c tvi_v4l.c tvi_v4l2.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 demux_ogg.c cdda.c demux_rawaudio.c demux_rawvideo.c cddb.c cdinfo.c demux_rawdv.c ai_alsa.c ai_alsa1x.c ai_oss.c audio_in.c demux_smjpeg.c demux_lmlm4.c cue_read.c extension.c demux_gif.c demux_ts.c demux_realaud.c url.c muxer_rawvideo.c demux_lavf.c demux_nsv.c demux_vqf.c stream_dvd.c stream_livedotcom.c demux_aac.c
 ifeq ($(XMMS_PLUGINS),yes)
 SRCS += demux_xmms.c
 endif 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmpdemux/demux_aac.c	Mon Jun 13 20:40:29 2005 +0000
@@ -0,0 +1,255 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "config.h"
+#include "mp_msg.h"
+#include "help_mp.h"
+
+#include "stream.h"
+#include "demuxer.h"
+#include "parse_es.h"
+#include "stheader.h"
+
+#include "bswap.h"
+#include "../unrarlib.h"
+#include "ms_hdr.h"
+
+extern void resync_audio_stream(sh_audio_t *sh_audio);
+
+typedef struct {
+	uint8_t *buf;
+	uint64_t size;	/// amount of time of data packets pushed to demuxer->audio (in bytes)
+	float time;	/// amount of time elapsed based upon samples_per_frame/sample_rate (in milliseconds)
+	float last_pts; /// last pts seen
+	int bitrate;	/// bitrate computed as size/time
+} aac_priv_t;
+
+/// \param srate (out) sample rate
+/// \param num (out) number of audio frames in this ADTS frame
+/// \return size of the ADTS frame in bytes
+/// aac_parse_frames needs a buffer at least 8 bytes long
+int aac_parse_frame(uint8_t *buf, int *srate, int *num)
+{
+	int i = 0, sr, fl = 0, id;
+	static int srates[] = {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 0, 0, 0};
+	
+	if((buf[i] != 0xFF) || ((buf[i+1] & 0xF6) != 0xF0))
+		return 0;
+	
+	id = (buf[i+1] >> 3) & 0x01;	//id=1 mpeg2, 0: mpeg4
+	sr = (buf[i+2] >> 2)  & 0x0F;
+	if(sr > 11)
+		return 0;
+	*srate = srates[sr];
+
+	fl = ((buf[i+3] & 0x03) << 11) | (buf[i+4] << 3) | ((buf[i+5] >> 5) & 0x07);
+	*num = (buf[i+6] & 0x02) + 1;
+
+	return fl;
+}
+
+static int demux_aac_init(demuxer_t *demuxer)
+{
+	aac_priv_t *priv;
+	
+	priv = calloc(1, sizeof(aac_priv_t));
+	if(!priv)
+		return 0;
+	
+	priv->buf = (uint8_t*) malloc(8);
+	if(!priv->buf)
+		return 0;
+
+	demuxer->priv = priv;
+	return 1;
+}
+
+void demux_close_aac(demuxer_t *demuxer)
+{
+	aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
+	
+	if(!priv)
+		return;
+
+	if(priv->buf)
+		free(priv->buf);
+
+	free(demuxer->priv);
+
+	return;
+}
+
+/// returns 1 if it finds 8 ADTS frames in 32768 bytes, 0 otherwise
+int demux_aac_probe(demuxer_t *demuxer)
+{
+	int cnt = 0, c, len, srate, num;
+	off_t init, probed;
+	aac_priv_t *priv;
+	
+	if(! demux_aac_init(demuxer))
+	{
+		mp_msg(MSGT_DEMUX, MSGL_ERR, "COULDN'T INIT aac_demux, exit\n");
+		return 0;
+	}
+	
+	priv = (aac_priv_t *) demuxer->priv;
+
+	init = probed = stream_tell(demuxer->stream);
+	while(probed-init <= 32768 && cnt < 8)
+	{
+		c = 0;
+		while(c != 0xFF)
+		{
+			c = stream_read_char(demuxer->stream);
+			if(c < 0)
+				goto fail;
+		}
+		priv->buf[0] = 0xFF;
+		if(stream_read(demuxer->stream, &(priv->buf[1]), 7) < 7)
+			goto fail;
+		
+		len = aac_parse_frame(priv->buf, &srate, &num);
+		if(len > 0)
+		{
+			cnt++;
+			stream_skip(demuxer->stream, len - 8);
+		}
+		probed = stream_tell(demuxer->stream);
+	}
+
+	stream_seek(demuxer->stream, init);
+	if(cnt < 8)
+		goto fail;
+	
+	mp_msg(MSGT_DEMUX, MSGL_V, "demux_aac_probe, INIT: %llu, PROBED: %llu, cnt: %d\n", init, probed, cnt);
+	return 1;
+
+fail:
+	mp_msg(MSGT_DEMUX, MSGL_V, "demux_aac_probe, failed to detect an AAC stream\n");
+	demux_close_aac(demuxer);
+	return 0;
+}
+
+int demux_aac_open(demuxer_t *demuxer)
+{
+	sh_audio_t *sh;
+
+	sh = new_sh_audio(demuxer, 0);
+	sh->ds = demuxer->audio;
+	sh->format = mmioFOURCC('M', 'P', '4', 'A');
+	demuxer->audio->sh = sh;
+
+	demuxer->filepos = stream_tell(demuxer->stream);
+	
+	return 1;
+}
+
+int demux_aac_fill_buffer(demuxer_t *demuxer)
+{
+	aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
+	demux_packet_t *dp;
+	int c1, c2, len, srate, num;
+	float tm = 0;
+
+	if(demuxer->stream->eof || (demuxer->movi_end && stream_tell(demuxer->stream) >= demuxer->movi_end))
+        	return 0;
+
+	while(! demuxer->stream->eof)
+	{
+		c1 = c2 = 0;
+		while(c1 != 0xFF)
+		{
+			c1 = stream_read_char(demuxer->stream);
+			if(c1 < 0)
+				return 0;
+		}
+		c2 = stream_read_char(demuxer->stream);
+		if(c2 < 0)
+			return 0;
+		if((c2 & 0xF6) != 0xF0)
+			continue;
+
+		priv->buf[0] = (unsigned char) c1;
+		priv->buf[1] = (unsigned char) c2;
+		if(stream_read(demuxer->stream, &(priv->buf[2]), 6) < 6)
+			return 0;
+		
+		len = aac_parse_frame(priv->buf, &srate, &num);
+		if(len > 0)
+		{
+			dp = new_demux_packet(len);
+			if(! dp)
+			{
+				mp_msg(MSGT_DEMUX, MSGL_ERR, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", len);
+				return 0;
+			}
+			
+			
+			memcpy(dp->buffer, priv->buf, 8);
+			stream_read(demuxer->stream, &(dp->buffer[8]), len-8);
+			if(srate)
+				tm = (float) (num * 1024.0/srate);
+			priv->last_pts += tm;
+			dp->pts = priv->last_pts;
+			//fprintf(stderr, "\nPTS: %.3f\n", dp->pts);
+			ds_add_packet(demuxer->audio, dp);
+			priv->size += len;
+			priv->time += tm;
+			
+			priv->bitrate = (int) (priv->size / priv->time);
+			demuxer->filepos = stream_tell(demuxer->stream);
+			
+			return len;
+		}
+		else
+			stream_skip(demuxer->stream, -6);
+	}
+
+	return 0;
+}
+
+
+//This is an almost verbatim copy of high_res_mp3_seek(), from demux_audio.c
+void demux_aac_seek(demuxer_t *demuxer, float rel_seek_secs, int flags)
+{
+	aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
+	demux_stream_t *d_audio=demuxer->audio;
+	sh_audio_t *sh_audio=d_audio->sh;
+	float time;
+
+	ds_free_packs(d_audio);
+
+	time = (flags & 1) ? rel_seek_secs - priv->last_pts : rel_seek_secs;
+	if(time < 0) 
+	{
+		stream_seek(demuxer->stream, demuxer->movi_start);
+		time = priv->last_pts + time;
+		priv->last_pts = 0;
+	}
+
+	if(time > 0)
+	{
+		int len, nf, srate, num;
+
+		nf = time * sh_audio->samplerate/1024;
+		
+		while(nf > 0) 
+		{
+			if(stream_read(demuxer->stream,priv->buf, 8) < 8)
+				break;
+			len = aac_parse_frame(priv->buf, &srate, &num);
+			if(len <= 0) 
+			{
+				stream_skip(demuxer->stream, -7);
+				continue;
+			}
+			stream_skip(demuxer->stream, len - 8);
+			priv->last_pts += (float) (num*1024.0/srate);
+			nf -= num;
+		}
+	}
+	sh_audio->delay = priv->last_pts - (ds_tell_pts(demuxer->audio)-sh_audio->a_in_buffer_len)/(float)priv->bitrate;
+	resync_audio_stream(sh_audio);
+}
+
--- a/libmpdemux/demuxer.c	Mon Jun 13 20:09:05 2005 +0000
+++ b/libmpdemux/demuxer.c	Mon Jun 13 20:40:29 2005 +0000
@@ -157,6 +157,7 @@
 extern void demux_close_ty(demuxer_t* demuxer);
 extern void demux_close_lavf(demuxer_t* demuxer);
 extern void demux_close_vqf(demuxer_t* demuxer);
+extern void demux_close_aac(demuxer_t* demuxer);
 #ifdef USE_WIN32DLL
 extern void demux_close_avs(demuxer_t* demuxer);
 #endif
@@ -247,6 +248,8 @@
       demux_close_ra(demuxer); break;
     case DEMUXER_TYPE_VQF:
       demux_close_vqf(demuxer); break;
+    case DEMUXER_TYPE_AAC:
+      demux_close_aac(demuxer); break;
 #ifdef USE_LIBAVFORMAT
     case DEMUXER_TYPE_LAVF:
       demux_close_lavf(demuxer); break;
@@ -338,6 +341,7 @@
 int demux_ra_fill_buffer(demuxer_t *demux);
 int demux_vqf_fill_buffer(demuxer_t *demux);
 
+extern int demux_aac_fill_buffer(demuxer_t *demux);
 extern int demux_demuxers_fill_buffer(demuxer_t *demux,demux_stream_t *ds);
 extern int demux_ogg_fill_buffer(demuxer_t *d);
 extern int demux_rawaudio_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds);
@@ -405,6 +409,7 @@
 	return demux_ts_fill_buffer(demux);
     case DEMUXER_TYPE_REALAUDIO: return demux_ra_fill_buffer(demux);
     case DEMUXER_TYPE_VQF: return demux_vqf_fill_buffer(demux);
+    case DEMUXER_TYPE_AAC: return demux_aac_fill_buffer(demux);
 #ifdef USE_LIBAVFORMAT
      case DEMUXER_TYPE_LAVF: return demux_lavf_fill_buffer(demux);
 #endif
@@ -612,6 +617,8 @@
 int avs_check_file(demuxer_t *demuxer, const char *filename);
 #endif
 
+extern int demux_aac_open(demuxer_t *demuxer);
+extern int demux_aac_probe(demuxer_t *demuxer);
 extern int vivo_check_file(demuxer_t *demuxer);
 extern void demux_open_vivo(demuxer_t *demuxer);
 extern int y4m_check_file(demuxer_t *demuxer);
@@ -1095,6 +1102,14 @@
   }
 }
 #endif
+//================Try AAC ===============================
+if(file_format==DEMUXER_TYPE_UNKNOWN || file_format==DEMUXER_TYPE_AAC) {
+ demuxer=new_demuxer(stream,DEMUXER_TYPE_AAC,audio_id,video_id,dvdsub_id);
+ if(! demux_aac_probe(demuxer)) {
+   free_demuxer(demuxer);
+   demuxer = NULL;
+ } else file_format = DEMUXER_TYPE_AAC;
+}
 //=============== Unknown, exiting... ===========================
 if(file_format==DEMUXER_TYPE_UNKNOWN || demuxer == NULL){
   //mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized); // will be done by mplayer.c after fallback to playlist-parsing
@@ -1302,6 +1317,10 @@
   if (!demux_open_vqf(demuxer)) return NULL;
   break;
  }
+ case DEMUXER_TYPE_AAC: {
+  if (!demux_aac_open(demuxer)) return NULL;
+  break;
+ }
 #ifdef USE_LIBAVFORMAT
   case DEMUXER_TYPE_LAVF: {
   if (!demux_open_lavf(demuxer)) return NULL;
@@ -1431,6 +1450,7 @@
 extern void demux_xmms_seek(demuxer_t *demuxer,float rel_seek_secs,int flags);
 extern void demux_mkv_seek(demuxer_t *demuxer,float rel_seek_secs,int flags);
 extern void demux_seek_vqf(demuxer_t *demuxer,float rel_seek_secs,int flags);
+extern void demux_aac_seek(demuxer_t *demuxer,float rel_seek_secs,int flags);
 
 int demux_seek(demuxer_t *demuxer,float rel_seek_secs,int flags){
     demux_stream_t *d_audio=demuxer->audio;
@@ -1539,6 +1559,8 @@
  case DEMUXER_TYPE_AVS:
       demux_seek_avs(demuxer,rel_seek_secs,flags); break;
  #endif
+ case DEMUXER_TYPE_AAC:
+      demux_aac_seek(demuxer,rel_seek_secs,flags); break;
 
 } // switch(demuxer->file_format)
 
--- a/libmpdemux/demuxer.h	Mon Jun 13 20:09:05 2005 +0000
+++ b/libmpdemux/demuxer.h	Mon Jun 13 20:40:29 2005 +0000
@@ -46,11 +46,12 @@
 #define DEMUXER_TYPE_NSV 36
 #define DEMUXER_TYPE_VQF 37
 #define DEMUXER_TYPE_AVS 38
+#define DEMUXER_TYPE_AAC 39
 
 // This should always match the higest demuxer type number.
 // Unless you want to disallow users to force the demuxer to some types
 #define DEMUXER_TYPE_MIN 0
-#define DEMUXER_TYPE_MAX 38
+#define DEMUXER_TYPE_MAX 39
 
 #define DEMUXER_TYPE_DEMUXERS (1<<16)
 // A virtual demuxer type for the network code