FFmpeg
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 <inttypes.h>
28 
29 #include "avformat.h"
30 #include "internal.h"
31 #include "riff.h"
32 #include "smjpeg.h"
33 
34 typedef struct SMJPEGContext {
38 
39 static int smjpeg_probe(const AVProbeData *p)
40 {
41  if (!memcmp(p->buf, SMJPEG_MAGIC, 8))
42  return AVPROBE_SCORE_MAX;
43  return 0;
44 }
45 
47 {
48  SMJPEGContext *sc = s->priv_data;
49  AVStream *ast = NULL, *vst = NULL;
50  AVIOContext *pb = s->pb;
51  uint32_t version, htype, hlength, duration;
52  char *comment;
53 
54  sc->audio_stream_index =
55  sc->video_stream_index = -1;
56 
57  avio_skip(pb, 8); // magic
58  version = avio_rb32(pb);
59  if (version)
60  avpriv_request_sample(s, "Unknown version %"PRIu32, version);
61 
62  duration = avio_rb32(pb); // in msec
63 
64  while (!avio_feof(pb)) {
65  htype = avio_rl32(pb);
66  switch (htype) {
67  case SMJPEG_TXT:
68  hlength = avio_rb32(pb);
69  if (!hlength || hlength > 512)
70  return AVERROR_INVALIDDATA;
71  comment = av_malloc(hlength + 1);
72  if (!comment)
73  return AVERROR(ENOMEM);
74  if (avio_read(pb, comment, hlength) != hlength) {
75  av_freep(&comment);
76  av_log(s, AV_LOG_ERROR, "error when reading comment\n");
77  return AVERROR_INVALIDDATA;
78  }
79  comment[hlength] = 0;
80  av_dict_set(&s->metadata, "comment", comment,
82  break;
83  case SMJPEG_SND:
84  if (ast) {
85  avpriv_request_sample(s, "Multiple audio streams");
86  return AVERROR_PATCHWELCOME;
87  }
88  hlength = avio_rb32(pb);
89  if (hlength < 8)
90  return AVERROR_INVALIDDATA;
91  ast = avformat_new_stream(s, 0);
92  if (!ast)
93  return AVERROR(ENOMEM);
95  ast->codecpar->sample_rate = avio_rb16(pb);
98  ast->codecpar->codec_tag = avio_rl32(pb);
100  ast->codecpar->codec_tag);
101  ast->duration = duration;
102  sc->audio_stream_index = ast->index;
103  avpriv_set_pts_info(ast, 32, 1, 1000);
104  avio_skip(pb, hlength - 8);
105  break;
106  case SMJPEG_VID:
107  if (vst) {
108  avpriv_request_sample(s, "Multiple video streams");
109  return AVERROR_INVALIDDATA;
110  }
111  hlength = avio_rb32(pb);
112  if (hlength < 12)
113  return AVERROR_INVALIDDATA;
114  vst = avformat_new_stream(s, 0);
115  if (!vst)
116  return AVERROR(ENOMEM);
117  vst->nb_frames = avio_rb32(pb);
118  vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
119  vst->codecpar->width = avio_rb16(pb);
120  vst->codecpar->height = avio_rb16(pb);
121  vst->codecpar->codec_tag = avio_rl32(pb);
122  vst->codecpar->codec_id = ff_codec_get_id(ff_codec_smjpeg_video_tags,
123  vst->codecpar->codec_tag);
124  vst->duration = duration;
125  sc->video_stream_index = vst->index;
126  avpriv_set_pts_info(vst, 32, 1, 1000);
127  avio_skip(pb, hlength - 12);
128  break;
129  case SMJPEG_HEND:
130  return 0;
131  default:
132  av_log(s, AV_LOG_ERROR, "unknown header %"PRIx32"\n", htype);
133  return AVERROR_INVALIDDATA;
134  }
135  }
136 
137  return AVERROR_EOF;
138 }
139 
141 {
142  SMJPEGContext *sc = s->priv_data;
143  uint32_t dtype, size, timestamp;
144  int64_t pos;
145  int ret;
146 
147  if (avio_feof(s->pb))
148  return AVERROR_EOF;
149  pos = avio_tell(s->pb);
150  dtype = avio_rl32(s->pb);
151  switch (dtype) {
152  case SMJPEG_SNDD:
153  if (sc->audio_stream_index < 0)
154  return AVERROR_INVALIDDATA;
155  timestamp = avio_rb32(s->pb);
156  size = avio_rb32(s->pb);
157  ret = av_get_packet(s->pb, pkt, size);
159  pkt->pts = timestamp;
160  pkt->pos = pos;
161  break;
162  case SMJPEG_VIDD:
163  if (sc->video_stream_index < 0)
164  return AVERROR_INVALIDDATA;
165  timestamp = avio_rb32(s->pb);
166  size = avio_rb32(s->pb);
167  ret = av_get_packet(s->pb, pkt, size);
169  pkt->pts = timestamp;
170  pkt->pos = pos;
171  break;
172  case SMJPEG_DONE:
173  ret = AVERROR_EOF;
174  break;
175  default:
176  av_log(s, AV_LOG_ERROR, "unknown chunk %"PRIx32"\n", dtype);
178  break;
179  }
180  return ret;
181 }
182 
184  .name = "smjpeg",
185  .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
186  .priv_data_size = sizeof(SMJPEGContext),
190  .extensions = "mjpg",
192 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
htype
#define htype
Definition: median_template.c:36
SMJPEG_SNDD
#define SMJPEG_SNDD
Definition: smjpeg.h:37
SMJPEGContext::audio_stream_index
int audio_stream_index
Definition: smjpegdec.c:35
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
SMJPEG_VID
#define SMJPEG_VID
Definition: smjpeg.h:39
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:464
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:761
SMJPEG_MAGIC
#define SMJPEG_MAGIC
Definition: smjpeg.h:32
smjpeg_read_packet
static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: smjpegdec.c:140
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:513
smjpeg_read_header
static int smjpeg_read_header(AVFormatContext *s)
Definition: smjpegdec.c:46
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:481
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:900
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:808
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:549
duration
int64_t duration
Definition: movenc.c:64
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:554
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:454
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVFormatContext
Format I/O context.
Definition: avformat.h:1115
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
SMJPEGContext::video_stream_index
int video_stream_index
Definition: smjpegdec.c:36
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:452
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:206
ff_codec_smjpeg_audio_tags
const AVCodecTag ff_codec_smjpeg_audio_tags[]
Definition: smjpeg.c:36
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:171
ff_codec_smjpeg_video_tags
const AVCodecTag ff_codec_smjpeg_video_tags[]
Definition: smjpeg.c:31
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:777
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
ff_codec_get_id
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:145
smjpeg.h
SMJPEG_DONE
#define SMJPEG_DONE
Definition: smjpeg.h:34
size
int size
Definition: twinvq_data.h:10344
SMJPEGContext
Definition: smjpegdec.c:34
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:650
version
version
Definition: libkvazaar.c:321
SMJPEG_VIDD
#define SMJPEG_VIDD
Definition: smjpeg.h:40
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:484
av_get_packet
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.
Definition: utils.c:103
SMJPEG_SND
#define SMJPEG_SND
Definition: smjpeg.h:36
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:841
comment
static int FUNC() comment(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawComment *current)
Definition: cbs_jpeg_syntax_template.c:174
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:793
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
SMJPEG_TXT
#define SMJPEG_TXT
Definition: smjpeg.h:38
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:847
smjpeg_probe
static int smjpeg_probe(const AVProbeData *p)
Definition: smjpegdec.c:39
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:659
AVPacket::stream_index
int stream_index
Definition: packet.h:493
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:365
SMJPEG_HEND
#define SMJPEG_HEND
Definition: smjpeg.h:35
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:29
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:97
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:468
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
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:88
riff.h
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:511
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_smjpeg_demuxer
const AVInputFormat ff_smjpeg_demuxer
Definition: smjpegdec.c:183
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:393