]> git.sesse.net Git - vlc/blob - modules/demux/rawdv.h
mediacodec: don't loop in GetOutput
[vlc] / modules / demux / rawdv.h
1 /*****************************************************************************
2  * rawdv.h : raw DV helpers
3  *****************************************************************************
4  * Copyright (C) 2001-2011 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Paul Corke <paul dot corke at datatote dot co dot uk>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #define DV_PAL_FRAME_SIZE  (12 * 150 * 80)
26 #define DV_NTSC_FRAME_SIZE (10 * 150 * 80)
27
28
29 static const uint16_t dv_audio_shuffle525[10][9] = {
30   {  0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
31   {  6, 36, 66, 26, 56, 86, 16, 46, 76 },
32   { 12, 42, 72,  2, 32, 62, 22, 52, 82 },
33   { 18, 48, 78,  8, 38, 68, 28, 58, 88 },
34   { 24, 54, 84, 14, 44, 74,  4, 34, 64 },
35
36   {  1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
37   {  7, 37, 67, 27, 57, 87, 17, 47, 77 },
38   { 13, 43, 73,  3, 33, 63, 23, 53, 83 },
39   { 19, 49, 79,  9, 39, 69, 29, 59, 89 },
40   { 25, 55, 85, 15, 45, 75,  5, 35, 65 },
41 };
42
43 static const uint16_t dv_audio_shuffle625[12][9] = {
44   {   0,  36,  72,  26,  62,  98,  16,  52,  88}, /* 1st channel */
45   {   6,  42,  78,  32,  68, 104,  22,  58,  94},
46   {  12,  48,  84,   2,  38,  74,  28,  64, 100},
47   {  18,  54,  90,   8,  44,  80,  34,  70, 106},
48   {  24,  60,  96,  14,  50,  86,   4,  40,  76},
49   {  30,  66, 102,  20,  56,  92,  10,  46,  82},
50
51   {   1,  37,  73,  27,  63,  99,  17,  53,  89}, /* 2nd channel */
52   {   7,  43,  79,  33,  69, 105,  23,  59,  95},
53   {  13,  49,  85,   3,  39,  75,  29,  65, 101},
54   {  19,  55,  91,   9,  45,  81,  35,  71, 107},
55   {  25,  61,  97,  15,  51,  87,   5,  41,  77},
56   {  31,  67, 103,  21,  57,  93,  11,  47,  83},
57 };
58
59 static inline uint16_t dv_audio_12to16( uint16_t sample )
60 {
61     uint16_t shift, result;
62
63     sample = (sample < 0x800) ? sample : sample | 0xf000;
64     shift = (sample & 0xf00) >> 8;
65
66     if (shift < 0x2 || shift > 0xd) {
67         result = sample;
68     } else if (shift < 0x8) {
69         shift--;
70         result = (sample - (256 * shift)) << shift;
71     } else {
72         shift = 0xe - shift;
73         result = ((sample + ((256 * shift) + 1)) << shift) - 1;
74     }
75
76     return result;
77 }
78
79 static inline void dv_get_audio_format( es_format_t *p_fmt,
80                                         const uint8_t *p_aaux_src )
81 {
82     /* 12 bits non-linear will be converted to 16 bits linear */
83     es_format_Init( p_fmt, AUDIO_ES, VLC_CODEC_S16L );
84
85     p_fmt->audio.i_bitspersample = 16;
86     p_fmt->audio.i_channels = 2;
87     switch( (p_aaux_src[4-1] >> 3) & 0x07 )
88     {
89     case 0:
90         p_fmt->audio.i_rate = 48000;
91         break;
92     case 1:
93         p_fmt->audio.i_rate = 44100;
94         break;
95     case 2:
96     default:
97         p_fmt->audio.i_rate = 32000;
98         break;
99     }
100 }
101
102 static inline int dv_get_audio_sample_count( const uint8_t *p_buffer, int i_dsf )
103 {
104     int i_samples = p_buffer[0] & 0x3f; /* samples in this frame - min samples */
105     switch( (p_buffer[3] >> 3) & 0x07 )
106     {
107     case 0:
108         return i_samples + (i_dsf ? 1896 : 1580);
109     case 1:
110         return i_samples + (i_dsf ? 1742 : 1452);
111     case 2:
112     default:
113         return i_samples + (i_dsf ? 1264 : 1053);
114     }
115 }
116
117 static inline block_t *dv_extract_audio( block_t *p_frame_block )
118 {
119     block_t *p_block;
120     uint8_t *p_frame, *p_buf;
121     int i_audio_quant, i_samples, i_half_ch;
122     const uint16_t (*audio_shuffle)[9];
123     int i, j, d, of;
124
125     if( p_frame_block->i_buffer < 4 )
126         return NULL;
127     const int i_dsf = (p_frame_block->p_buffer[3] & 0x80) >> 7;
128     if( p_frame_block->i_buffer < (i_dsf ? DV_PAL_FRAME_SIZE
129                                          : DV_NTSC_FRAME_SIZE ) )
130         return NULL;
131
132     /* Beginning of AAUX pack */
133     p_buf = p_frame_block->p_buffer + 80*6+80*16*3 + 3;
134     if( *p_buf != 0x50 ) return NULL;
135
136     i_audio_quant = p_buf[4] & 0x07; /* 0 - 16bit, 1 - 12bit */
137     if( i_audio_quant > 1 )
138         return NULL;
139
140     i_samples = dv_get_audio_sample_count( &p_buf[1], i_dsf );
141
142     p_block = block_Alloc( 4 * i_samples );
143
144     /* for each DIF segment */
145     p_frame = p_frame_block->p_buffer;
146     audio_shuffle = i_dsf ? dv_audio_shuffle625 : dv_audio_shuffle525;
147     i_half_ch = (i_dsf ? 12 : 10)/2;
148     for( i = 0; i < (i_dsf ? 12 : 10); i++ )
149     {
150         p_frame += 6 * 80; /* skip DIF segment header */
151
152         if( i_audio_quant == 1 && i == i_half_ch ) break;
153
154         for( j = 0; j < 9; j++ )
155         {
156             for( d = 8; d < 80; d += 2 )
157             {
158                 if( i_audio_quant == 0 )
159                 {
160                     /* 16bit quantization */
161                     of = audio_shuffle[i][j] + (d - 8) / 2 *
162                            (i_dsf ? 108 : 90);
163
164                     if( of * 2 >= 4 * i_samples ) continue;
165
166                     /* big endian */
167                     p_block->p_buffer[of*2] = p_frame[d+1];
168                     p_block->p_buffer[of*2+1] = p_frame[d];
169
170                     if( p_block->p_buffer[of*2+1] == 0x80 &&
171                         p_block->p_buffer[of*2] == 0x00 )
172                         p_block->p_buffer[of*2+1] = 0;
173                 }
174                 else
175                 {
176                     /* 12bit quantization */
177                     uint16_t lc = (p_frame[d+0] << 4) | (p_frame[d+2] >> 4);
178                     uint16_t rc = (p_frame[d+1] << 4) | (p_frame[d+2] & 0x0f);
179
180                     lc = lc == 0x800 ? 0 : dv_audio_12to16(lc);
181                     rc = rc == 0x800 ? 0 : dv_audio_12to16(rc);
182
183                     of = audio_shuffle[i][j] + (d - 8) / 3 * (i_dsf ? 108 : 90);
184                     if( of*2 >= 4 * i_samples )
185                         continue;
186                     p_block->p_buffer[of*2+0] = lc & 0xff;
187                     p_block->p_buffer[of*2+1] = lc >> 8;
188
189                     of = audio_shuffle[i + i_half_ch][j] + (d - 8) / 3 * (i_dsf ? 108 : 90);
190                     if( of*2 >= 4 * i_samples )
191                         continue;
192                     p_block->p_buffer[of*2+0] = rc & 0xff;
193                     p_block->p_buffer[of*2+1] = rc >> 8;
194
195                     ++d;
196                 }
197             }
198
199             p_frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
200         }
201     }
202
203     p_block->i_pts = p_frame_block->i_pts > VLC_TS_INVALID ? p_frame_block->i_pts
204                                                            : p_frame_block->i_dts;
205     p_block->i_dts = p_frame_block->i_dts;
206     return p_block;
207 }
208