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 "internal.h"
00031 #include "oggdec.h"
00032 #include "riff.h"
00033
00034 static int
00035 ogm_header(AVFormatContext *s, int idx)
00036 {
00037 struct ogg *ogg = s->priv_data;
00038 struct ogg_stream *os = ogg->streams + idx;
00039 AVStream *st = s->streams[idx];
00040 const uint8_t *p = os->buf + os->pstart;
00041 uint64_t time_unit;
00042 uint64_t spu;
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 p += 4;
00078 p += 8;
00079
00080 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
00081 st->codec->width = bytestream_get_le32(&p);
00082 st->codec->height = bytestream_get_le32(&p);
00083 st->codec->time_base.den = spu * 10000000;
00084 st->codec->time_base.num = time_unit;
00085 avpriv_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
00086 } else {
00087 st->codec->channels = bytestream_get_le16(&p);
00088 p += 2;
00089 st->codec->bit_rate = bytestream_get_le32(&p) * 8;
00090 st->codec->sample_rate = spu * 10000000 / time_unit;
00091 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00092 }
00093 } else if (*p == 3) {
00094 if (os->psize > 8)
00095 ff_vorbis_comment(s, &st->metadata, p+7, os->psize-8);
00096 }
00097
00098 return 1;
00099 }
00100
00101 static int
00102 ogm_dshow_header(AVFormatContext *s, int idx)
00103 {
00104 struct ogg *ogg = s->priv_data;
00105 struct ogg_stream *os = ogg->streams + idx;
00106 AVStream *st = s->streams[idx];
00107 uint8_t *p = os->buf + os->pstart;
00108 uint32_t t;
00109
00110 if(!(*p & 1))
00111 return 0;
00112 if(*p != 1)
00113 return 1;
00114
00115 t = AV_RL32(p + 96);
00116
00117 if(t == 0x05589f80){
00118 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00119 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, AV_RL32(p + 68));
00120 st->codec->time_base.den = 10000000;
00121 st->codec->time_base.num = AV_RL64(p + 164);
00122 st->codec->width = AV_RL32(p + 176);
00123 st->codec->height = AV_RL32(p + 180);
00124 } else if(t == 0x05589f81){
00125 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00126 st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, AV_RL16(p + 124));
00127 st->codec->channels = AV_RL16(p + 126);
00128 st->codec->sample_rate = AV_RL32(p + 128);
00129 st->codec->bit_rate = AV_RL32(p + 132) * 8;
00130 }
00131
00132 return 1;
00133 }
00134
00135 static int
00136 ogm_packet(AVFormatContext *s, int idx)
00137 {
00138 struct ogg *ogg = s->priv_data;
00139 struct ogg_stream *os = ogg->streams + idx;
00140 uint8_t *p = os->buf + os->pstart;
00141 int lb;
00142
00143 if(*p & 8)
00144 os->pflags |= AV_PKT_FLAG_KEY;
00145
00146 lb = ((*p & 2) << 1) | ((*p >> 6) & 3);
00147 os->pstart += lb + 1;
00148 os->psize -= lb + 1;
00149
00150 while (lb--)
00151 os->pduration += p[lb+1] << (lb*8);
00152
00153 return 0;
00154 }
00155
00156 const struct ogg_codec ff_ogm_video_codec = {
00157 .magic = "\001video",
00158 .magicsize = 6,
00159 .header = ogm_header,
00160 .packet = ogm_packet,
00161 .granule_is_start = 1,
00162 };
00163
00164 const struct ogg_codec ff_ogm_audio_codec = {
00165 .magic = "\001audio",
00166 .magicsize = 6,
00167 .header = ogm_header,
00168 .packet = ogm_packet,
00169 .granule_is_start = 1,
00170 };
00171
00172 const struct ogg_codec ff_ogm_text_codec = {
00173 .magic = "\001text",
00174 .magicsize = 5,
00175 .header = ogm_header,
00176 .packet = ogm_packet,
00177 .granule_is_start = 1,
00178 };
00179
00180 const struct ogg_codec ff_ogm_old_codec = {
00181 .magic = "\001Direct Show Samples embedded in Ogg",
00182 .magicsize = 35,
00183 .header = ogm_dshow_header,
00184 .packet = ogm_packet,
00185 .granule_is_start = 1,
00186 };