]> git.sesse.net Git - ffmpeg/blob - libavcodec/s302menc.c
Merge commit '7c6eb0a1b7bf1aac7f033a7ec6d8cacc3b5c2615'
[ffmpeg] / libavcodec / s302menc.c
1 /*
2  * SMPTE 302M encoder
3  * Copyright (c) 2010 Google, Inc.
4  * Copyright (c) 2013 Darryl Wallace <wallacdj@gmail.com>
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 "avcodec.h"
24 #include "internal.h"
25 #include "mathops.h"
26 #include "put_bits.h"
27
28 #define AES3_HEADER_LEN 4
29
30 typedef struct S302MEncContext {
31     uint8_t framing_index; /* Set for even channels on multiple of 192 samples */
32 } S302MEncContext;
33
34 static av_cold int s302m_encode_init(AVCodecContext *avctx)
35 {
36     S302MEncContext *s = avctx->priv_data;
37
38     if (avctx->channels & 1 || avctx->channels > 8) {
39         av_log(avctx, AV_LOG_ERROR,
40                "Encoding %d channel(s) is not allowed. Only 2, 4, 6 and 8 channels are supported.\n",
41                avctx->channels);
42         return AVERROR(EINVAL);
43     }
44
45     switch (avctx->sample_fmt) {
46     case AV_SAMPLE_FMT_S16:
47         avctx->bits_per_raw_sample = 16;
48         break;
49     case AV_SAMPLE_FMT_S32:
50         if (avctx->bits_per_raw_sample > 20) {
51             if (avctx->bits_per_raw_sample > 24)
52                 av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
53             avctx->bits_per_raw_sample = 24;
54         } else if (!avctx->bits_per_raw_sample) {
55             avctx->bits_per_raw_sample = 24;
56         } else if (avctx->bits_per_raw_sample <= 20) {
57             avctx->bits_per_raw_sample = 20;
58         }
59     }
60
61     avctx->frame_size = 0;
62     avctx->bit_rate   = 48000 * avctx->channels *
63                        (avctx->bits_per_raw_sample + 4);
64     s->framing_index  = 0;
65
66     return 0;
67 }
68
69 static int s302m_encode2_frame(AVCodecContext *avctx, AVPacket *avpkt,
70                                const AVFrame *frame, int *got_packet_ptr)
71 {
72     S302MEncContext *s = avctx->priv_data;
73     const int buf_size = AES3_HEADER_LEN +
74                         (frame->nb_samples *
75                          avctx->channels *
76                         (avctx->bits_per_raw_sample + 4)) / 8;
77     int ret, c, channels;
78     uint8_t *o;
79     PutBitContext pb;
80
81     if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size, 0)) < 0)
82         return ret;
83
84     o = avpkt->data;
85     init_put_bits(&pb, o, buf_size);
86     put_bits(&pb, 16, buf_size - AES3_HEADER_LEN);
87     put_bits(&pb, 2, (avctx->channels - 2) >> 1);   // number of channels
88     put_bits(&pb, 8, 0);                            // channel ID
89     put_bits(&pb, 2, (avctx->bits_per_raw_sample - 16) / 4); // bits per samples (0 = 16bit, 1 = 20bit, 2 = 24bit)
90     put_bits(&pb, 4, 0);                            // alignments
91     flush_put_bits(&pb);
92     o += AES3_HEADER_LEN;
93
94     if (avctx->bits_per_raw_sample == 24) {
95         const uint32_t *samples = (uint32_t *)frame->data[0];
96
97         for (c = 0; c < frame->nb_samples; c++) {
98             uint8_t vucf = s->framing_index == 0 ? 0x10: 0;
99
100             for (channels = 0; channels < avctx->channels; channels += 2) {
101                 o[0] = ff_reverse[(samples[0] & 0x0000FF00) >> 8];
102                 o[1] = ff_reverse[(samples[0] & 0x00FF0000) >> 16];
103                 o[2] = ff_reverse[(samples[0] & 0xFF000000) >> 24];
104                 o[3] = ff_reverse[(samples[1] & 0x00000F00) >> 4] | vucf;
105                 o[4] = ff_reverse[(samples[1] & 0x000FF000) >> 12];
106                 o[5] = ff_reverse[(samples[1] & 0x0FF00000) >> 20];
107                 o[6] = ff_reverse[(samples[1] & 0xF0000000) >> 28];
108                 o += 7;
109                 samples += 2;
110             }
111
112             s->framing_index++;
113             if (s->framing_index >= 192)
114                 s->framing_index = 0;
115         }
116     } else if (avctx->bits_per_raw_sample == 20) {
117         const uint32_t *samples = (uint32_t *)frame->data[0];
118
119         for (c = 0; c < frame->nb_samples; c++) {
120             uint8_t vucf = s->framing_index == 0 ? 0x80: 0;
121
122             for (channels = 0; channels < avctx->channels; channels += 2) {
123                 o[0] = ff_reverse[ (samples[0] & 0x000FF000) >> 12];
124                 o[1] = ff_reverse[ (samples[0] & 0x0FF00000) >> 20];
125                 o[2] = ff_reverse[((samples[0] & 0xF0000000) >> 28) | vucf];
126                 o[3] = ff_reverse[ (samples[1] & 0x000FF000) >> 12];
127                 o[4] = ff_reverse[ (samples[1] & 0x0FF00000) >> 20];
128                 o[5] = ff_reverse[ (samples[1] & 0xF0000000) >> 28];
129                 o += 6;
130                 samples += 2;
131             }
132
133             s->framing_index++;
134             if (s->framing_index >= 192)
135                 s->framing_index = 0;
136         }
137     } else if (avctx->bits_per_raw_sample == 16) {
138         const uint16_t *samples = (uint16_t *)frame->data[0];
139
140         for (c = 0; c < frame->nb_samples; c++) {
141             uint8_t vucf = s->framing_index == 0 ? 0x10 : 0;
142
143             for (channels = 0; channels < avctx->channels; channels += 2) {
144                 o[0] = ff_reverse[ samples[0] & 0xFF];
145                 o[1] = ff_reverse[(samples[0] & 0xFF00) >>  8];
146                 o[2] = ff_reverse[(samples[1] & 0x0F)   <<  4] | vucf;
147                 o[3] = ff_reverse[(samples[1] & 0x0FF0) >>  4];
148                 o[4] = ff_reverse[(samples[1] & 0xF000) >> 12];
149                 o += 5;
150                 samples += 2;
151
152             }
153
154             s->framing_index++;
155             if (s->framing_index >= 192)
156                 s->framing_index = 0;
157         }
158     }
159
160     *got_packet_ptr = 1;
161
162     return 0;
163 }
164
165 AVCodec ff_s302m_encoder = {
166     .name                  = "s302m",
167     .long_name             = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
168     .type                  = AVMEDIA_TYPE_AUDIO,
169     .id                    = AV_CODEC_ID_S302M,
170     .priv_data_size        = sizeof(S302MEncContext),
171     .init                  = s302m_encode_init,
172     .encode2               = s302m_encode2_frame,
173     .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
174                                                             AV_SAMPLE_FMT_S16,
175                                                             AV_SAMPLE_FMT_NONE },
176     .capabilities          = CODEC_CAP_VARIABLE_FRAME_SIZE | CODEC_CAP_EXPERIMENTAL,
177     .supported_samplerates = (const int[]) { 48000, 0 },
178 };