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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avformat.h"
28 #include "internal.h"
29 #include "riff.h"
30 #include "smjpeg.h"
31 
32 typedef struct SMJPEGMuxContext {
33  uint32_t duration;
35 
37 {
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 
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  AVCodecContext *codec = st->codec;
61  if (codec->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, codec->sample_rate);
71  avio_w8(pb, codec->channels);
72  avio_wl32(pb, tag);
73  avpriv_set_pts_info(st, 32, 1, 1000);
74  } else if (codec->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, codec->width);
84  avio_wb16(pb, codec->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  AVCodecContext *codec = st->codec;
102 
103  if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
104  avio_wl32(pb, SMJPEG_SNDD);
105  else if (codec->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  avio_flush(pb);
114 
115  smc->duration = FFMAX(smc->duration, pkt->pts + pkt->duration);
116  return 0;
117 }
118 
120 {
121  SMJPEGMuxContext *smc = s->priv_data;
122  AVIOContext *pb = s->pb;
123  int64_t currentpos;
124 
125  if (pb->seekable) {
126  currentpos = avio_tell(pb);
127  avio_seek(pb, 12, SEEK_SET);
128  avio_wb32(pb, smc->duration);
129  avio_seek(pb, currentpos, SEEK_SET);
130  }
131 
132  avio_wl32(pb, SMJPEG_DONE);
133  avio_flush(pb);
134 
135  return 0;
136 }
137 
139  .name = "smjpeg",
140  .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
141  .priv_data_size = sizeof(SMJPEGMuxContext),
142  .audio_codec = CODEC_ID_PCM_S16LE,
143  .video_codec = CODEC_ID_MJPEG,
148  .codec_tag = (const AVCodecTag *const []){ ff_codec_smjpeg_video_tags, ff_codec_smjpeg_audio_tags, 0 },
149 };
static int write_header(AVFormatContext *s)
Definition: assenc.c:28
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 pts for a given stream.
Definition: utils.c:3828
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
static int smjpeg_write_trailer(AVFormatContext *s)
Definition: smjpegenc.c:119
int size
Definition: avcodec.h:909
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:211
#define SMJPEG_HEND
Definition: smjpeg.h:35
static int smjpeg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: smjpegenc.c:96
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:27
struct SMJPEGMuxContext SMJPEGMuxContext
Format I/O context.
Definition: avformat.h:863
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:316
AVStream ** streams
Definition: avformat.h:908
#define SMJPEG_SND
Definition: smjpeg.h:36
uint8_t * data
Definition: avcodec.h:908
static int flags
Definition: log.c:34
#define SMJPEG_VIDD
Definition: smjpeg.h:40
uint32_t tag
Definition: movenc.c:670
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:190
int av_get_bits_per_sample(enum CodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1634
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:930
#define SMJPEG_TXT
Definition: smjpeg.h:38
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:67
static float t
static int smjpeg_write_header(AVFormatContext *s)
Definition: smjpegenc.c:36
AVDictionary * metadata
Definition: avformat.h:1085
#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:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
#define FFMAX(a, b)
Definition: common.h:53
AVCodecContext * codec
codec context
Definition: avformat.h:623
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:907
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:122
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:205
int width
picture width / height.
Definition: avcodec.h:1408
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:374
const char * name
Definition: avformat.h:390
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:169
main external API structure.
Definition: avcodec.h:1329
uint32_t duration
Definition: smjpegenc.c:33
#define SMJPEG_VID
Definition: smjpeg.h:39
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:533
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum CodecID id)
Definition: utils.c:2196
Main libavformat public API header.
#define SMJPEG_SNDD
Definition: smjpeg.h:37
char * key
Definition: dict.h:75
const AVCodecTag ff_codec_smjpeg_audio_tags[]
Definition: smjpeg.c:36
char * value
Definition: dict.h:76
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:324
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:62
int stream_index
Definition: avcodec.h:910
AVOutputFormat ff_smjpeg_muxer
Definition: smjpegenc.c:138
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
enum CodecID codec_id
Definition: avcodec.h:1575