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