smjpegenc.c
Go to the documentation of this file.
1 /*
2  * SMJPEG muxer
3  * Copyright (c) 2012 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 muxer for Loki SDL Motion JPEG files
25  */
26 
27 #include "avformat.h"
28 #include "internal.h"
29 #include "smjpeg.h"
30 
31 typedef struct SMJPEGMuxContext {
32  uint32_t duration;
34 
36 {
38  AVIOContext *pb = s->pb;
39  int n, tag;
40 
41  if (s->nb_streams > 2) {
42  av_log(s, AV_LOG_ERROR, "more than >2 streams are not supported\n");
43  return AVERROR(EINVAL);
44  }
45  avio_write(pb, SMJPEG_MAGIC, 8);
46  avio_wb32(pb, 0);
47  avio_wb32(pb, 0);
48 
49  while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
50  avio_wl32(pb, SMJPEG_TXT);
51  avio_wb32(pb, strlen(t->key) + strlen(t->value) + 3);
52  avio_write(pb, t->key, strlen(t->key));
53  avio_write(pb, " = ", 3);
54  avio_write(pb, t->value, strlen(t->value));
55  }
56 
57  for (n = 0; n < s->nb_streams; n++) {
58  AVStream *st = s->streams[n];
59  AVCodecContext *codec = st->codec;
60  if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
62  if (!tag) {
63  av_log(s, AV_LOG_ERROR, "unsupported audio codec\n");
64  return AVERROR(EINVAL);
65  }
66  avio_wl32(pb, SMJPEG_SND);
67  avio_wb32(pb, 8);
68  avio_wb16(pb, codec->sample_rate);
69  avio_w8(pb, codec->bits_per_coded_sample);
70  avio_w8(pb, codec->channels);
71  avio_wl32(pb, tag);
72  avpriv_set_pts_info(st, 32, 1, 1000);
73  } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
75  if (!tag) {
76  av_log(s, AV_LOG_ERROR, "unsupported video codec\n");
77  return AVERROR(EINVAL);
78  }
79  avio_wl32(pb, SMJPEG_VID);
80  avio_wb32(pb, 12);
81  avio_wb32(pb, 0);
82  avio_wb16(pb, codec->width);
83  avio_wb16(pb, codec->height);
84  avio_wl32(pb, tag);
85  avpriv_set_pts_info(st, 32, 1, 1000);
86  }
87  }
88 
90  avio_flush(pb);
91 
92  return 0;
93 }
94 
96 {
97  SMJPEGMuxContext *smc = s->priv_data;
98  AVIOContext *pb = s->pb;
99  AVStream *st = s->streams[pkt->stream_index];
100  AVCodecContext *codec = st->codec;
101 
102  if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
103  avio_wl32(pb, SMJPEG_SNDD);
104  else if (codec->codec_type == AVMEDIA_TYPE_VIDEO)
105  avio_wl32(pb, SMJPEG_VIDD);
106  else
107  return 0;
108 
109  avio_wb32(pb, pkt->pts);
110  avio_wb32(pb, pkt->size);
111  avio_write(pb, pkt->data, pkt->size);
112 
113  smc->duration = FFMAX(smc->duration, pkt->pts + pkt->duration);
114  return 0;
115 }
116 
118 {
119  SMJPEGMuxContext *smc = s->priv_data;
120  AVIOContext *pb = s->pb;
121  int64_t currentpos;
122 
123  if (pb->seekable) {
124  currentpos = avio_tell(pb);
125  avio_seek(pb, 12, SEEK_SET);
126  avio_wb32(pb, smc->duration);
127  avio_seek(pb, currentpos, SEEK_SET);
128  }
129 
130  avio_wl32(pb, SMJPEG_DONE);
131 
132  return 0;
133 }
134 
136  .name = "smjpeg",
137  .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
138  .priv_data_size = sizeof(SMJPEGMuxContext),
139  .audio_codec = AV_CODEC_ID_PCM_S16LE,
140  .video_codec = AV_CODEC_ID_MJPEG,
145  .codec_tag = (const AVCodecTag *const []){ ff_codec_smjpeg_video_tags, ff_codec_smjpeg_audio_tags, 0 },
146 };
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define SMJPEG_DONE
Definition: smjpeg.h:34
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.
static int write_packet(AVFormatContext *s, AVPacket *pkt)
static int smjpeg_write_trailer(AVFormatContext *s)
Definition: smjpegenc.c:117
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
#define SMJPEG_HEND
Definition: smjpeg.h:35
static int smjpeg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: smjpegenc.c:95
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
SMJPEG common code.
const AVCodecTag ff_codec_smjpeg_video_tags[]
Definition: smjpeg.c:31
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
struct SMJPEGMuxContext SMJPEGMuxContext
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:363
Format I/O context.
Definition: avformat.h:944
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:291
static AVPacket pkt
Definition: demuxing.c:56
AVStream ** streams
Definition: avformat.h:992
#define SMJPEG_SND
Definition: smjpeg.h:36
uint8_t * data
#define SMJPEG_VIDD
Definition: smjpeg.h:40
uint32_t tag
Definition: movenc.c:894
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).
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
#define SMJPEG_TXT
Definition: smjpeg.h:38
static int write_trailer(AVFormatContext *s)
static int smjpeg_write_header(AVFormatContext *s)
Definition: smjpegenc.c:35
AVDictionary * metadata
Definition: avformat.h:1092
#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
#define FFMAX(a, b)
Definition: common.h:56
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
int width
picture width / height.
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:351
t
Definition: genspecsines3.m:6
const char * name
Definition: avformat.h:378
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:151
main external API structure.
uint32_t duration
Definition: smjpegenc.c:32
#define SMJPEG_VID
Definition: smjpeg.h:39
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
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
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:373
static int flags
Definition: cpu.c:23
Main libavformat public API header.
#define SMJPEG_SNDD
Definition: smjpeg.h:37
char * key
Definition: dict.h:81
const AVCodecTag ff_codec_smjpeg_audio_tags[]
Definition: smjpeg.c:36
char * value
Definition: dict.h:82
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:470
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:299
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:68
AVOutputFormat ff_smjpeg_muxer
Definition: smjpegenc.c:135
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...