libavformat/smjpegdec.c
Go to the documentation of this file.
00001 /*
00002  * SMJPEG demuxer
00003  * Copyright (c) 2011 Paul B Mahol
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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, AVFormatParameters *ap)
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); // magic
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); // in msec
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); // number of frames
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 
00140     if (s->pb->eof_reached)
00141         return AVERROR_EOF;
00142     dtype = avio_rl32(s->pb);
00143     switch (dtype) {
00144     case SMJPEG_SNDD:
00145         timestamp = avio_rb32(s->pb);
00146         size = avio_rb32(s->pb);
00147         ret = av_get_packet(s->pb, pkt, size);
00148         pkt->stream_index = sc->audio_stream_index;
00149         pkt->pts = timestamp;
00150         break;
00151     case SMJPEG_VIDD:
00152         timestamp = avio_rb32(s->pb);
00153         size = avio_rb32(s->pb);
00154         ret = av_get_packet(s->pb, pkt, size);
00155         pkt->stream_index = sc->video_stream_index;
00156         pkt->pts = timestamp;
00157         break;
00158     case SMJPEG_DONE:
00159         ret = AVERROR_EOF;
00160         break;
00161     default:
00162         av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", dtype);
00163         ret = AVERROR_INVALIDDATA;
00164         break;
00165     }
00166     return ret;
00167 }
00168 
00169 AVInputFormat ff_smjpeg_demuxer = {
00170     .name           = "smjpeg",
00171     .long_name      = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
00172     .priv_data_size = sizeof(SMJPEGContext),
00173     .read_probe     = smjpeg_probe,
00174     .read_header    = smjpeg_read_header,
00175     .read_packet    = smjpeg_read_packet,
00176     .extensions     = "mjpg",
00177 };