00001
00025 #include <stdlib.h>
00026 #include "avformat.h"
00027 #include "bitstream.h"
00028 #include "bytestream.h"
00029 #include "intreadwrite.h"
00030 #include "oggdec.h"
00031 #include "riff.h"
00032
00033 static int
00034 ogm_header(AVFormatContext *s, int idx)
00035 {
00036 ogg_t *ogg = s->priv_data;
00037 ogg_stream_t *os = ogg->streams + idx;
00038 AVStream *st = s->streams[idx];
00039 uint8_t *p = os->buf + os->pstart;
00040 uint64_t time_unit;
00041 uint64_t spu;
00042 uint32_t default_len;
00043
00044 if(!(*p & 1))
00045 return 0;
00046 if(*p != 1)
00047 return 1;
00048
00049 p++;
00050
00051 if(*p == 'v'){
00052 int tag;
00053 st->codec->codec_type = CODEC_TYPE_VIDEO;
00054 p += 8;
00055 tag = bytestream_get_le32(&p);
00056 st->codec->codec_id = codec_get_id(codec_bmp_tags, tag);
00057 st->codec->codec_tag = tag;
00058 } else {
00059 uint8_t acid[5];
00060 int cid;
00061 st->codec->codec_type = CODEC_TYPE_AUDIO;
00062 p += 8;
00063 bytestream_get_buffer(&p, acid, 4);
00064 acid[4] = 0;
00065 cid = strtol(acid, NULL, 16);
00066 st->codec->codec_id = codec_get_id(codec_wav_tags, cid);
00067 }
00068
00069 p += 4;
00070
00071 time_unit = bytestream_get_le64(&p);
00072 spu = bytestream_get_le64(&p);
00073 default_len = bytestream_get_le32(&p);
00074
00075 p += 8;
00076
00077 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
00078 st->codec->width = bytestream_get_le32(&p);
00079 st->codec->height = bytestream_get_le32(&p);
00080 st->codec->time_base.den = spu * 10000000;
00081 st->codec->time_base.num = time_unit;
00082 st->time_base = st->codec->time_base;
00083 } else {
00084 st->codec->channels = bytestream_get_le16(&p);
00085 p += 2;
00086 st->codec->bit_rate = bytestream_get_le32(&p) * 8;
00087 st->codec->sample_rate = spu * 10000000 / time_unit;
00088 st->time_base.num = 1;
00089 st->time_base.den = st->codec->sample_rate;
00090 }
00091
00092 return 1;
00093 }
00094
00095 static int
00096 ogm_dshow_header(AVFormatContext *s, int idx)
00097 {
00098 ogg_t *ogg = s->priv_data;
00099 ogg_stream_t *os = ogg->streams + idx;
00100 AVStream *st = s->streams[idx];
00101 uint8_t *p = os->buf + os->pstart;
00102 uint32_t t;
00103
00104 if(!(*p & 1))
00105 return 0;
00106 if(*p != 1)
00107 return 1;
00108
00109 t = AV_RL32(p + 96);
00110
00111 if(t == 0x05589f80){
00112 st->codec->codec_type = CODEC_TYPE_VIDEO;
00113 st->codec->codec_id = codec_get_id(codec_bmp_tags, AV_RL32(p + 68));
00114 st->codec->time_base.den = 10000000;
00115 st->codec->time_base.num = AV_RL64(p + 164);
00116 st->codec->width = AV_RL32(p + 176);
00117 st->codec->height = AV_RL32(p + 180);
00118 } else if(t == 0x05589f81){
00119 st->codec->codec_type = CODEC_TYPE_AUDIO;
00120 st->codec->codec_id = codec_get_id(codec_wav_tags, AV_RL16(p + 124));
00121 st->codec->channels = AV_RL16(p + 126);
00122 st->codec->sample_rate = AV_RL32(p + 128);
00123 st->codec->bit_rate = AV_RL32(p + 132) * 8;
00124 }
00125
00126 return 1;
00127 }
00128
00129 static int
00130 ogm_packet(AVFormatContext *s, int idx)
00131 {
00132 ogg_t *ogg = s->priv_data;
00133 ogg_stream_t *os = ogg->streams + idx;
00134 uint8_t *p = os->buf + os->pstart;
00135 int lb;
00136
00137 if(*p & 8)
00138 os->pflags |= PKT_FLAG_KEY;
00139
00140 lb = ((*p & 2) << 1) | ((*p >> 6) & 3);
00141 os->pstart += lb + 1;
00142 os->psize -= lb + 1;
00143
00144 return 0;
00145 }
00146
00147 ogg_codec_t ogm_video_codec = {
00148 .magic = "\001video",
00149 .magicsize = 6,
00150 .header = ogm_header,
00151 .packet = ogm_packet
00152 };
00153
00154 ogg_codec_t ogm_audio_codec = {
00155 .magic = "\001audio",
00156 .magicsize = 6,
00157 .header = ogm_header,
00158 .packet = ogm_packet
00159 };
00160
00161 ogg_codec_t ogm_old_codec = {
00162 .magic = "\001Direct Show Samples embedded in Ogg",
00163 .magicsize = 35,
00164 .header = ogm_dshow_header,
00165 .packet = ogm_packet
00166 };