smjpegdec.c
Go to the documentation of this file.
1 /*
2  * SMJPEG demuxer
3  * Copyright (c) 2011 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * This is a demuxer for Loki SDL Motion JPEG files
25  */
26 
27 #include "avformat.h"
28 #include "internal.h"
29 #include "riff.h"
30 #include "smjpeg.h"
31 
32 typedef struct SMJPEGContext {
36 
37 static int smjpeg_probe(AVProbeData *p)
38 {
39  if (!memcmp(p->buf, SMJPEG_MAGIC, 8))
40  return AVPROBE_SCORE_MAX;
41  return 0;
42 }
43 
45 {
46  SMJPEGContext *sc = s->priv_data;
47  AVStream *ast = NULL, *vst = NULL;
48  AVIOContext *pb = s->pb;
49  uint32_t version, htype, hlength, duration;
50  char *comment;
51 
52  avio_skip(pb, 8); // magic
53  version = avio_rb32(pb);
54  if (version)
55  avpriv_request_sample(s, "Unknown version %d", version);
56 
57  duration = avio_rb32(pb); // in msec
58 
59  while (!url_feof(pb)) {
60  htype = avio_rl32(pb);
61  switch (htype) {
62  case SMJPEG_TXT:
63  hlength = avio_rb32(pb);
64  if (!hlength || hlength > 512)
65  return AVERROR_INVALIDDATA;
66  comment = av_malloc(hlength + 1);
67  if (!comment)
68  return AVERROR(ENOMEM);
69  if (avio_read(pb, comment, hlength) != hlength) {
70  av_freep(&comment);
71  av_log(s, AV_LOG_ERROR, "error when reading comment\n");
72  return AVERROR_INVALIDDATA;
73  }
74  comment[hlength] = 0;
75  av_dict_set(&s->metadata, "comment", comment,
77  break;
78  case SMJPEG_SND:
79  if (ast) {
80  avpriv_request_sample(s, "Multiple audio streams");
81  return AVERROR_PATCHWELCOME;
82  }
83  hlength = avio_rb32(pb);
84  if (hlength < 8)
85  return AVERROR_INVALIDDATA;
86  ast = avformat_new_stream(s, 0);
87  if (!ast)
88  return AVERROR(ENOMEM);
90  ast->codec->sample_rate = avio_rb16(pb);
92  ast->codec->channels = avio_r8(pb);
93  ast->codec->codec_tag = avio_rl32(pb);
95  ast->codec->codec_tag);
96  ast->duration = duration;
97  sc->audio_stream_index = ast->index;
98  avpriv_set_pts_info(ast, 32, 1, 1000);
99  avio_skip(pb, hlength - 8);
100  break;
101  case SMJPEG_VID:
102  if (vst) {
103  avpriv_request_sample(s, "Multiple video streams");
104  return AVERROR_INVALIDDATA;
105  }
106  hlength = avio_rb32(pb);
107  if (hlength < 12)
108  return AVERROR_INVALIDDATA;
109  vst = avformat_new_stream(s, 0);
110  if (!vst)
111  return AVERROR(ENOMEM);
112  vst->nb_frames = avio_rb32(pb);
113  vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
114  vst->codec->width = avio_rb16(pb);
115  vst->codec->height = avio_rb16(pb);
116  vst->codec->codec_tag = avio_rl32(pb);
117  vst->codec->codec_id = ff_codec_get_id(ff_codec_smjpeg_video_tags,
118  vst->codec->codec_tag);
119  vst->duration = duration;
120  sc->video_stream_index = vst->index;
121  avpriv_set_pts_info(vst, 32, 1, 1000);
122  avio_skip(pb, hlength - 12);
123  break;
124  case SMJPEG_HEND:
125  return 0;
126  default:
127  av_log(s, AV_LOG_ERROR, "unknown header %x\n", htype);
128  return AVERROR_INVALIDDATA;
129  }
130  }
131 
132  return AVERROR_EOF;
133 }
134 
136 {
137  SMJPEGContext *sc = s->priv_data;
138  uint32_t dtype, size, timestamp;
139  int64_t pos;
140  int ret;
141 
142  if (url_feof(s->pb))
143  return AVERROR_EOF;
144  pos = avio_tell(s->pb);
145  dtype = avio_rl32(s->pb);
146  switch (dtype) {
147  case SMJPEG_SNDD:
148  timestamp = avio_rb32(s->pb);
149  size = avio_rb32(s->pb);
150  ret = av_get_packet(s->pb, pkt, size);
151  pkt->stream_index = sc->audio_stream_index;
152  pkt->pts = timestamp;
153  pkt->pos = pos;
154  break;
155  case SMJPEG_VIDD:
156  timestamp = avio_rb32(s->pb);
157  size = avio_rb32(s->pb);
158  ret = av_get_packet(s->pb, pkt, size);
159  pkt->stream_index = sc->video_stream_index;
160  pkt->pts = timestamp;
161  pkt->pos = pos;
162  break;
163  case SMJPEG_DONE:
164  ret = AVERROR_EOF;
165  break;
166  default:
167  av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", dtype);
168  ret = AVERROR_INVALIDDATA;
169  break;
170  }
171  return ret;
172 }
173 
175  .name = "smjpeg",
176  .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
177  .priv_data_size = sizeof(SMJPEGContext),
181  .extensions = "mjpg",
183 };
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define SMJPEG_DONE
Definition: smjpeg.h:34
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
int64_t pos
byte position in stream, -1 if unknown
AVInputFormat ff_smjpeg_demuxer
Definition: smjpegdec.c:174
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
int index
stream index in AVFormatContext
Definition: avformat.h:644
#define SMJPEG_HEND
Definition: smjpeg.h:35
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
int version
Definition: avisynth_c.h:666
SMJPEG common code.
const AVCodecTag ff_codec_smjpeg_video_tags[]
Definition: smjpeg.c:31
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:595
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:944
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:610
static AVPacket pkt
Definition: demuxing.c:56
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
static int smjpeg_probe(AVProbeData *p)
Definition: smjpegdec.c:37
#define SMJPEG_SND
Definition: smjpeg.h:36
static int smjpeg_read_header(AVFormatContext *s)
Definition: smjpegdec.c:44
#define SMJPEG_VIDD
Definition: smjpeg.h:40
#define AVERROR_EOF
End of file.
Definition: error.h:55
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
static int64_t duration
Definition: ffplay.c:294
int video_stream_index
Definition: smjpegdec.c:34
#define SMJPEG_TXT
Definition: smjpeg.h:38
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
AVDictionary * metadata
Definition: avformat.h:1092
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
#define SMJPEG_MAGIC
Definition: smjpeg.h:32
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int size
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and chilren.
Definition: dict.h:72
static int read_probe(AVProbeData *pd)
ret
Definition: avfilter.c:821
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
struct SMJPEGContext SMJPEGContext
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
int audio_stream_index
Definition: smjpegdec.c:33
#define SMJPEG_VID
Definition: smjpeg.h:39
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:62
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:353
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
static int flags
Definition: cpu.c:23
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:696
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
Main libavformat public API header.
#define SMJPEG_SNDD
Definition: smjpeg.h:37
static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: smjpegdec.c:135
const AVCodecTag ff_codec_smjpeg_audio_tags[]
Definition: smjpeg.c:36
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...