FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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(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  avio_skip(pb, 8); // magic
55  version = avio_rb32(pb);
56  if (version)
57  avpriv_request_sample(s, "Unknown version %"PRIu32, version);
58 
59  duration = avio_rb32(pb); // in msec
60 
61  while (!avio_feof(pb)) {
62  htype = avio_rl32(pb);
63  switch (htype) {
64  case SMJPEG_TXT:
65  hlength = avio_rb32(pb);
66  if (!hlength || hlength > 512)
67  return AVERROR_INVALIDDATA;
68  comment = av_malloc(hlength + 1);
69  if (!comment)
70  return AVERROR(ENOMEM);
71  if (avio_read(pb, comment, hlength) != hlength) {
72  av_freep(&comment);
73  av_log(s, AV_LOG_ERROR, "error when reading comment\n");
74  return AVERROR_INVALIDDATA;
75  }
76  comment[hlength] = 0;
77  av_dict_set(&s->metadata, "comment", comment,
79  break;
80  case SMJPEG_SND:
81  if (ast) {
82  avpriv_request_sample(s, "Multiple audio streams");
83  return AVERROR_PATCHWELCOME;
84  }
85  hlength = avio_rb32(pb);
86  if (hlength < 8)
87  return AVERROR_INVALIDDATA;
88  ast = avformat_new_stream(s, 0);
89  if (!ast)
90  return AVERROR(ENOMEM);
92  ast->codecpar->sample_rate = avio_rb16(pb);
94  ast->codecpar->channels = avio_r8(pb);
95  ast->codecpar->codec_tag = avio_rl32(pb);
97  ast->codecpar->codec_tag);
98  ast->duration = duration;
99  sc->audio_stream_index = ast->index;
100  avpriv_set_pts_info(ast, 32, 1, 1000);
101  avio_skip(pb, hlength - 8);
102  break;
103  case SMJPEG_VID:
104  if (vst) {
105  avpriv_request_sample(s, "Multiple video streams");
106  return AVERROR_INVALIDDATA;
107  }
108  hlength = avio_rb32(pb);
109  if (hlength < 12)
110  return AVERROR_INVALIDDATA;
111  vst = avformat_new_stream(s, 0);
112  if (!vst)
113  return AVERROR(ENOMEM);
114  vst->nb_frames = avio_rb32(pb);
115  vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
116  vst->codecpar->width = avio_rb16(pb);
117  vst->codecpar->height = avio_rb16(pb);
118  vst->codecpar->codec_tag = avio_rl32(pb);
119  vst->codecpar->codec_id = ff_codec_get_id(ff_codec_smjpeg_video_tags,
120  vst->codecpar->codec_tag);
121  vst->duration = duration;
122  sc->video_stream_index = vst->index;
123  avpriv_set_pts_info(vst, 32, 1, 1000);
124  avio_skip(pb, hlength - 12);
125  break;
126  case SMJPEG_HEND:
127  return 0;
128  default:
129  av_log(s, AV_LOG_ERROR, "unknown header %"PRIx32"\n", htype);
130  return AVERROR_INVALIDDATA;
131  }
132  }
133 
134  return AVERROR_EOF;
135 }
136 
138 {
139  SMJPEGContext *sc = s->priv_data;
140  uint32_t dtype, size, timestamp;
141  int64_t pos;
142  int ret;
143 
144  if (avio_feof(s->pb))
145  return AVERROR_EOF;
146  pos = avio_tell(s->pb);
147  dtype = avio_rl32(s->pb);
148  switch (dtype) {
149  case SMJPEG_SNDD:
150  timestamp = avio_rb32(s->pb);
151  size = avio_rb32(s->pb);
152  ret = av_get_packet(s->pb, pkt, size);
153  pkt->stream_index = sc->audio_stream_index;
154  pkt->pts = timestamp;
155  pkt->pos = pos;
156  break;
157  case SMJPEG_VIDD:
158  timestamp = avio_rb32(s->pb);
159  size = avio_rb32(s->pb);
160  ret = av_get_packet(s->pb, pkt, size);
161  pkt->stream_index = sc->video_stream_index;
162  pkt->pts = timestamp;
163  pkt->pos = pos;
164  break;
165  case SMJPEG_DONE:
166  ret = AVERROR_EOF;
167  break;
168  default:
169  av_log(s, AV_LOG_ERROR, "unknown chunk %"PRIx32"\n", dtype);
170  ret = AVERROR_INVALIDDATA;
171  break;
172  }
173  return ret;
174 }
175 
177  .name = "smjpeg",
178  .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
179  .priv_data_size = sizeof(SMJPEGContext),
183  .extensions = "mjpg",
185 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:155
#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)
Definition: utils.c:3050
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1677
AVInputFormat ff_smjpeg_demuxer
Definition: smjpegdec.c:176
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.
Definition: utils.c:4601
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4066
int index
stream index in AVFormatContext
Definition: avformat.h:890
#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:316
int version
Definition: avisynth_c.h:766
SMJPEG common code.
static AVPacket pkt
const AVCodecTag ff_codec_smjpeg_video_tags[]
Definition: smjpeg.c:31
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:754
Format I/O context.
Definition: avformat.h:1349
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define av_malloc(s)
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:769
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4231
static int smjpeg_probe(AVProbeData *p)
Definition: smjpegdec.c:39
int64_t duration
Definition: movenc.c:63
#define SMJPEG_SND
Definition: smjpeg.h:36
static int smjpeg_read_header(AVFormatContext *s)
Definition: smjpegdec.c:46
static int flags
Definition: log.c:57
#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.
Definition: utils.c:289
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:525
int video_stream_index
Definition: smjpegdec.c:36
#define av_log(a,...)
#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:616
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1567
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#define AVERROR(e)
Definition: error.h:43
#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. ...
Definition: internal.h:179
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4062
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:607
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
#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:76
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:510
Stream structure.
Definition: avformat.h:889
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
int audio_stream_index
Definition: smjpegdec.c:35
#define SMJPEG_VID
Definition: smjpeg.h:39
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:70
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:487
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:946
int sample_rate
Audio only.
Definition: avcodec.h:4176
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
Main libavformat public API header.
#define SMJPEG_SNDD
Definition: smjpeg.h:37
static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: smjpegdec.c:137
const AVCodecTag ff_codec_smjpeg_audio_tags[]
Definition: smjpeg.c:36
void * priv_data
Format private data.
Definition: avformat.h:1377
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4108
int channels
Audio only.
Definition: avcodec.h:4172
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:340
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4070
int stream_index
Definition: avcodec.h:1659
This structure stores compressed data.
Definition: avcodec.h:1634
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1650