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