Mercurial > libavcodec.hg
annotate flacenc.c @ 3384:573fed4bf20f libavcodec
simplify
author | michael |
---|---|
date | Sun, 02 Jul 2006 09:30:51 +0000 |
parents | 84f29207af3a |
children | 340e5d35b326 |
rev | line source |
---|---|
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
1 /** |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
2 * FLAC audio encoder |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
3 * Copyright (c) 2006 Justin Ruggles <jruggle@earthlink.net> |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
4 * |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
5 * This library is free software; you can redistribute it and/or |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
6 * modify it under the terms of the GNU Lesser General Public |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
7 * License as published by the Free Software Foundation; either |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
8 * version 2 of the License, or (at your option) any later version. |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
9 * |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
10 * This library is distributed in the hope that it will be useful, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
13 * Lesser General Public License for more details. |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
14 * |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
15 * You should have received a copy of the GNU Lesser General Public |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
16 * License along with this library; if not, write to the Free Software |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
18 */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
19 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
20 #include "avcodec.h" |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
21 #include "bitstream.h" |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
22 #include "crc.h" |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
23 #include "golomb.h" |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
24 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
25 #define FLAC_MAX_CH 8 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
26 #define FLAC_MIN_BLOCKSIZE 16 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
27 #define FLAC_MAX_BLOCKSIZE 65535 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
28 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
29 #define FLAC_SUBFRAME_CONSTANT 0 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
30 #define FLAC_SUBFRAME_VERBATIM 1 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
31 #define FLAC_SUBFRAME_FIXED 8 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
32 #define FLAC_SUBFRAME_LPC 32 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
33 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
34 #define FLAC_CHMODE_NOT_STEREO 0 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
35 #define FLAC_CHMODE_LEFT_RIGHT 1 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
36 #define FLAC_CHMODE_LEFT_SIDE 8 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
37 #define FLAC_CHMODE_RIGHT_SIDE 9 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
38 #define FLAC_CHMODE_MID_SIDE 10 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
39 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
40 #define FLAC_STREAMINFO_SIZE 34 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
41 |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
42 typedef struct RiceContext { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
43 int porder; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
44 int params[256]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
45 } RiceContext; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
46 |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
47 typedef struct FlacSubframe { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
48 int type; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
49 int type_code; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
50 int obits; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
51 int order; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
52 RiceContext rc; |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
53 int32_t samples[FLAC_MAX_BLOCKSIZE]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
54 int32_t residual[FLAC_MAX_BLOCKSIZE]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
55 } FlacSubframe; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
56 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
57 typedef struct FlacFrame { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
58 FlacSubframe subframes[FLAC_MAX_CH]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
59 int blocksize; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
60 int bs_code[2]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
61 uint8_t crc8; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
62 int ch_mode; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
63 } FlacFrame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
64 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
65 typedef struct FlacEncodeContext { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
66 PutBitContext pb; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
67 int channels; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
68 int ch_code; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
69 int samplerate; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
70 int sr_code[2]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
71 int blocksize; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
72 int max_framesize; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
73 uint32_t frame_count; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
74 FlacFrame frame; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
75 AVCodecContext *avctx; |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
76 } FlacEncodeContext; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
77 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
78 static const int flac_samplerates[16] = { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
79 0, 0, 0, 0, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
80 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
81 0, 0, 0, 0 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
82 }; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
83 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
84 static const int flac_blocksizes[16] = { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
85 0, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
86 192, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
87 576, 1152, 2304, 4608, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
88 0, 0, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
89 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
90 }; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
91 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
92 /** |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
93 * Writes streaminfo metadata block to byte array |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
94 */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
95 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header) |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
96 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
97 PutBitContext pb; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
98 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
99 memset(header, 0, FLAC_STREAMINFO_SIZE); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
100 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
101 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
102 /* streaminfo metadata block */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
103 put_bits(&pb, 16, s->blocksize); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
104 put_bits(&pb, 16, s->blocksize); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
105 put_bits(&pb, 24, 0); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
106 put_bits(&pb, 24, s->max_framesize); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
107 put_bits(&pb, 20, s->samplerate); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
108 put_bits(&pb, 3, s->channels-1); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
109 put_bits(&pb, 5, 15); /* bits per sample - 1 */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
110 flush_put_bits(&pb); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
111 /* total samples = 0 */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
112 /* MD5 signature = 0 */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
113 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
114 |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
115 #define BLOCK_TIME_MS 27 |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
116 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
117 /** |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
118 * Sets blocksize based on samplerate |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
119 * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
120 */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
121 static int select_blocksize(int samplerate) |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
122 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
123 int i; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
124 int target; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
125 int blocksize; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
126 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
127 assert(samplerate > 0); |
3354 | 128 blocksize = flac_blocksizes[1]; |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
129 target = (samplerate * BLOCK_TIME_MS) / 1000; |
3354 | 130 for(i=0; i<16; i++) { |
131 if(target >= flac_blocksizes[i] && flac_blocksizes[i] > blocksize) { | |
132 blocksize = flac_blocksizes[i]; | |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
133 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
134 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
135 return blocksize; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
136 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
137 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
138 static int flac_encode_init(AVCodecContext *avctx) |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
139 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
140 int freq = avctx->sample_rate; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
141 int channels = avctx->channels; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
142 FlacEncodeContext *s = avctx->priv_data; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
143 int i; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
144 uint8_t *streaminfo; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
145 |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
146 s->avctx = avctx; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
147 |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
148 if(avctx->sample_fmt != SAMPLE_FMT_S16) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
149 return -1; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
150 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
151 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
152 if(channels < 1 || channels > FLAC_MAX_CH) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
153 return -1; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
154 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
155 s->channels = channels; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
156 s->ch_code = s->channels-1; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
157 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
158 /* find samplerate in table */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
159 if(freq < 1) |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
160 return -1; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
161 for(i=4; i<12; i++) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
162 if(freq == flac_samplerates[i]) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
163 s->samplerate = flac_samplerates[i]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
164 s->sr_code[0] = i; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
165 s->sr_code[1] = 0; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
166 break; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
167 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
168 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
169 /* if not in table, samplerate is non-standard */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
170 if(i == 12) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
171 if(freq % 1000 == 0 && freq < 255000) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
172 s->sr_code[0] = 12; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
173 s->sr_code[1] = freq / 1000; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
174 } else if(freq % 10 == 0 && freq < 655350) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
175 s->sr_code[0] = 14; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
176 s->sr_code[1] = freq / 10; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
177 } else if(freq < 65535) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
178 s->sr_code[0] = 13; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
179 s->sr_code[1] = freq; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
180 } else { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
181 return -1; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
182 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
183 s->samplerate = freq; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
184 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
185 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
186 s->blocksize = select_blocksize(s->samplerate); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
187 avctx->frame_size = s->blocksize; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
188 |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
189 /* set maximum encoded frame size in verbatim mode */ |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
190 if(s->channels == 2) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
191 s->max_framesize = 14 + ((s->blocksize * 33 + 7) >> 3); |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
192 } else { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
193 s->max_framesize = 14 + (s->blocksize * s->channels * 2); |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
194 } |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
195 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
196 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
197 write_streaminfo(s, streaminfo); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
198 avctx->extradata = streaminfo; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
199 avctx->extradata_size = FLAC_STREAMINFO_SIZE; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
200 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
201 s->frame_count = 0; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
202 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
203 avctx->coded_frame = avcodec_alloc_frame(); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
204 avctx->coded_frame->key_frame = 1; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
205 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
206 return 0; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
207 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
208 |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
209 static void init_frame(FlacEncodeContext *s) |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
210 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
211 int i, ch; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
212 FlacFrame *frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
213 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
214 frame = &s->frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
215 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
216 for(i=0; i<16; i++) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
217 if(s->blocksize == flac_blocksizes[i]) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
218 frame->blocksize = flac_blocksizes[i]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
219 frame->bs_code[0] = i; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
220 frame->bs_code[1] = 0; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
221 break; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
222 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
223 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
224 if(i == 16) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
225 frame->blocksize = s->blocksize; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
226 if(frame->blocksize <= 256) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
227 frame->bs_code[0] = 6; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
228 frame->bs_code[1] = frame->blocksize-1; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
229 } else { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
230 frame->bs_code[0] = 7; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
231 frame->bs_code[1] = frame->blocksize-1; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
232 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
233 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
234 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
235 for(ch=0; ch<s->channels; ch++) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
236 frame->subframes[ch].obits = 16; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
237 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
238 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
239 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
240 /** |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
241 * Copy channel-interleaved input samples into separate subframes |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
242 */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
243 static void copy_samples(FlacEncodeContext *s, int16_t *samples) |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
244 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
245 int i, j, ch; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
246 FlacFrame *frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
247 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
248 frame = &s->frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
249 for(i=0,j=0; i<frame->blocksize; i++) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
250 for(ch=0; ch<s->channels; ch++,j++) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
251 frame->subframes[ch].samples[i] = samples[j]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
252 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
253 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
254 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
255 |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
256 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
257 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k))) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
258 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
259 static int find_optimal_param(uint32_t sum, int n) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
260 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
261 int k, k_opt; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
262 uint32_t nbits, nbits_opt; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
263 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
264 k_opt = 0; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
265 nbits_opt = rice_encode_count(sum, n, 0); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
266 for(k=1; k<=14; k++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
267 nbits = rice_encode_count(sum, n, k); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
268 if(nbits < nbits_opt) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
269 nbits_opt = nbits; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
270 k_opt = k; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
271 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
272 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
273 return k_opt; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
274 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
275 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
276 static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder, |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
277 uint32_t *sums, int n, int pred_order) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
278 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
279 int i; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
280 int k, cnt, part; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
281 uint32_t all_bits; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
282 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
283 part = (1 << porder); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
284 all_bits = 0; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
285 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
286 cnt = (n >> porder) - pred_order; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
287 for(i=0; i<part; i++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
288 if(i == 1) cnt = (n >> porder); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
289 k = find_optimal_param(sums[i], cnt); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
290 rc->params[i] = k; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
291 all_bits += rice_encode_count(sums[i], cnt, k); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
292 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
293 all_bits += (4 * part); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
294 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
295 rc->porder = porder; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
296 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
297 return all_bits; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
298 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
299 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
300 static void calc_sums(int pmax, uint32_t *data, int n, int pred_order, |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
301 uint32_t sums[][256]) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
302 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
303 int i, j; |
3384 | 304 int parts; |
305 uint32_t *res, *res_end; | |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
306 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
307 /* sums for highest level */ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
308 parts = (1 << pmax); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
309 res = &data[pred_order]; |
3384 | 310 res_end = &data[n >> pmax]; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
311 for(i=0; i<parts; i++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
312 sums[pmax][i] = 0; |
3384 | 313 while(res < res_end){ |
314 sums[pmax][i] += *(res++); | |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
315 } |
3384 | 316 res_end+= n >> pmax; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
317 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
318 /* sums for lower levels */ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
319 for(i=pmax-1; i>=0; i--) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
320 parts = (1 << i); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
321 for(j=0; j<parts; j++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
322 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
323 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
324 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
325 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
326 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
327 static uint32_t calc_rice_params(RiceContext *rc, int pmax, int32_t *data, |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
328 int n, int pred_order) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
329 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
330 int i; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
331 uint32_t bits, opt_bits; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
332 int opt_porder; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
333 RiceContext opt_rc; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
334 uint32_t *udata; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
335 uint32_t sums[9][256]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
336 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
337 assert(pmax >= 0 && pmax <= 8); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
338 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
339 udata = av_malloc(n * sizeof(uint32_t)); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
340 for(i=0; i<n; i++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
341 udata[i] = (2*data[i]) ^ (data[i]>>31); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
342 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
343 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
344 calc_sums(pmax, udata, n, pred_order, sums); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
345 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
346 opt_porder = 0; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
347 opt_bits = UINT32_MAX; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
348 for(i=0; i<=pmax; i++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
349 bits = calc_optimal_rice_params(rc, i, sums[i], n, pred_order); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
350 if(bits < opt_bits) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
351 opt_bits = bits; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
352 opt_porder = i; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
353 memcpy(&opt_rc, rc, sizeof(RiceContext)); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
354 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
355 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
356 if(opt_porder != pmax) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
357 memcpy(rc, &opt_rc, sizeof(RiceContext)); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
358 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
359 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
360 av_freep(&udata); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
361 return opt_bits; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
362 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
363 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
364 static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmax, int32_t *data, |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
365 int n, int pred_order, int bps) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
366 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
367 uint32_t bits; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
368 bits = pred_order*bps + 6; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
369 bits += calc_rice_params(rc, pmax, data, n, pred_order); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
370 return bits; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
371 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
372 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
373 static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
374 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
375 assert(n > 0); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
376 memcpy(res, smp, n * sizeof(int32_t)); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
377 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
378 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
379 static void encode_residual_fixed(int32_t *res, int32_t *smp, int n, int order) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
380 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
381 int i; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
382 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
383 for(i=0; i<order; i++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
384 res[i] = smp[i]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
385 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
386 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
387 if(order==0){ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
388 for(i=order; i<n; i++) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
389 res[i]= smp[i]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
390 }else if(order==1){ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
391 for(i=order; i<n; i++) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
392 res[i]= smp[i] - smp[i-1]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
393 }else if(order==2){ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
394 for(i=order; i<n; i++) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
395 res[i]= smp[i] - 2*smp[i-1] + smp[i-2]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
396 }else if(order==3){ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
397 for(i=order; i<n; i++) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
398 res[i]= smp[i] - 3*smp[i-1] + 3*smp[i-2] - smp[i-3]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
399 }else{ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
400 for(i=order; i<n; i++) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
401 res[i]= smp[i] - 4*smp[i-1] + 6*smp[i-2] - 4*smp[i-3] + smp[i-4]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
402 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
403 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
404 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
405 static int get_max_p_order(int max_porder, int n, int order) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
406 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
407 int porder, max_parts; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
408 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
409 porder = max_porder; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
410 while(porder > 0) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
411 max_parts = (1 << porder); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
412 if(!(n % max_parts) && (n > max_parts*order)) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
413 break; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
414 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
415 porder--; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
416 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
417 return porder; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
418 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
419 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
420 static int encode_residual(FlacEncodeContext *ctx, int ch) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
421 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
422 int i, opt_order, porder, max_porder, n; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
423 FlacFrame *frame; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
424 FlacSubframe *sub; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
425 uint32_t bits[5]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
426 int32_t *res, *smp; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
427 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
428 frame = &ctx->frame; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
429 sub = &frame->subframes[ch]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
430 res = sub->residual; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
431 smp = sub->samples; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
432 n = frame->blocksize; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
433 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
434 /* CONSTANT */ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
435 for(i=1; i<n; i++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
436 if(smp[i] != smp[0]) break; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
437 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
438 if(i == n) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
439 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
440 res[0] = smp[0]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
441 return sub->obits; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
442 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
443 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
444 /* VERBATIM */ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
445 if(n < 5) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
446 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
447 encode_residual_verbatim(res, smp, n); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
448 return sub->obits * n; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
449 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
450 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
451 max_porder = 3; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
452 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
453 /* FIXED */ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
454 opt_order = 0; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
455 bits[0] = UINT32_MAX; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
456 for(i=0; i<=4; i++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
457 encode_residual_fixed(res, smp, n, i); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
458 porder = get_max_p_order(max_porder, n, i); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
459 bits[i] = calc_rice_params_fixed(&sub->rc, porder, res, n, i, sub->obits); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
460 if(bits[i] < bits[opt_order]) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
461 opt_order = i; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
462 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
463 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
464 sub->order = opt_order; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
465 sub->type = FLAC_SUBFRAME_FIXED; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
466 sub->type_code = sub->type | sub->order; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
467 if(sub->order != 4) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
468 encode_residual_fixed(res, smp, n, sub->order); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
469 porder = get_max_p_order(max_porder, n, sub->order); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
470 calc_rice_params_fixed(&sub->rc, porder, res, n, sub->order, sub->obits); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
471 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
472 return bits[sub->order]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
473 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
474 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
475 static int encode_residual_v(FlacEncodeContext *ctx, int ch) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
476 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
477 int i, n; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
478 FlacFrame *frame; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
479 FlacSubframe *sub; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
480 int32_t *res, *smp; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
481 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
482 frame = &ctx->frame; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
483 sub = &frame->subframes[ch]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
484 res = sub->residual; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
485 smp = sub->samples; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
486 n = frame->blocksize; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
487 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
488 /* CONSTANT */ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
489 for(i=1; i<n; i++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
490 if(smp[i] != smp[0]) break; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
491 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
492 if(i == n) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
493 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
494 res[0] = smp[0]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
495 return sub->obits; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
496 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
497 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
498 /* VERBATIM */ |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
499 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
500 encode_residual_verbatim(res, smp, n); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
501 return sub->obits * n; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
502 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
503 |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
504 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n) |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
505 { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
506 int i, best; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
507 int32_t lt, rt; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
508 uint64_t sum[4]; |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
509 uint64_t score[4]; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
510 int k; |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
511 |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
512 /* calculate sum of squares for each channel */ |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
513 sum[0] = sum[1] = sum[2] = sum[3] = 0; |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
514 for(i=2; i<n; i++) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
515 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2]; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
516 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2]; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
517 sum[2] += ABS((lt + rt) >> 1); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
518 sum[3] += ABS(lt - rt); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
519 sum[0] += ABS(lt); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
520 sum[1] += ABS(rt); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
521 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
522 for(i=0; i<4; i++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
523 k = find_optimal_param(2*sum[i], n); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
524 sum[i] = rice_encode_count(2*sum[i], n, k); |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
525 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
526 |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
527 /* calculate score for each mode */ |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
528 score[0] = sum[0] + sum[1]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
529 score[1] = sum[0] + sum[3]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
530 score[2] = sum[1] + sum[3]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
531 score[3] = sum[2] + sum[3]; |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
532 |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
533 /* return mode with lowest score */ |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
534 best = 0; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
535 for(i=1; i<4; i++) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
536 if(score[i] < score[best]) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
537 best = i; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
538 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
539 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
540 if(best == 0) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
541 return FLAC_CHMODE_LEFT_RIGHT; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
542 } else if(best == 1) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
543 return FLAC_CHMODE_LEFT_SIDE; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
544 } else if(best == 2) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
545 return FLAC_CHMODE_RIGHT_SIDE; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
546 } else { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
547 return FLAC_CHMODE_MID_SIDE; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
548 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
549 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
550 |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
551 /** |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
552 * Perform stereo channel decorrelation |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
553 */ |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
554 static void channel_decorrelation(FlacEncodeContext *ctx) |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
555 { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
556 FlacFrame *frame; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
557 int32_t *left, *right; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
558 int i, n; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
559 |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
560 frame = &ctx->frame; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
561 n = frame->blocksize; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
562 left = frame->subframes[0].samples; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
563 right = frame->subframes[1].samples; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
564 |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
565 if(ctx->channels != 2) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
566 frame->ch_mode = FLAC_CHMODE_NOT_STEREO; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
567 return; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
568 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
569 |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
570 frame->ch_mode = estimate_stereo_mode(left, right, n); |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
571 |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
572 /* perform decorrelation and adjust bits-per-sample */ |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
573 if(frame->ch_mode == FLAC_CHMODE_LEFT_RIGHT) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
574 return; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
575 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
576 if(frame->ch_mode == FLAC_CHMODE_MID_SIDE) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
577 int32_t tmp; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
578 for(i=0; i<n; i++) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
579 tmp = left[i]; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
580 left[i] = (tmp + right[i]) >> 1; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
581 right[i] = tmp - right[i]; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
582 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
583 frame->subframes[1].obits++; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
584 } else if(frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
585 for(i=0; i<n; i++) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
586 right[i] = left[i] - right[i]; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
587 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
588 frame->subframes[1].obits++; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
589 } else { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
590 for(i=0; i<n; i++) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
591 left[i] -= right[i]; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
592 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
593 frame->subframes[0].obits++; |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
594 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
595 } |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
596 |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
597 static void put_sbits(PutBitContext *pb, int bits, int32_t val) |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
598 { |
3354 | 599 assert(bits >= 0 && bits <= 31); |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
600 |
3354 | 601 put_bits(pb, bits, val & ((1<<bits)-1)); |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
602 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
603 |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
604 static void write_utf8(PutBitContext *pb, uint32_t val) |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
605 { |
3354 | 606 int bytes, shift; |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
607 |
3354 | 608 if(val < 0x80){ |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
609 put_bits(pb, 8, val); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
610 return; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
611 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
612 |
3357 | 613 bytes= (av_log2(val)+4) / 5; |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
614 shift = (bytes - 1) * 6; |
3354 | 615 put_bits(pb, 8, (256 - (256>>bytes)) | (val >> shift)); |
616 while(shift >= 6){ | |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
617 shift -= 6; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
618 put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F)); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
619 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
620 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
621 |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
622 static void output_frame_header(FlacEncodeContext *s) |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
623 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
624 FlacFrame *frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
625 int crc; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
626 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
627 frame = &s->frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
628 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
629 put_bits(&s->pb, 16, 0xFFF8); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
630 put_bits(&s->pb, 4, frame->bs_code[0]); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
631 put_bits(&s->pb, 4, s->sr_code[0]); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
632 if(frame->ch_mode == FLAC_CHMODE_NOT_STEREO) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
633 put_bits(&s->pb, 4, s->ch_code); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
634 } else { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
635 put_bits(&s->pb, 4, frame->ch_mode); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
636 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
637 put_bits(&s->pb, 3, 4); /* bits-per-sample code */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
638 put_bits(&s->pb, 1, 0); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
639 write_utf8(&s->pb, s->frame_count); |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
640 if(frame->bs_code[0] == 6) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
641 put_bits(&s->pb, 8, frame->bs_code[1]); |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
642 } else if(frame->bs_code[0] == 7) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
643 put_bits(&s->pb, 16, frame->bs_code[1]); |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
644 } |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
645 if(s->sr_code[0] == 12) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
646 put_bits(&s->pb, 8, s->sr_code[1]); |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
647 } else if(s->sr_code[0] > 12) { |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
648 put_bits(&s->pb, 16, s->sr_code[1]); |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
649 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
650 flush_put_bits(&s->pb); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
651 crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
652 put_bits(&s->pb, 8, crc); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
653 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
654 |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
655 static void output_subframe_constant(FlacEncodeContext *s, int ch) |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
656 { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
657 FlacSubframe *sub; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
658 int32_t res; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
659 |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
660 sub = &s->frame.subframes[ch]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
661 res = sub->residual[0]; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
662 put_sbits(&s->pb, sub->obits, res); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
663 } |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
664 |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
665 static void output_subframe_verbatim(FlacEncodeContext *s, int ch) |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
666 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
667 int i; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
668 FlacFrame *frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
669 FlacSubframe *sub; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
670 int32_t res; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
671 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
672 frame = &s->frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
673 sub = &frame->subframes[ch]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
674 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
675 for(i=0; i<frame->blocksize; i++) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
676 res = sub->residual[i]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
677 put_sbits(&s->pb, sub->obits, res); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
678 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
679 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
680 |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
681 static void output_residual(FlacEncodeContext *ctx, int ch) |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
682 { |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
683 int i, j, p, n, parts; |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
684 int k, porder, psize, res_cnt; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
685 FlacFrame *frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
686 FlacSubframe *sub; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
687 int32_t *res; |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
688 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
689 frame = &ctx->frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
690 sub = &frame->subframes[ch]; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
691 res = sub->residual; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
692 n = frame->blocksize; |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
693 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
694 /* rice-encoded block */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
695 put_bits(&ctx->pb, 2, 0); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
696 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
697 /* partition order */ |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
698 porder = sub->rc.porder; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
699 psize = n >> porder; |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
700 parts = (1 << porder); |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
701 put_bits(&ctx->pb, 4, porder); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
702 res_cnt = psize - sub->order; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
703 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
704 /* residual */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
705 j = sub->order; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
706 for(p=0; p<parts; p++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
707 k = sub->rc.params[p]; |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
708 put_bits(&ctx->pb, 4, k); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
709 if(p == 1) res_cnt = psize; |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
710 for(i=0; i<res_cnt && j<n; i++, j++) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
711 set_sr_golomb_flac(&ctx->pb, res[j], k, INT32_MAX, 0); |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
712 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
713 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
714 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
715 |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
716 static void output_subframe_fixed(FlacEncodeContext *ctx, int ch) |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
717 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
718 int i; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
719 FlacFrame *frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
720 FlacSubframe *sub; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
721 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
722 frame = &ctx->frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
723 sub = &frame->subframes[ch]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
724 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
725 /* warm-up samples */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
726 for(i=0; i<sub->order; i++) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
727 put_sbits(&ctx->pb, sub->obits, sub->residual[i]); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
728 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
729 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
730 /* residual */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
731 output_residual(ctx, ch); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
732 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
733 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
734 static void output_subframes(FlacEncodeContext *s) |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
735 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
736 FlacFrame *frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
737 FlacSubframe *sub; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
738 int ch; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
739 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
740 frame = &s->frame; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
741 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
742 for(ch=0; ch<s->channels; ch++) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
743 sub = &frame->subframes[ch]; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
744 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
745 /* subframe header */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
746 put_bits(&s->pb, 1, 0); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
747 put_bits(&s->pb, 6, sub->type_code); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
748 put_bits(&s->pb, 1, 0); /* no wasted bits */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
749 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
750 /* subframe */ |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
751 if(sub->type == FLAC_SUBFRAME_CONSTANT) { |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
752 output_subframe_constant(s, ch); |
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
753 } else if(sub->type == FLAC_SUBFRAME_VERBATIM) { |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
754 output_subframe_verbatim(s, ch); |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
755 } else if(sub->type == FLAC_SUBFRAME_FIXED) { |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
756 output_subframe_fixed(s, ch); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
757 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
758 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
759 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
760 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
761 static void output_frame_footer(FlacEncodeContext *s) |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
762 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
763 int crc; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
764 flush_put_bits(&s->pb); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
765 crc = bswap_16(av_crc(av_crc8005, 0, s->pb.buf, put_bits_count(&s->pb)>>3)); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
766 put_bits(&s->pb, 16, crc); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
767 flush_put_bits(&s->pb); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
768 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
769 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
770 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
771 int buf_size, void *data) |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
772 { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
773 int ch; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
774 FlacEncodeContext *s; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
775 int16_t *samples = data; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
776 int out_bytes; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
777 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
778 s = avctx->priv_data; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
779 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
780 s->blocksize = avctx->frame_size; |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
781 init_frame(s); |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
782 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
783 copy_samples(s, samples); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
784 |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
785 channel_decorrelation(s); |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
786 |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
787 for(ch=0; ch<s->channels; ch++) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
788 encode_residual(s, ch); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
789 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
790 init_put_bits(&s->pb, frame, buf_size); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
791 output_frame_header(s); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
792 output_subframes(s); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
793 output_frame_footer(s); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
794 out_bytes = put_bits_count(&s->pb) >> 3; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
795 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
796 if(out_bytes > s->max_framesize || out_bytes >= buf_size) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
797 /* frame too large. use verbatim mode */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
798 for(ch=0; ch<s->channels; ch++) { |
3365
84f29207af3a
flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net
michael
parents:
3358
diff
changeset
|
799 encode_residual_v(s, ch); |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
800 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
801 init_put_bits(&s->pb, frame, buf_size); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
802 output_frame_header(s); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
803 output_subframes(s); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
804 output_frame_footer(s); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
805 out_bytes = put_bits_count(&s->pb) >> 3; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
806 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
807 if(out_bytes > s->max_framesize || out_bytes >= buf_size) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
808 /* still too large. must be an error. */ |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
809 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n"); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
810 return -1; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
811 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
812 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
813 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
814 s->frame_count++; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
815 return out_bytes; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
816 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
817 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
818 static int flac_encode_close(AVCodecContext *avctx) |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
819 { |
3358
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
820 av_freep(&avctx->extradata); |
4ae69b5b596b
stereo decorrelation support by (Justin Ruggles jruggle earthlink net>)
michael
parents:
3357
diff
changeset
|
821 avctx->extradata_size = 0; |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
822 av_freep(&avctx->coded_frame); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
823 return 0; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
824 } |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
825 |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
826 AVCodec flac_encoder = { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
827 "flac", |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
828 CODEC_TYPE_AUDIO, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
829 CODEC_ID_FLAC, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
830 sizeof(FlacEncodeContext), |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
831 flac_encode_init, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
832 flac_encode_frame, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
833 flac_encode_close, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
834 NULL, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
835 .capabilities = CODEC_CAP_SMALL_LAST_FRAME, |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
diff
changeset
|
836 }; |