OSDN Git Service

ffmdec: handle wrapped file in ffm_seek
[coroid/ffmpeg_saccubus.git] / libavformat / latmenc.c
1 /*
2  * LATM/LOAS muxer
3  * Copyright (c) 2011 Kieran Kunhya <kieran@kunhya.com>
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
22 #include "libavcodec/get_bits.h"
23 #include "libavcodec/put_bits.h"
24 #include "libavcodec/avcodec.h"
25 #include "libavcodec/mpeg4audio.h"
26 #include "libavutil/opt.h"
27 #include "avformat.h"
28 #include "rawenc.h"
29
30 typedef struct {
31     int off;
32     int channel_conf;
33     int object_type;
34     int counter;
35     int mod;
36 } LATMContext;
37
38 static const AVOption options[] = {
39     {"smc-interval", "StreamMuxConfig interval.",
40      offsetof(LATMContext, mod), FF_OPT_TYPE_INT, {.dbl = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
41     {NULL},
42 };
43
44 static const AVClass latm_muxer_class = {
45     .class_name = "LATM/LOAS muxer",
46     .item_name  = av_default_item_name,
47     .option     = options,
48     .version    = LIBAVUTIL_VERSION_INT,
49 };
50
51 static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
52 {
53     GetBitContext gb;
54     MPEG4AudioConfig m4ac;
55
56     init_get_bits(&gb, buf, size * 8);
57     ctx->off = ff_mpeg4audio_get_config(&m4ac, buf, size);
58     if (ctx->off < 0)
59         return ctx->off;
60     skip_bits_long(&gb, ctx->off);
61
62     /* FIXME: are any formats not allowed in LATM? */
63
64     if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
65         av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
66         return AVERROR_INVALIDDATA;
67     }
68     ctx->channel_conf = m4ac.chan_config;
69     ctx->object_type  = m4ac.object_type;
70
71     return 0;
72 }
73
74 static int latm_write_header(AVFormatContext *s)
75 {
76     LATMContext *ctx = s->priv_data;
77     AVCodecContext *avctx = s->streams[0]->codec;
78
79     if (avctx->codec_id == CODEC_ID_AAC_LATM)
80         return 0;
81
82     if (avctx->extradata_size > 0 &&
83         latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
84         return AVERROR_INVALIDDATA;
85
86     return 0;
87 }
88
89 static int latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
90 {
91     LATMContext *ctx = s->priv_data;
92     AVCodecContext *avctx = s->streams[0]->codec;
93     GetBitContext gb;
94     int header_size;
95
96     /* AudioMuxElement */
97     put_bits(bs, 1, !!ctx->counter);
98
99     if (!ctx->counter) {
100         init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
101
102         /* StreamMuxConfig */
103         put_bits(bs, 1, 0); /* audioMuxVersion */
104         put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
105         put_bits(bs, 6, 0); /* numSubFrames */
106         put_bits(bs, 4, 0); /* numProgram */
107         put_bits(bs, 3, 0); /* numLayer */
108
109         /* AudioSpecificConfig */
110         if (ctx->object_type == AOT_ALS) {
111             header_size = avctx->extradata_size-(ctx->off + 7) >> 3;
112             ff_copy_bits(bs, &avctx->extradata[ctx->off], header_size);
113         } else {
114             ff_copy_bits(bs, avctx->extradata, ctx->off + 3);
115
116             if (!ctx->channel_conf) {
117                 ff_copy_pce_data(bs, &gb);
118             }
119         }
120
121         put_bits(bs, 3, 0); /* frameLengthType */
122         put_bits(bs, 8, 0); /* latmBufferFullness */
123
124         put_bits(bs, 1, 0); /* otherDataPresent */
125         put_bits(bs, 1, 0); /* crcCheckPresent */
126     }
127
128     ctx->counter++;
129     ctx->counter %= ctx->mod;
130
131     return 0;
132 }
133
134 static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
135 {
136     AVIOContext *pb = s->pb;
137     PutBitContext bs;
138     int i, len;
139     uint8_t loas_header[] = "\x56\xe0\x00";
140     uint8_t *buf;
141
142     if (s->streams[0]->codec->codec_id == CODEC_ID_AAC_LATM)
143         return ff_raw_write_packet(s, pkt);
144
145     if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
146         av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
147         return AVERROR_INVALIDDATA;
148     }
149
150     buf = av_malloc(pkt->size+1024);
151     if (!buf)
152         return AVERROR(ENOMEM);
153
154     init_put_bits(&bs, buf, pkt->size+1024);
155
156     latm_write_frame_header(s, &bs);
157
158     /* PayloadLengthInfo() */
159     for (i = 0; i <= pkt->size-255; i+=255)
160         put_bits(&bs, 8, 255);
161
162     put_bits(&bs, 8, pkt->size-i);
163
164     /* The LATM payload is written unaligned */
165
166     /* PayloadMux() */
167     for (i = 0; i < pkt->size; i++)
168         put_bits(&bs, 8, pkt->data[i]);
169
170     align_put_bits(&bs);
171     flush_put_bits(&bs);
172
173     len = put_bits_count(&bs) >> 3;
174
175     loas_header[1] |= (len >> 8) & 0x1f;
176     loas_header[2] |= len & 0xff;
177
178     avio_write(pb, loas_header, 3);
179     avio_write(pb, buf, len);
180
181     av_free(buf);
182
183     return 0;
184 }
185
186 AVOutputFormat ff_latm_muxer = {
187     .name           = "latm",
188     .long_name      = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
189     .mime_type      = "audio/MP4A-LATM",
190     .extensions     = "latm,loas",
191     .priv_data_size = sizeof(LATMContext),
192     .audio_codec    = CODEC_ID_AAC,
193     .video_codec    = CODEC_ID_NONE,
194     .write_header   = latm_write_header,
195     .write_packet   = latm_write_packet,
196     .priv_class     = &latm_muxer_class,
197 };