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 #include "smjpeg.h"
00031 
00032 typedef struct SMJPEGContext {
00033     int audio_stream_index;
00034     int video_stream_index;
00035 } SMJPEGContext;
00036 
00037 static int smjpeg_probe(AVProbeData *p)
00038 {
00039     if (!memcmp(p->buf, SMJPEG_MAGIC, 8))
00040         return AVPROBE_SCORE_MAX;
00041     return 0;
00042 }
00043 
00044 static int smjpeg_read_header(AVFormatContext *s)
00045 {
00046     SMJPEGContext *sc = s->priv_data;
00047     AVStream *ast = NULL, *vst = NULL;
00048     AVIOContext *pb = s->pb;
00049     uint32_t version, htype, hlength, duration;
00050     char *comment;
00051 
00052     avio_skip(pb, 8); 
00053     version = avio_rb32(pb);
00054     if (version)
00055         av_log_ask_for_sample(s, "unknown version %d\n", version);
00056 
00057     duration = avio_rb32(pb); 
00058 
00059     while (!pb->eof_reached) {
00060         htype = avio_rl32(pb);
00061         switch (htype) {
00062         case SMJPEG_TXT:
00063             hlength = avio_rb32(pb);
00064             if (!hlength || hlength > 512)
00065                 return AVERROR_INVALIDDATA;
00066             comment = av_malloc(hlength + 1);
00067             if (!comment)
00068                 return AVERROR(ENOMEM);
00069             if (avio_read(pb, comment, hlength) != hlength) {
00070                 av_freep(&comment);
00071                 av_log(s, AV_LOG_ERROR, "error when reading comment\n");
00072                 return AVERROR_INVALIDDATA;
00073             }
00074             comment[hlength] = 0;
00075             av_dict_set(&s->metadata, "comment", comment,
00076                         AV_DICT_DONT_STRDUP_VAL);
00077             break;
00078         case SMJPEG_SND:
00079             if (ast) {
00080                 av_log_ask_for_sample(s, "multiple audio streams not supported\n");
00081                 return AVERROR_INVALIDDATA;
00082             }
00083             hlength = avio_rb32(pb);
00084             if (hlength < 8)
00085                 return AVERROR_INVALIDDATA;
00086             ast = avformat_new_stream(s, 0);
00087             if (!ast)
00088                 return AVERROR(ENOMEM);
00089             ast->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
00090             ast->codec->sample_rate = avio_rb16(pb);
00091             ast->codec->bits_per_coded_sample = avio_r8(pb);
00092             ast->codec->channels    = avio_r8(pb);
00093             ast->codec->codec_tag   = avio_rl32(pb);
00094             ast->codec->codec_id    = ff_codec_get_id(ff_codec_smjpeg_audio_tags,
00095                                                       ast->codec->codec_tag);
00096             ast->duration           = duration;
00097             sc->audio_stream_index  = ast->index;
00098             avpriv_set_pts_info(ast, 32, 1, 1000);
00099             avio_skip(pb, hlength - 8);
00100             break;
00101         case SMJPEG_VID:
00102             if (vst) {
00103                 av_log_ask_for_sample(s, "multiple video streams not supported\n");
00104                 return AVERROR_INVALIDDATA;
00105             }
00106             hlength = avio_rb32(pb);
00107             if (hlength < 12)
00108                 return AVERROR_INVALIDDATA;
00109             avio_skip(pb, 4); 
00110             vst = avformat_new_stream(s, 0);
00111             if (!vst)
00112                 return AVERROR(ENOMEM);
00113             vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00114             vst->codec->width      = avio_rb16(pb);
00115             vst->codec->height     = avio_rb16(pb);
00116             vst->codec->codec_tag  = avio_rl32(pb);
00117             vst->codec->codec_id   = ff_codec_get_id(ff_codec_smjpeg_video_tags,
00118                                                      vst->codec->codec_tag);
00119             vst->duration          = duration;
00120             sc->video_stream_index = vst->index;
00121             avpriv_set_pts_info(vst, 32, 1, 1000);
00122             avio_skip(pb, hlength - 12);
00123             break;
00124         case SMJPEG_HEND:
00125             return 0;
00126         default:
00127             av_log(s, AV_LOG_ERROR, "unknown header %x\n", htype);
00128             return AVERROR_INVALIDDATA;
00129         }
00130     }
00131 
00132     return AVERROR_EOF;
00133 }
00134 
00135 static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
00136 {
00137     SMJPEGContext *sc = s->priv_data;
00138     uint32_t dtype, ret, size, timestamp;
00139     int64_t pos;
00140 
00141     if (s->pb->eof_reached)
00142         return AVERROR_EOF;
00143     pos   = avio_tell(s->pb);
00144     dtype = avio_rl32(s->pb);
00145     switch (dtype) {
00146     case SMJPEG_SNDD:
00147         timestamp = avio_rb32(s->pb);
00148         size = avio_rb32(s->pb);
00149         ret = av_get_packet(s->pb, pkt, size);
00150         pkt->stream_index = sc->audio_stream_index;
00151         pkt->pts = timestamp;
00152         pkt->pos = pos;
00153         break;
00154     case SMJPEG_VIDD:
00155         timestamp = avio_rb32(s->pb);
00156         size = avio_rb32(s->pb);
00157         ret = av_get_packet(s->pb, pkt, size);
00158         pkt->stream_index = sc->video_stream_index;
00159         pkt->pts = timestamp;
00160         pkt->pos = pos;
00161         break;
00162     case SMJPEG_DONE:
00163         ret = AVERROR_EOF;
00164         break;
00165     default:
00166         av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", dtype);
00167         ret = AVERROR_INVALIDDATA;
00168         break;
00169     }
00170     return ret;
00171 }
00172 
00173 AVInputFormat ff_smjpeg_demuxer = {
00174     .name           = "smjpeg",
00175     .long_name      = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
00176     .priv_data_size = sizeof(SMJPEGContext),
00177     .read_probe     = smjpeg_probe,
00178     .read_header    = smjpeg_read_header,
00179     .read_packet    = smjpeg_read_packet,
00180     .extensions     = "mjpg",
00181     .flags          = AVFMT_GENERIC_INDEX,
00182 };