FFmpeg
Loading...
Searching...
No Matches
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 "libavutil/mem.h"
30#include "avformat.h"
31#include "demux.h"
32#include "internal.h"
33#include "smjpeg.h"
34
39
40static int smjpeg_probe(const AVProbeData *p)
41{
42 if (!memcmp(p->buf, SMJPEG_MAGIC, 8))
43 return AVPROBE_SCORE_MAX;
44 return 0;
45}
46
48{
49 SMJPEGContext *sc = s->priv_data;
50 AVStream *ast = NULL, *vst = NULL;
51 AVIOContext *pb = s->pb;
52 uint32_t version, htype, hlength, duration;
53 char *comment;
54
56 sc->video_stream_index = -1;
57
58 avio_skip(pb, 8); // magic
59 version = avio_rb32(pb);
60 if (version)
61 avpriv_request_sample(s, "Unknown version %"PRIu32, version);
62
63 duration = avio_rb32(pb); // in msec
64
65 while (!avio_feof(pb)) {
66 htype = avio_rl32(pb);
67 switch (htype) {
68 case SMJPEG_TXT:
69 hlength = avio_rb32(pb);
70 if (!hlength || hlength > 512)
72 comment = av_malloc(hlength + 1);
73 if (!comment)
74 return AVERROR(ENOMEM);
75 if (avio_read(pb, comment, hlength) != hlength) {
77 av_log(s, AV_LOG_ERROR, "error when reading comment\n");
79 }
80 comment[hlength] = 0;
81 av_dict_set(&s->metadata, "comment", comment,
83 break;
84 case SMJPEG_SND:
85 if (ast) {
86 avpriv_request_sample(s, "Multiple audio streams");
88 }
89 hlength = avio_rb32(pb);
90 if (hlength < 8)
92 ast = avformat_new_stream(s, 0);
93 if (!ast)
94 return AVERROR(ENOMEM);
96 ast->codecpar->sample_rate = avio_rb16(pb);
99 ast->codecpar->codec_tag = avio_rl32(pb);
101 ast->codecpar->codec_tag);
102 ast->duration = duration;
103 sc->audio_stream_index = ast->index;
104 avpriv_set_pts_info(ast, 32, 1, 1000);
105 avio_skip(pb, hlength - 8);
106 break;
107 case SMJPEG_VID:
108 if (vst) {
109 avpriv_request_sample(s, "Multiple video streams");
110 return AVERROR_INVALIDDATA;
111 }
112 hlength = avio_rb32(pb);
113 if (hlength < 12)
114 return AVERROR_INVALIDDATA;
115 vst = avformat_new_stream(s, 0);
116 if (!vst)
117 return AVERROR(ENOMEM);
118 vst->nb_frames = avio_rb32(pb);
119 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
120 vst->codecpar->width = avio_rb16(pb);
121 vst->codecpar->height = avio_rb16(pb);
122 vst->codecpar->codec_tag = avio_rl32(pb);
123 vst->codecpar->codec_id = ff_codec_get_id(ff_codec_smjpeg_video_tags,
124 vst->codecpar->codec_tag);
125 vst->duration = duration;
126 sc->video_stream_index = vst->index;
127 avpriv_set_pts_info(vst, 32, 1, 1000);
128 avio_skip(pb, hlength - 12);
129 break;
130 case SMJPEG_HEND:
131 return 0;
132 default:
133 av_log(s, AV_LOG_ERROR, "unknown header %"PRIx32"\n", htype);
134 return AVERROR_INVALIDDATA;
135 }
136 }
137
138 return AVERROR_EOF;
139}
140
142{
143 SMJPEGContext *sc = s->priv_data;
144 uint32_t dtype, size, timestamp;
145 int64_t pos;
146 int ret;
147
148 if (avio_feof(s->pb))
149 return AVERROR_EOF;
150 pos = avio_tell(s->pb);
151 dtype = avio_rl32(s->pb);
152 switch (dtype) {
153 case SMJPEG_SNDD:
154 if (sc->audio_stream_index < 0)
155 return AVERROR_INVALIDDATA;
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->audio_stream_index;
160 pkt->pts = timestamp;
161 pkt->pos = pos;
162 break;
163 case SMJPEG_VIDD:
164 if (sc->video_stream_index < 0)
165 return AVERROR_INVALIDDATA;
166 timestamp = avio_rb32(s->pb);
167 size = avio_rb32(s->pb);
168 ret = av_get_packet(s->pb, pkt, size);
169 pkt->stream_index = sc->video_stream_index;
170 pkt->pts = timestamp;
171 pkt->pos = pos;
172 break;
173 case SMJPEG_DONE:
174 ret = AVERROR_EOF;
175 break;
176 default:
177 av_log(s, AV_LOG_ERROR, "unknown chunk %"PRIx32"\n", dtype);
179 break;
180 }
181 return ret;
182}
183
185 .p.name = "smjpeg",
186 .p.long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
187 .p.extensions = "mjpg",
188 .p.flags = AVFMT_GENERIC_INDEX,
189 .priv_data_size = sizeof(SMJPEGContext),
193};
const FFInputFormat ff_smjpeg_demuxer
Definition smjpegdec.c:184
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:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
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:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
unsigned int avio_rb16(AVIOContext *s)
Definition aviobuf.c:749
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static int FUNC comment(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawComment *current)
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int64_t duration
Definition ffplay.c:330
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#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
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:86
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition utils.c:143
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
version
Definition libkvazaar.c:313
#define htype
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
const AVCodecTag ff_codec_smjpeg_video_tags[]
Definition smjpeg.c:31
const AVCodecTag ff_codec_smjpeg_audio_tags[]
Definition smjpeg.c:36
SMJPEG common code.
#define SMJPEG_TXT
Definition smjpeg.h:38
#define SMJPEG_DONE
Definition smjpeg.h:34
#define SMJPEG_SND
Definition smjpeg.h:36
#define SMJPEG_SNDD
Definition smjpeg.h:37
#define SMJPEG_VIDD
Definition smjpeg.h:40
#define SMJPEG_VID
Definition smjpeg.h:39
#define SMJPEG_HEND
Definition smjpeg.h:35
#define SMJPEG_MAGIC
Definition smjpeg.h:32
static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition smjpegdec.c:141
static int smjpeg_probe(const AVProbeData *p)
Definition smjpegdec.c:40
static int smjpeg_read_header(AVFormatContext *s)
Definition smjpegdec.c:47
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int index
stream index in AVFormatContext
Definition avformat.h:772
int audio_stream_index
Definition smjpegdec.c:36
int video_stream_index
Definition smjpegdec.c:37
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
int size