Browse code

Lego Mindstorms RSO muxer and demuxer.

Patch by Rafaël Carré, rafael d carre a gmail

Originally committed as revision 24355 to svn://svn.ffmpeg.org/ffmpeg/trunk

Rafaël Carré authored on 2010/07/20 23:13:24
Showing 10 changed files
... ...
@@ -22,6 +22,7 @@ version <next>:
22 22
 - native GSM / GSM MS decoder
23 23
 - RTP depacketization of QDM2
24 24
 - ANSI/ASCII art playback system
25
+- Lego Mindstorms RSO de/muxer
25 26
 
26 27
 
27 28
 
... ...
@@ -209,6 +209,7 @@ library:
209 209
 @item RL2                       @tab   @tab X
210 210
     @tab Audio and video format used in some games by Entertainment Software Partners.
211 211
 @item RPL/ARMovie               @tab   @tab X
212
+@item Lego Mindstorms RSO       @tab X @tab X
212 213
 @item RTMP                      @tab X @tab X
213 214
     @tab Output is performed by publishing stream to RTMP server
214 215
 @item RTP                       @tab   @tab X
... ...
@@ -209,6 +209,8 @@ OBJS-$(CONFIG_RM_DEMUXER)                += rmdec.o rm.o
209 209
 OBJS-$(CONFIG_RM_MUXER)                  += rmenc.o rm.o
210 210
 OBJS-$(CONFIG_ROQ_DEMUXER)               += idroq.o
211 211
 OBJS-$(CONFIG_ROQ_MUXER)                 += raw.o
212
+OBJS-$(CONFIG_RSO_DEMUXER)               += rsodec.o rso.o raw.o
213
+OBJS-$(CONFIG_RSO_MUXER)                 += rsoenc.o rso.o
212 214
 OBJS-$(CONFIG_RPL_DEMUXER)               += rpl.o
213 215
 OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
214 216
                                             rtpenc_aac.o     \
... ...
@@ -174,6 +174,7 @@ void av_register_all(void)
174 174
     REGISTER_MUXDEMUX (RM, rm);
175 175
     REGISTER_MUXDEMUX (ROQ, roq);
176 176
     REGISTER_DEMUXER  (RPL, rpl);
177
+    REGISTER_MUXDEMUX (RSO, rso);
177 178
     REGISTER_MUXER    (RTP, rtp);
178 179
     REGISTER_MUXDEMUX (RTSP, rtsp);
179 180
     REGISTER_DEMUXER  (SDP, sdp);
... ...
@@ -22,7 +22,7 @@
22 22
 #define AVFORMAT_AVFORMAT_H
23 23
 
24 24
 #define LIBAVFORMAT_VERSION_MAJOR 52
25
-#define LIBAVFORMAT_VERSION_MINOR 75
25
+#define LIBAVFORMAT_VERSION_MINOR 76
26 26
 #define LIBAVFORMAT_VERSION_MICRO  0
27 27
 
