FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
50  while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
51  avio_wl32(pb, SMJPEG_TXT);
52  avio_wb32(pb, strlen(t->key) + strlen(t->value) + 3);
53  avio_write(pb, t->key, strlen(t->key));
54  avio_write(pb, " = ", 3);
55  avio_write(pb, t->value, strlen(t->value));
56  }
57 
58  for (n = 0; n < s->nb_streams; n++) {
59  AVStream *st = s->streams[n];
60  AVCodecParameters *par = st->codecpar;
61  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
63  if (!tag) {
64  av_log(s, AV_LOG_ERROR, "unsupported audio codec\n");
65  return AVERROR(EINVAL);
66  }
67  avio_wl32(pb, SMJPEG_SND);
68  avio_wb32(pb, 8);
69  avio_wb16(pb, par->sample_rate);
71  avio_w8(pb, par->channels);
72  avio_wl32(pb, tag);
73  avpriv_set_pts_info(st, 32, 1, 1000);
74  } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
76  if (!tag) {
77  av_log(s, AV_LOG_ERROR, "unsupported video codec\n");
78  return AVERROR(EINVAL);
79  }
80  avio_wl32(pb, SMJPEG_VID);
81  avio_wb32(pb, 12);
82  avio_wb32(pb, 0);
83  avio_wb16(pb, par->width);
84  avio_wb16(pb, par->height);
85  avio_wl32(pb, tag);
86  avpriv_set_pts_info(st, 32, 1, 1000);
87  }
88  }
89 
91  avio_flush(pb);
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) {
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  .name = "smjpeg",
138  .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
139  .priv_data_size = sizeof(SMJPEGMuxContext),
140  .audio_codec = AV_CODEC_ID_PCM_S16LE,
141  .video_codec = AV_CODEC_ID_MJPEG,
146  .codec_tag = (const AVCodecTag *const []){ ff_codec_smjpeg_video_tags, ff_codec_smjpeg_audio_tags, 0 },
147 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:147
#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.
Definition: utils.c:4493
static int smjpeg_write_trailer(AVFormatContext *s)
Definition: smjpegenc.c:118
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3939
int size
Definition: avcodec.h:1589
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
#define SMJPEG_HEND
Definition: smjpeg.h:35
static int smjpeg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: smjpegenc.c:96
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2945
SMJPEG common code.
static AVPacket pkt
const AVCodecTag ff_codec_smjpeg_video_tags[]
Definition: smjpeg.c:31
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:494
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3931
Format I/O context.
Definition: avformat.h:1325
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:346
int width
Video only.
Definition: avcodec.h:4005
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1606
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1393
#define SMJPEG_SND
Definition: smjpeg.h:36
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
uint8_t * data
Definition: avcodec.h:1588
#define SMJPEG_VIDD
Definition: smjpeg.h:40
uint32_t tag
Definition: movenc.c:1376
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:511
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
#define av_log(a,...)
#define SMJPEG_TXT
Definition: smjpeg.h:38
static int smjpeg_write_header(AVFormatContext *s)
Definition: smjpegenc.c:35
#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:1539
#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:176
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3935
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: utils.c:5206
#define FFMAX(a, b)
Definition: common.h:94
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1381
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:243
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:224
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:483
const char * name
Definition: avformat.h:522
int n
Definition: avisynth_c.h:547
Stream structure.
Definition: avformat.h:876
AVIOContext * pb
I/O context.
Definition: avformat.h:1367
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:182
uint32_t duration
Definition: smjpegenc.c:32
#define SMJPEG_VID
Definition: smjpeg.h:39
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:452
static int flags
Definition: cpu.c:47
int sample_rate
Audio only.
Definition: avcodec.h:4049
Main libavformat public API header.
#define SMJPEG_SNDD
Definition: smjpeg.h:37
char * key
Definition: dict.h:86
const AVCodecTag ff_codec_smjpeg_audio_tags[]
Definition: smjpeg.c:36
char * value
Definition: dict.h:87
void * priv_data
Format private data.
Definition: avformat.h:1353
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:498
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3981
int channels
Audio only.
Definition: avcodec.h:4045
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:354
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
AVCodecParameters * codecpar
Definition: avformat.h:1228
int stream_index
Definition: avcodec.h:1590
AVOutputFormat ff_smjpeg_muxer
Definition: smjpegenc.c:136
This structure stores compressed data.
Definition: avcodec.h:1565
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1581