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