FFmpeg
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 "mux.h"
30 #include "smjpeg.h"
31 
32 typedef struct SMJPEGMuxContext {
33  uint32_t duration;
35 
37 {
38  const AVDictionaryEntry *t = NULL;
39  AVIOContext *pb = s->pb;
40  int n, tag;
41 
42  if (s->nb_streams > 2) {
43  av_log(s, AV_LOG_ERROR, "more than >2 streams are not supported\n");
44  return AVERROR(EINVAL);
45  }
46  avio_write(pb, SMJPEG_MAGIC, 8);
47  avio_wb32(pb, 0);
48  avio_wb32(pb, 0);
49 
51  while ((t = av_dict_iterate(s->metadata, t))) {
52  avio_wl32(pb, SMJPEG_TXT);
53  avio_wb32(pb, strlen(t->key) + strlen(t->value) + 3);
54  avio_write(pb, t->key, strlen(t->key));
55  avio_write(pb, " = ", 3);
56  avio_write(pb, t->value, strlen(t->value));
57  }
58 
59  for (n = 0; n < s->nb_streams; n++) {
60  AVStream *st = s->streams[n];
61  AVCodecParameters *par = st->codecpar;
62  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
64  if (!tag) {
65  av_log(s, AV_LOG_ERROR, "unsupported audio codec\n");
66  return AVERROR(EINVAL);
67  }
68  avio_wl32(pb, SMJPEG_SND);
69  avio_wb32(pb, 8);
70  avio_wb16(pb, par->sample_rate);
72  avio_w8(pb, par->ch_layout.nb_channels);
73  avio_wl32(pb, tag);
74  avpriv_set_pts_info(st, 32, 1, 1000);
75  } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
77  if (!tag) {
78  av_log(s, AV_LOG_ERROR, "unsupported video codec\n");
79  return AVERROR(EINVAL);
80  }
81  avio_wl32(pb, SMJPEG_VID);
82  avio_wb32(pb, 12);
83  avio_wb32(pb, 0);
84  avio_wb16(pb, par->width);
85  avio_wb16(pb, par->height);
86  avio_wl32(pb, tag);
87  avpriv_set_pts_info(st, 32, 1, 1000);
88  }
89  }
90 
92 
93  return 0;
94 }
95 
97 {
98  SMJPEGMuxContext *smc = s->priv_data;
99  AVIOContext *pb = s->pb;
100  AVStream *st = s->streams[pkt->stream_index];
101  AVCodecParameters *par = st->codecpar;
102 
103  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
104  avio_wl32(pb, SMJPEG_SNDD);
105  else if (par->codec_type == AVMEDIA_TYPE_VIDEO)
106  avio_wl32(pb, SMJPEG_VIDD);
107  else
108  return 0;
109 
110  avio_wb32(pb, pkt->pts);
111  avio_wb32(pb, pkt->size);
112  avio_write(pb, pkt->data, pkt->size);
113 
114  smc->duration = FFMAX(smc->duration, pkt->pts + pkt->duration);
115  return 0;
116 }
117 
119 {
120  SMJPEGMuxContext *smc = s->priv_data;
121  AVIOContext *pb = s->pb;
122  int64_t currentpos;
123 
124  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
125  currentpos = avio_tell(pb);
126  avio_seek(pb, 12, SEEK_SET);
127  avio_wb32(pb, smc->duration);
128  avio_seek(pb, currentpos, SEEK_SET);
129  }
130 
131  avio_wl32(pb, SMJPEG_DONE);
132 
133  return 0;
134 }
135 
137  .p.name = "smjpeg",
138  .p.long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
139  .priv_data_size = sizeof(SMJPEGMuxContext),
140  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
141  .p.video_codec = AV_CODEC_ID_MJPEG,
142  .write_header = smjpeg_write_header,
143  .write_packet = smjpeg_write_packet,
144  .write_trailer = smjpeg_write_trailer,
146  .p.codec_tag = (const AVCodecTag *const []){ ff_codec_smjpeg_video_tags, ff_codec_smjpeg_audio_tags, 0 },
147 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:326
AVOutputFormat::name
const char * name
Definition: avformat.h:508
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:58
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:54
SMJPEG_SNDD
#define SMJPEG_SNDD
Definition: smjpeg.h:37
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:34
SMJPEG_VID
#define SMJPEG_VID
Definition: smjpeg.h:39
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:771
SMJPEG_MAGIC
#define SMJPEG_MAGIC
Definition: smjpeg.h:32
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:500
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
ff_smjpeg_muxer
const FFOutputFormat ff_smjpeg_muxer
Definition: smjpegenc.c:136
AVCodecTag
Definition: internal.h:49
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVDictionaryEntry::key
char * key
Definition: dict.h:90
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:128
SMJPEGMuxContext
Definition: smjpegenc.c:32
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:861
NULL
#define NULL
Definition: coverity.c:32
FFOutputFormat
Definition: mux.h:30
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:200
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:213
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:178
ff_codec_smjpeg_video_tags
const AVCodecTag ff_codec_smjpeg_video_tags[]
Definition: smjpeg.c:31
smjpeg_write_packet
static int smjpeg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: smjpegenc.c:96
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
AVPacket::size
int size
Definition: packet.h:375
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:115
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:267
ff_standardize_creation_time
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: mux_utils.c:143
smjpeg.h
SMJPEG_DONE
#define SMJPEG_DONE
Definition: smjpeg.h:34
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:222
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:386
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:378
SMJPEG_VIDD
#define SMJPEG_VIDD
Definition: smjpeg.h:40
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:478
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
AVCodecParameters::height
int height
Definition: codec_par.h:129
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:489
SMJPEG_SND
#define SMJPEG_SND
Definition: smjpeg.h:36
tag
uint32_t tag
Definition: movenc.c:1641
AVStream
Stream structure.
Definition: avformat.h:838
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:252
smjpeg_write_header
static int smjpeg_write_header(AVFormatContext *s)
Definition: smjpegenc.c:36
avformat.h
SMJPEG_TXT
#define SMJPEG_TXT
Definition: smjpeg.h:38
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
ff_codec_get_tag
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:134
SMJPEGMuxContext::duration
uint32_t duration
Definition: smjpegenc.c:33
AVPacket::stream_index
int stream_index
Definition: packet.h:376
SMJPEG_HEND
#define SMJPEG_HEND
Definition: smjpeg.h:35
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:104
AVDictionaryEntry
Definition: dict.h:89
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
smjpeg_write_trailer
static int smjpeg_write_trailer(AVFormatContext *s)
Definition: smjpegenc.c:118
AVPacket
This structure stores compressed data.
Definition: packet.h:351
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:464
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVDictionaryEntry::value
char * value
Definition: dict.h:91
av_dict_iterate
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition: dict.c:42
mux.h