00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avformat.h"
00028 #include "internal.h"
00029 #include "riff.h"
00030
00031 static const AVCodecTag codec_smjpeg_tags[] = {
00032 { CODEC_ID_ADPCM_IMA_SMJPEG, MKTAG('A', 'P', 'C', 'M') },
00033 { CODEC_ID_PCM_S16LE, MKTAG('N', 'O', 'N', 'E') },
00034 { CODEC_ID_MJPEG, MKTAG('J', 'F', 'I', 'F') },
00035 { CODEC_ID_NONE, 0 },
00036 };
00037
00038 typedef struct SMJPEGContext {
00039 int audio_stream_index;
00040 int video_stream_index;
00041 } SMJPEGContext;
00042
00043 static int smjpeg_probe(AVProbeData *p)
00044 {
00045 if (!memcmp(p->buf, "\x0\xaSMJPEG", 8))
00046 return AVPROBE_SCORE_MAX;
00047 return 0;
00048 }
00049
00050 static int smjpeg_read_header(AVFormatContext *s, AVFormatParameters *ap)
00051 {
00052 SMJPEGContext *sc = s->priv_data;
00053 AVStream *ast = NULL, *vst = NULL;
00054 AVIOContext *pb = s->pb;
00055 uint32_t version, htype, hlength, duration;
00056 char *comment;
00057
00058 avio_skip(pb, 8);
00059 version = avio_rb32(pb);
00060 if (version) {
00061 av_log_ask_for_sample(s, "unknown version %d\n", version);
00062 }
00063 duration = avio_rb32(pb);
00064
00065 while (!pb->eof_reached) {
00066 htype = avio_rl32(pb);
00067 switch (htype) {
00068 case MKTAG('_', 'T', 'X', 'T'):
00069 hlength = avio_rb32(pb);
00070 if (!hlength || hlength > 512)
00071 return AVERROR_INVALIDDATA;
00072 comment = av_malloc(hlength + 1);
00073 if (!comment)
00074 return AVERROR(ENOMEM);
00075 if (avio_read(pb, comment, hlength) != hlength) {
00076 av_freep(&comment);
00077 av_log(s, AV_LOG_ERROR, "error when reading comment\n");
00078 return AVERROR_INVALIDDATA;
00079 }
00080 comment[hlength] = 0;
00081 av_dict_set(&s->metadata, "comment", comment,
00082 AV_DICT_DONT_STRDUP_VAL);
00083 break;
00084 case MKTAG('_', 'S', 'N', 'D'):
00085 if (ast) {
00086 av_log_ask_for_sample(s, "multiple audio streams not supported\n");
00087 return AVERROR_INVALIDDATA;
00088 }
00089 hlength = avio_rb32(pb);
00090 if (hlength < 8)
00091 return AVERROR_INVALIDDATA;
00092 ast = avformat_new_stream(s, 0);
00093 if (!ast)
00094 return AVERROR(ENOMEM);
00095 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00096 ast->codec->sample_rate = avio_rb16(pb);
00097 ast->codec->bits_per_coded_sample = avio_r8(pb);
00098 ast->codec->channels = avio_r8(pb);
00099 ast->codec->codec_tag = avio_rl32(pb);
00100 ast->codec->codec_id = ff_codec_get_id(codec_smjpeg_tags,
00101 ast->codec->codec_tag);
00102 ast->duration = duration;
00103 sc->audio_stream_index = ast->index;
00104 avpriv_set_pts_info(ast, 32, 1, 1000);
00105 avio_skip(pb, hlength - 8);
00106 break;
00107 case MKTAG('_', 'V', 'I', 'D'):
00108 if (vst) {
00109 av_log_ask_for_sample(s, "multiple video streams not supported\n");
00110 return AVERROR_INVALIDDATA;
00111 }
00112 hlength = avio_rb32(pb);
00113 if (hlength < 12)
00114 return AVERROR_INVALIDDATA;
00115 avio_skip(pb, 4);
00116 vst = avformat_new_stream(s, 0);
00117 if (!vst)
00118 return AVERROR(ENOMEM);
00119 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00120 vst->codec->width = avio_rb16(pb);
00121 vst->codec->height = avio_rb16(pb);
00122 vst->codec->codec_tag = avio_rl32(pb);
00123 vst->codec->codec_id = ff_codec_get_id(codec_smjpeg_tags,
00124 vst->codec->codec_tag);
00125 vst->duration = duration;
00126 sc->video_stream_index = vst->index;
00127 avpriv_set_pts_info(vst, 32, 1, 1000);
00128 avio_skip(pb, hlength - 12);
00129 break;
00130 case MKTAG('H', 'E', 'N', 'D'):
00131 return 0;
00132 default:
00133 av_log(s, AV_LOG_ERROR, "unknown header %x\n", htype);
00134 return AVERROR_INVALIDDATA;
00135 }
00136 }
00137
00138 return AVERROR_EOF;
00139 }
00140
00141 static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
00142 {
00143 SMJPEGContext *sc = s->priv_data;
00144 uint32_t dtype, ret, size, timestamp;
00145
00146 if (s->pb->eof_reached)
00147 return AVERROR_EOF;
00148 dtype = avio_rl32(s->pb);
00149 switch (dtype) {
00150 case MKTAG('s', 'n', 'd', 'D'):
00151 timestamp = avio_rb32(s->pb);
00152 size = avio_rb32(s->pb);
00153 ret = av_get_packet(s->pb, pkt, size);
00154 pkt->stream_index = sc->audio_stream_index;
00155 pkt->pts = timestamp;
00156 break;
00157 case MKTAG('v', 'i', 'd', 'D'):
00158 timestamp = avio_rb32(s->pb);
00159 size = avio_rb32(s->pb);
00160 ret = av_get_packet(s->pb, pkt, size);
00161 pkt->stream_index = sc->video_stream_index;
00162 pkt->pts = timestamp;
00163 break;
00164 case MKTAG('D', 'O', 'N', 'E'):
00165 ret = AVERROR_EOF;
00166 break;
00167 default:
00168 av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", dtype);
00169 ret = AVERROR_INVALIDDATA;
00170 break;
00171 }
00172 return ret;
00173 }
00174
00175 AVInputFormat ff_smjpeg_demuxer = {
00176 .name = "smjpeg",
00177 .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
00178 .priv_data_size = sizeof(SMJPEGContext),
00179 .read_probe = smjpeg_probe,
00180 .read_header = smjpeg_read_header,
00181 .read_packet = smjpeg_read_packet,
00182 .extensions = "mjpg",
00183 };