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