28 28
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
29 29
new file mode 100644
... ...
@@ -0,0 +1,30 @@
0
+/*
1
+ * RSO format common data
2
+ * Copyright (c) 2010 Rafael Carre
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "internal.h"
23
+#include "rso.h"
24
+
25
+const AVCodecTag ff_codec_rso_tags[] = {
26
+    { CODEC_ID_PCM_U8,          0x0100 },
27
+    { CODEC_ID_ADPCM_IMA_WAV,   0x0101 },
28
+    { CODEC_ID_NONE, 0 },
29
+};
0 30
new file mode 100644
... ...
@@ -0,0 +1,32 @@
0
+/*
1
+ * RSO format common data
2
+ * Copyright (c) 2010 Rafael Carre
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVFORMAT_RSO_H
22
+#define AVFORMAT_RSO_H
23
+
24
+#include "internal.h"
25
+
26
+#define RSO_HEADER_SIZE 8
27
+
28
+/* The ffmpeg codecs we support, and the IDs they have in the file */
29
+extern const AVCodecTag ff_codec_rso_tags[];
30
+
31
+#endif /* AVFORMAT_RSO_H */
0 32
new file mode 100644
... ...
@@ -0,0 +1,102 @@
0
+/*
1
+ * RSO demuxer
2
+ * Copyright (c) 2001 Fabrice Bellard (original AU code)
3
+ * Copyright (c) 2010 Rafael Carre
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
+#include "libavutil/intreadwrite.h"
23
+#include "avformat.h"
24
+#include "internal.h"
25
+#include "raw.h"
26
+#include "riff.h"
27
+#include "rso.h"
28
+
29
+static int rso_read_header(AVFormatContext *s, AVFormatParameters *ap)
30
+{
31
+    ByteIOContext *pb = s->pb;
32
+    int id, rate, bps;
33
+    unsigned int size;
34
+    enum CodecID codec;
35
+    AVStream *st;
36
+
37
+    id   = get_be16(pb);
38
+    size = get_be16(pb);
39
+    rate = get_be16(pb);
40
+    get_be16(pb);   /* play mode ? (0x0000 = don't loop) */
41
+
42
+    codec = ff_codec_get_id(ff_codec_rso_tags, id);
43
+
44
+    if (codec == CODEC_ID_ADPCM_IMA_WAV) {
45
+        av_log(s, AV_LOG_ERROR, "ADPCM in RSO not implemented\n");
46
+        return AVERROR_PATCHWELCOME;
47
+    }
48
+
49
+    bps = av_get_bits_per_sample(codec);
50
+    if (!bps) {
51
+        av_log_ask_for_sample(s, "could not determine bits per sample\n");
52
+        return AVERROR_INVALIDDATA;
53
+    }
54
+
55
+    /* now we are ready: build format streams */
56
+    st = av_new_stream(s, 0);
57
+    if (!st)
58
+        return AVERROR(ENOMEM);
59
+
60
+    st->duration            = (size * 8) / bps;
61
+    st->codec->codec_type   = AVMEDIA_TYPE_AUDIO;
62
+    st->codec->codec_tag    = id;
63
+    st->codec->codec_id     = codec;
64
+    st->codec->channels     = 1;
65
+    st->codec->sample_rate  = rate;
66
+
67
+    av_set_pts_info(st, 64, 1, rate);
68
+
69
+    return 0;
70
+}
71
+
72
+#define BLOCK_SIZE 1024 /* in samples */
73
+
74
+static int rso_read_packet(AVFormatContext *s, AVPacket *pkt)
75
+{
76
+    int bps = av_get_bits_per_sample(s->streams[0]->codec->codec_id);
77
+    int ret = av_get_packet(s->pb, pkt, BLOCK_SIZE * bps >> 3);
78
+
79
+    if (ret < 0)
80
+        return ret;
81
+
82
+    pkt->stream_index = 0;
83
+
84
+    /* note: we need to modify the packet size here to handle the last packet */
85
+    pkt->size = ret;
86
+
87
+    return 0;
88
+}
89
+
90
+AVInputFormat rso_demuxer = {
91
+    .name           =   "rso",
92
+    .long_name      =   NULL_IF_CONFIG_SMALL("Lego Mindstorms RSO format"),
93
+    .extensions     =   "rso",
94
+    .priv_data_size =   0,
95
+    .read_probe     =   NULL, /* no magic value in this format */
96
+    .read_header    =   rso_read_header,
97
+    .read_packet    =   rso_read_packet,
98
+    .read_close     =   NULL,
99
+    .read_seek      =   pcm_read_seek,
100
+    .codec_tag      =   (const AVCodecTag* const []){ff_codec_rso_tags, 0},
101
+};
0 102
new file mode 100644
... ...
@@ -0,0 +1,114 @@
0
+/*
1
+ * RSO muxer
2
+ * Copyright (c) 2001 Fabrice Bellard (original AU code)
3
+ * Copyright (c) 2010 Rafael Carre
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
+#include "avformat.h"
23
+#include "internal.h"
24
+#include "riff.h"
25
+#include "rso.h"
26
+
27
+static int rso_write_header(AVFormatContext *s)
28
+{
29
+    ByteIOContext  *pb  = s->pb;
30
+    AVCodecContext *enc = s->streams[0]->codec;
31
+
32
+    if (!enc->codec_tag)
33
+        return AVERROR_INVALIDDATA;
34
+
35
+    if (enc->channels != 1) {
36
+        av_log(s, AV_LOG_ERROR, "RSO only supports mono\n");
37
+        return AVERROR_INVALIDDATA;
38
+    }
39
+
40
+    if (url_is_streamed(s->pb)) {
41
+        av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
42
+        return AVERROR_INVALIDDATA;
43
+    }
44
+
45
+    /* XXX: find legal sample rates (if any) */
46
+    if (enc->sample_rate >= 1u<<16) {
47
+        av_log(s, AV_LOG_ERROR, "Sample rate must be < 65536\n");
48
+        return AVERROR_INVALIDDATA;
49
+    }
50
+
51
+    if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
52
+        av_log(s, AV_LOG_ERROR, "ADPCM in RSO not implemented\n");
53
+        return AVERROR_PATCHWELCOME;
54
+    }
55
+
56
+    /* format header */
57
+    put_be16(pb, enc->codec_tag);   /* codec ID */
58
+    put_be16(pb, 0);                /* data size, will be written at EOF */
59
+    put_be16(pb, enc->sample_rate);
60
+    put_be16(pb, 0x0000);           /* play mode ? (0x0000 = don't loop) */
61
+
62
+    put_flush_packet(pb);
63
+
64
+    return 0;
65
+}
66
+
67
+static int rso_write_packet(AVFormatContext *s, AVPacket *pkt)
68
+{
69
+    put_buffer(s->pb, pkt->data, pkt->size);
70
+    return 0;
71
+}
72
+
73
+static int rso_write_trailer(AVFormatContext *s)
74
+{
75
+    ByteIOContext *pb = s->pb;
76
+    int64_t file_size;
77
+    uint16_t coded_file_size;
78
+
79
+    file_size = url_ftell(pb);
80
+
81
+    if (file_size < 0)
82
+        return file_size;
83
+
84
+    if (file_size > 0xffff + RSO_HEADER_SIZE) {
85
+        av_log(s, AV_LOG_WARNING,
86
+               "Output file is too big (%"PRId64" bytes >= 64kB)\n", file_size);
87
+        coded_file_size = 0xffff;
88
+    } else {
89
+        coded_file_size = file_size - RSO_HEADER_SIZE;
90
+    }
91
+
92
+    /* update file size */
93
+    url_fseek(pb, 2, SEEK_SET);
94
+    put_be16(pb, coded_file_size);
95
+    url_fseek(pb, file_size, SEEK_SET);
96
+
97
+    put_flush_packet(pb);
98
+
99
+    return 0;
100
+}
101
+
102
+AVOutputFormat rso_muxer = {
103
+    .name           =   "rso",
104
+    .long_name      =   NULL_IF_CONFIG_SMALL("Lego Mindstorms RSO format"),
105
+    .extensions     =   "rso",
106
+    .priv_data_size =   0,
107
+    .audio_codec    =   CODEC_ID_PCM_U8,
108
+    .video_codec    =   CODEC_ID_NONE,
109
+    .write_header   =   rso_write_header,
110
+    .write_packet   =   rso_write_packet,
111
+    .write_trailer  =   rso_write_trailer,
112
+    .codec_tag      =   (const AVCodecTag* const []){ff_codec_rso_tags, 0},
113
+};
... ...
@@ -206,6 +206,10 @@ if [ -n "$do_ogg" ] ; then
206 206
 do_audio_only ogg
207 207
 fi
208 208
 
209
+if [ -n "$do_rso" ] ; then
210
+do_audio_only rso
211
+fi
212
+
209 213
 # pix_fmt conversions
210 214
 
211 215
 if [ -n "$do_pixfmt" ] ; then