FFmpeg
Loading...
Searching...
No Matches
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
32typedef struct SMJPEGMuxContext {
33 uint32_t duration;
35
37{
38 const AVDictionaryEntry *t = NULL;
39 AVIOContext *pb = s->pb;
40 int n, tag;
41
43 avio_wb32(pb, 0);
44 avio_wb32(pb, 0);
45
47 while ((t = av_dict_iterate(s->metadata, t))) {
49 avio_wb32(pb, strlen(t->key) + strlen(t->value) + 3);
50 avio_write(pb, t->key, strlen(t->key));
51 avio_write(pb, " = ", 3);
52 avio_write(pb, t->value, strlen(t->value));
53 }
54
55 for (n = 0; n < s->nb_streams; n++) {
56 AVStream *st = s->streams[n];
57 AVCodecParameters *par = st->codecpar;
58 if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
60 if (!tag) {
61 av_log(s, AV_LOG_ERROR, "unsupported audio codec\n");
62 return AVERROR(EINVAL);
63 }
65 avio_wb32(pb, 8);
66 avio_wb16(pb, par->sample_rate);
69 avio_wl32(pb, tag);
70 avpriv_set_pts_info(st, 32, 1, 1000);
71 } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
73 if (!tag) {
74 av_log(s, AV_LOG_ERROR, "unsupported video codec\n");
75 return AVERROR(EINVAL);
76 }
78 avio_wb32(pb, 12);
79 avio_wb32(pb, 0);
80 avio_wb16(pb, par->width);
81 avio_wb16(pb, par->height);
82 avio_wl32(pb, tag);
83 avpriv_set_pts_info(st, 32, 1, 1000);
84 }
85 }
86
88
89 return 0;
90}
91
93{
94 SMJPEGMuxContext *smc = s->priv_data;
95 AVIOContext *pb = s->pb;
96 AVStream *st = s->streams[pkt->stream_index];
97 AVCodecParameters *par = st->codecpar;
98
101 else if (par->codec_type == AVMEDIA_TYPE_VIDEO)
103 else
104 return 0;
105
106 avio_wb32(pb, pkt->pts);
107 avio_wb32(pb, pkt->size);
108 avio_write(pb, pkt->data, pkt->size);
109
110 smc->duration = FFMAX(smc->duration, pkt->pts + pkt->duration);
111 return 0;
112}
113
115{
116 SMJPEGMuxContext *smc = s->priv_data;
117 AVIOContext *pb = s->pb;
118 int64_t currentpos;
119
120 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
121 currentpos = avio_tell(pb);
122 avio_seek(pb, 12, SEEK_SET);
123 avio_wb32(pb, smc->duration);
124 avio_seek(pb, currentpos, SEEK_SET);
125 }
126
128
129 return 0;
130}
131
133 .p.name = "smjpeg",
134 .p.long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
135 .priv_data_size = sizeof(SMJPEGMuxContext),
136 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
137 .p.video_codec = AV_CODEC_ID_MJPEG,
138 .p.subtitle_codec = AV_CODEC_ID_NONE,
139 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
140 .write_header = smjpeg_write_header,
141 .write_packet = smjpeg_write_packet,
142 .write_trailer = smjpeg_write_trailer,
144 .p.codec_tag = (const AVCodecTag *const []){ ff_codec_smjpeg_video_tags, ff_codec_smjpeg_audio_tags, 0 },
145};
const FFOutputFormat ff_smjpeg_muxer
Definition smjpegenc.c:132
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 AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition avformat.h:507
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition avformat.h:497
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
void avio_wl32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:360
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_MJPEG
Definition codec_id.h:57
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:42
#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
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition utils.c:133
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMAX(a, b)
Definition macros.h:47
uint32_t tag
Definition movenc.c:2073
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition mux_utils.c:154
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_write_header(AVFormatContext *s)
Definition smjpegenc.c:36
static int smjpeg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition smjpegenc.c:92
static int smjpeg_write_trailer(AVFormatContext *s)
Definition smjpegenc.c:114
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int height
The height of the video frame in pixels.
Definition codec_par.h:150
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
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
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
char * key
Definition dict.h:91
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
uint32_t duration
Definition smjpegenc.c:33
#define av_log(a,...)