00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/channel_layout.h"
00023 #include "avformat.h"
00024 #include "internal.h"
00025 #include "avio_internal.h"
00026 #include "pcm.h"
00027 #include "riff.h"
00028
00029 typedef struct {
00030 int64_t atrpos, atsqpos, awapos;
00031 int64_t data_end;
00032 } MMFContext;
00033
00034 static const int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
00035
00036 static int mmf_rate(int code)
00037 {
00038 if((code < 0) || (code > 4))
00039 return -1;
00040 return mmf_rates[code];
00041 }
00042
00043 #if CONFIG_MMF_MUXER
00044 static int mmf_rate_code(int rate)
00045 {
00046 int i;
00047 for(i = 0; i < 5; i++)
00048 if(mmf_rates[i] == rate)
00049 return i;
00050 return -1;
00051 }
00052
00053
00054 static void end_tag_be(AVIOContext *pb, int64_t start)
00055 {
00056 int64_t pos;
00057
00058 pos = avio_tell(pb);
00059 avio_seek(pb, start - 4, SEEK_SET);
00060 avio_wb32(pb, (uint32_t)(pos - start));
00061 avio_seek(pb, pos, SEEK_SET);
00062 }
00063
00064 static int mmf_write_header(AVFormatContext *s)
00065 {
00066 MMFContext *mmf = s->priv_data;
00067 AVIOContext *pb = s->pb;
00068 int64_t pos;
00069 int rate;
00070
00071 rate = mmf_rate_code(s->streams[0]->codec->sample_rate);
00072 if(rate < 0) {
00073 av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d, supported are 4000, 8000, 11025, 22050 and 44100\n", s->streams[0]->codec->sample_rate);
00074 return AVERROR(EINVAL);
00075 }
00076
00077 ffio_wfourcc(pb, "MMMD");
00078 avio_wb32(pb, 0);
00079 pos = ff_start_tag(pb, "CNTI");
00080 avio_w8(pb, 0);
00081 avio_w8(pb, 1);
00082 avio_w8(pb, 1);
00083 avio_w8(pb, 0);
00084 avio_w8(pb, 0);
00085 avio_write(pb, "VN:libavcodec,", sizeof("VN:libavcodec,") -1);
00086 end_tag_be(pb, pos);
00087
00088 avio_write(pb, "ATR\x00", 4);
00089 avio_wb32(pb, 0);
00090 mmf->atrpos = avio_tell(pb);
00091 avio_w8(pb, 0);
00092 avio_w8(pb, 0);
00093 avio_w8(pb, (0 << 7) | (1 << 4) | rate);
00094 avio_w8(pb, 0);
00095 avio_w8(pb, 2);
00096 avio_w8(pb, 2);
00097
00098 ffio_wfourcc(pb, "Atsq");
00099 avio_wb32(pb, 16);
00100 mmf->atsqpos = avio_tell(pb);
00101
00102 avio_write(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
00103
00104 mmf->awapos = ff_start_tag(pb, "Awa\x01");
00105
00106 avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
00107
00108 avio_flush(pb);
00109
00110 return 0;
00111 }
00112
00113 static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
00114 {
00115 AVIOContext *pb = s->pb;
00116 avio_write(pb, pkt->data, pkt->size);
00117 return 0;
00118 }
00119
00120
00121 static void put_varlength(AVIOContext *pb, int val)
00122 {
00123 if(val < 128)
00124 avio_w8(pb, val);
00125 else {
00126 val -= 128;
00127 avio_w8(pb, 0x80 | val >> 7);
00128 avio_w8(pb, 0x7f & val);
00129 }
00130 }
00131
00132 static int mmf_write_trailer(AVFormatContext *s)
00133 {
00134 AVIOContext *pb = s->pb;
00135 MMFContext *mmf = s->priv_data;
00136 int64_t pos, size;
00137 int gatetime;
00138
00139 if (s->pb->seekable) {
00140
00141 end_tag_be(pb, mmf->awapos);
00142 end_tag_be(pb, mmf->atrpos);
00143 end_tag_be(pb, 8);
00144
00145 pos = avio_tell(pb);
00146 size = pos - mmf->awapos;
00147
00148
00149 avio_seek(pb, mmf->atsqpos, SEEK_SET);
00150
00151
00152 avio_w8(pb, 0);
00153 avio_w8(pb, 1);
00154 gatetime = size * 500 / s->streams[0]->codec->sample_rate;
00155 put_varlength(pb, gatetime);
00156
00157
00158 put_varlength(pb, gatetime);
00159 avio_write(pb, "\xff\x00", 2);
00160
00161
00162 avio_write(pb, "\x00\x00\x00\x00", 4);
00163
00164 avio_seek(pb, pos, SEEK_SET);
00165
00166 avio_flush(pb);
00167 }
00168 return 0;
00169 }
00170 #endif
00171
00172 static int mmf_probe(AVProbeData *p)
00173 {
00174
00175 if (p->buf[0] == 'M' && p->buf[1] == 'M' &&
00176 p->buf[2] == 'M' && p->buf[3] == 'D' &&
00177 p->buf[8] == 'C' && p->buf[9] == 'N' &&
00178 p->buf[10] == 'T' && p->buf[11] == 'I')
00179 return AVPROBE_SCORE_MAX;
00180 else
00181 return 0;
00182 }
00183
00184
00185 static int mmf_read_header(AVFormatContext *s)
00186 {
00187 MMFContext *mmf = s->priv_data;
00188 unsigned int tag;
00189 AVIOContext *pb = s->pb;
00190 AVStream *st;
00191 int64_t size;
00192 int rate, params;
00193
00194 tag = avio_rl32(pb);
00195 if (tag != MKTAG('M', 'M', 'M', 'D'))
00196 return AVERROR_INVALIDDATA;
00197 avio_skip(pb, 4);
00198
00199
00200 for(;; avio_skip(pb, size)) {
00201 tag = avio_rl32(pb);
00202 size = avio_rb32(pb);
00203 if(tag == MKTAG('C','N','T','I')) continue;
00204 if(tag == MKTAG('O','P','D','A')) continue;
00205 break;
00206 }
00207
00208
00209 if ((tag & 0xffffff) == MKTAG('M', 'T', 'R', 0)) {
00210 av_log(s, AV_LOG_ERROR, "MIDI like format found, unsupported\n");
00211 return AVERROR_PATCHWELCOME;
00212 }
00213 if ((tag & 0xffffff) != MKTAG('A', 'T', 'R', 0)) {
00214 av_log(s, AV_LOG_ERROR, "Unsupported SMAF chunk %08x\n", tag);
00215 return AVERROR_PATCHWELCOME;
00216 }
00217
00218 avio_r8(pb);
00219 avio_r8(pb);
00220 params = avio_r8(pb);
00221 rate = mmf_rate(params & 0x0f);
00222 if(rate < 0) {
00223 av_log(s, AV_LOG_ERROR, "Invalid sample rate\n");
00224 return AVERROR_INVALIDDATA;
00225 }
00226 avio_r8(pb);
00227 avio_r8(pb);
00228 avio_r8(pb);
00229
00230
00231 for(;; avio_skip(pb, size)) {
00232 tag = avio_rl32(pb);
00233 size = avio_rb32(pb);
00234 if(tag == MKTAG('A','t','s','q')) continue;
00235 if(tag == MKTAG('A','s','p','I')) continue;
00236 break;
00237 }
00238
00239
00240 if ((tag & 0xffffff) != MKTAG('A', 'w', 'a', 0)) {
00241 av_log(s, AV_LOG_ERROR, "Unexpected SMAF chunk %08x\n", tag);
00242 return AVERROR_INVALIDDATA;
00243 }
00244 mmf->data_end = avio_tell(pb) + size;
00245
00246 st = avformat_new_stream(s, NULL);
00247 if (!st)
00248 return AVERROR(ENOMEM);
00249
00250 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00251 st->codec->codec_id = AV_CODEC_ID_ADPCM_YAMAHA;
00252 st->codec->sample_rate = rate;
00253 st->codec->channels = 1;
00254 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
00255 st->codec->bits_per_coded_sample = 4;
00256 st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_coded_sample;
00257
00258 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00259
00260 return 0;
00261 }
00262
00263 #define MAX_SIZE 4096
00264
00265 static int mmf_read_packet(AVFormatContext *s,
00266 AVPacket *pkt)
00267 {
00268 MMFContext *mmf = s->priv_data;
00269 int64_t left, size;
00270 int ret;
00271
00272 left = mmf->data_end - avio_tell(s->pb);
00273 size = FFMIN(left, MAX_SIZE);
00274 if (url_feof(s->pb) || size <= 0)
00275 return AVERROR_EOF;
00276
00277 ret = av_get_packet(s->pb, pkt, size);
00278 if (ret < 0)
00279 return ret;
00280
00281 pkt->stream_index = 0;
00282
00283 return ret;
00284 }
00285
00286 #if CONFIG_MMF_DEMUXER
00287 AVInputFormat ff_mmf_demuxer = {
00288 .name = "mmf",
00289 .long_name = NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
00290 .priv_data_size = sizeof(MMFContext),
00291 .read_probe = mmf_probe,
00292 .read_header = mmf_read_header,
00293 .read_packet = mmf_read_packet,
00294 .flags = AVFMT_GENERIC_INDEX,
00295 };
00296 #endif
00297 #if CONFIG_MMF_MUXER
00298 AVOutputFormat ff_mmf_muxer = {
00299 .name = "mmf",
00300 .long_name = NULL_IF_CONFIG_SMALL("Yamaha SMAF"),
00301 .mime_type = "application/vnd.smaf",
00302 .extensions = "mmf",
00303 .priv_data_size = sizeof(MMFContext),
00304 .audio_codec = AV_CODEC_ID_ADPCM_YAMAHA,
00305 .video_codec = AV_CODEC_ID_NONE,
00306 .write_header = mmf_write_header,
00307 .write_packet = mmf_write_packet,
00308 .write_trailer = mmf_write_trailer,
00309 };
00310 #endif