FFmpeg
rsoenc.c
Go to the documentation of this file.
1 /*
2  * RSO muxer
3  * Copyright (c) 2001 Fabrice Bellard (original AU code)
4  * Copyright (c) 2010 Rafael Carre
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "avformat.h"
24 #include "internal.h"
25 #include "riff.h"
26 #include "rso.h"
27 
29 {
30  AVIOContext *pb = s->pb;
31  AVCodecParameters *par = s->streams[0]->codecpar;
32 
33  if (!par->codec_tag)
34  return AVERROR_INVALIDDATA;
35 
36  if (par->channels != 1) {
37  av_log(s, AV_LOG_ERROR, "RSO only supports mono\n");
38  return AVERROR_INVALIDDATA;
39  }
40 
41  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
42  av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
43  return AVERROR_INVALIDDATA;
44  }
45 
46  /* XXX: find legal sample rates (if any) */
47  if (par->sample_rate >= 1u<<16) {
48  av_log(s, AV_LOG_ERROR, "Sample rate must be < 65536\n");
49  return AVERROR_INVALIDDATA;
50  }
51 
52  if (par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
53  avpriv_report_missing_feature(s, "ADPCM in RSO");
54  return AVERROR_PATCHWELCOME;
55  }
56 
57  /* format header */
58  avio_wb16(pb, par->codec_tag); /* codec ID */
59  avio_wb16(pb, 0); /* data size, will be written at EOF */
60  avio_wb16(pb, par->sample_rate);
61  avio_wb16(pb, 0x0000); /* play mode ? (0x0000 = don't loop) */
62 
63  avio_flush(pb);
64 
65  return 0;
66 }
67 
69 {
70  avio_write(s->pb, pkt->data, pkt->size);
71  return 0;
72 }
73 
75 {
76  AVIOContext *pb = s->pb;
77  int64_t file_size;
78  uint16_t coded_file_size;
79 
80  file_size = avio_tell(pb);
81 
82  if (file_size < 0)
83  return file_size;
84 
85  if (file_size > 0xffff + RSO_HEADER_SIZE) {
87  "Output file is too big (%"PRId64" bytes >= 64kB)\n", file_size);
88  coded_file_size = 0xffff;
89  } else {
90  coded_file_size = file_size - RSO_HEADER_SIZE;
91  }
92 
93  /* update file size */
94  avio_seek(pb, 2, SEEK_SET);
95  avio_wb16(pb, coded_file_size);
96  avio_seek(pb, file_size, SEEK_SET);
97 
98  return 0;
99 }
100 
102  .name = "rso",
103  .long_name = NULL_IF_CONFIG_SMALL("Lego Mindstorms RSO"),
104  .extensions = "rso",
105  .audio_codec = AV_CODEC_ID_PCM_U8,
106  .video_codec = AV_CODEC_ID_NONE,
107  .write_header = rso_write_header,
108  .write_packet = rso_write_packet,
109  .write_trailer = rso_write_trailer,
110  .codec_tag = (const AVCodecTag* const []){ff_codec_rso_tags, 0},
111  .flags = AVFMT_NOTIMESTAMPS,
112 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVOutputFormat::name
const char * name
Definition: avformat.h:496
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3949
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:252
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:467
ff_rso_muxer
AVOutputFormat ff_rso_muxer
Definition: rsoenc.c:101
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
rso.h
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3961
AVCodecParameters::channels
int channels
Audio only.
Definition: avcodec.h:4063
rso_write_header
static int rso_write_header(AVFormatContext *s)
Definition: rsoenc.c:28
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
RSO_HEADER_SIZE
#define RSO_HEADER_SIZE
Definition: rso.h:27
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
rso_write_packet
static int rso_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rsoenc.c:68
AVCodecTag
Definition: internal.h:44
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: avcodec.h:4067
ff_codec_rso_tags
const AVCodecTag ff_codec_rso_tags[]
Definition: rso.c:26
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: avcodec.h:1478
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:188
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: avcodec.h:216
AVOutputFormat
Definition: avformat.h:495
rso_write_trailer
static int rso_write_trailer(AVFormatContext *s)
Definition: rsoenc.c:74
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
avformat.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: avcodec.h:468
avio_flush
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:238
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
AV_CODEC_ID_ADPCM_IMA_WAV
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition: avcodec.h:503
riff.h
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:475
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59