annotate svq1enc.c @ 10507:ca71c924b65f libavcodec

cosmetics : fix indentation.
author jai_menon
date Tue, 10 Nov 2009 14:37:42 +0000
parents 0a6fc654cfb6
children d0657e337f91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
1 /*
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
2 * SVQ1 Encoder
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
3 * Copyright (C) 2004 Mike Melanson <melanson@pcisys.net>
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
4 *
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
5 * This file is part of FFmpeg.
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
6 *
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
11 *
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
15 * Lesser General Public License for more details.
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
16 *
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
20 */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
21
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
22 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 8590
diff changeset
23 * @file libavcodec/svq1enc.c
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
24 * Sorenson Vector Quantizer #1 (SVQ1) video codec.
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
25 * For more information of the SVQ1 algorithm, visit:
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
26 * http://www.pcisys.net/~melanson/codecs/
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
27 */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
28
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
29
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
30 #include "avcodec.h"
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
31 #include "dsputil.h"
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
32 #include "mpegvideo.h"
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
33
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
34 #include "svq1.h"
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
35 #include "svq1enc_cb.h"
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
36
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
37 #undef NDEBUG
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
38 #include <assert.h>
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
39
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
40
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
41 typedef struct SVQ1Context {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
42 MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independent of MpegEncContext, so this will be removed then (FIXME/XXX)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
43 AVCodecContext *avctx;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
44 DSPContext dsp;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
45 AVFrame picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
46 AVFrame current_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
47 AVFrame last_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
48 PutBitContext pb;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
49 GetBitContext gb;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
50
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
51 PutBitContext reorder_pb[6]; //why ooh why this sick breadth first order, everything is slower and more complex
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
52
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
53 int frame_width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
54 int frame_height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
55
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
56 /* Y plane block dimensions */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
57 int y_block_width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
58 int y_block_height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
59
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
60 /* U & V plane (C planes) block dimensions */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
61 int c_block_width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
62 int c_block_height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
63
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
64 uint16_t *mb_type;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
65 uint32_t *dummy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
66 int16_t (*motion_val8[3])[2];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
67 int16_t (*motion_val16[3])[2];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
68
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
69 int64_t rd_total;
8211
b9e82845e7eb svq1enc: move scratch buffer from stack to context to ensure alignment
mru
parents: 7040
diff changeset
70
b9e82845e7eb svq1enc: move scratch buffer from stack to context to ensure alignment
mru
parents: 7040
diff changeset
71 uint8_t *scratchbuf;
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
72 } SVQ1Context;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
73
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
74 static void svq1_write_header(SVQ1Context *s, int frame_type)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
75 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
76 int i;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
77
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
78 /* frame code */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
79 put_bits(&s->pb, 22, 0x20);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
80
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
81 /* temporal reference (sure hope this is a "don't care") */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
82 put_bits(&s->pb, 8, 0x00);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
83
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
84 /* frame type */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
85 put_bits(&s->pb, 2, frame_type - 1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
86
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
87 if (frame_type == FF_I_TYPE) {
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
88
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
89 /* no checksum since frame code is 0x20 */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
90
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
91 /* no embedded string either */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
92
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
93 /* output 5 unknown bits (2 + 2 + 1) */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
94 put_bits(&s->pb, 5, 2); /* 2 needed by quicktime decoder */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
95
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
96 for (i = 0; i < 7; i++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
97 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
98 if ((ff_svq1_frame_size_table[i].width == s->frame_width) &&
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
99 (ff_svq1_frame_size_table[i].height == s->frame_height))
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
100 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
101 put_bits(&s->pb, 3, i);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
102 break;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
103 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
104 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
105
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
106 if (i == 7)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
107 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
108 put_bits(&s->pb, 3, 7);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
109 put_bits(&s->pb, 12, s->frame_width);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
110 put_bits(&s->pb, 12, s->frame_height);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
111 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
112 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
113
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
114 /* no checksum or extra data (next 2 bits get 0) */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
115 put_bits(&s->pb, 2, 0);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
116 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
117
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
118
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
119 #define QUALITY_THRESHOLD 100
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
120 #define THRESHOLD_MULTIPLIER 0.6
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
121
8590
7a463923ecd1 Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurel
parents: 8211
diff changeset
122 #if HAVE_ALTIVEC
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
123 #undef vector
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
124 #endif
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
125
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
126 static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref, uint8_t *decoded, int stride, int level, int threshold, int lambda, int intra){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
127 int count, y, x, i, j, split, best_mean, best_score, best_count;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
128 int best_vector[6];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
129 int block_sum[7]= {0, 0, 0, 0, 0, 0};
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
130 int w= 2<<((level+2)>>1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
131 int h= 2<<((level+1)>>1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
132 int size=w*h;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
133 int16_t block[7][256];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
134 const int8_t *codebook_sum, *codebook;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
135 const uint16_t (*mean_vlc)[2];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
136 const uint8_t (*multistage_vlc)[2];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
137
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
138 best_score=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
139 //FIXME optimize, this doenst need to be done multiple times
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
140 if(intra){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
141 codebook_sum= svq1_intra_codebook_sum[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
142 codebook= ff_svq1_intra_codebooks[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
143 mean_vlc= ff_svq1_intra_mean_vlc;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
144 multistage_vlc= ff_svq1_intra_multistage_vlc[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
145 for(y=0; y<h; y++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
146 for(x=0; x<w; x++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
147 int v= src[x + y*stride];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
148 block[0][x + w*y]= v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
149 best_score += v*v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
150 block_sum[0] += v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
151 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
152 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
153 }else{
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
154 codebook_sum= svq1_inter_codebook_sum[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
155 codebook= ff_svq1_inter_codebooks[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
156 mean_vlc= ff_svq1_inter_mean_vlc + 256;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
157 multistage_vlc= ff_svq1_inter_multistage_vlc[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
158 for(y=0; y<h; y++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
159 for(x=0; x<w; x++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
160 int v= src[x + y*stride] - ref[x + y*stride];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
161 block[0][x + w*y]= v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
162 best_score += v*v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
163 block_sum[0] += v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
164 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
165 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
166 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
167
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
168 best_count=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
169 best_score -= ((block_sum[0]*block_sum[0])>>(level+3));
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
170 best_mean= (block_sum[0] + (size>>1)) >> (level+3);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
171
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
172 if(level<4){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
173 for(count=1; count<7; count++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
174 int best_vector_score= INT_MAX;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
175 int best_vector_sum=-999, best_vector_mean=-999;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
176 const int stage= count-1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
177 const int8_t *vector;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
178
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
179 for(i=0; i<16; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
180 int sum= codebook_sum[stage*16 + i];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
181 int sqr, diff, score;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
182
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
183 vector = codebook + stage*size*16 + i*size;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
184 sqr = s->dsp.ssd_int8_vs_int16(vector, block[stage], size);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
185 diff= block_sum[stage] - sum;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
186 score= sqr - ((diff*(int64_t)diff)>>(level+3)); //FIXME 64bit slooow
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
187 if(score < best_vector_score){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
188 int mean= (diff + (size>>1)) >> (level+3);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
189 assert(mean >-300 && mean<300);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
190 mean= av_clip(mean, intra?0:-256, 255);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
191 best_vector_score= score;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
192 best_vector[stage]= i;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
193 best_vector_sum= sum;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
194 best_vector_mean= mean;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
195 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
196 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
197 assert(best_vector_mean != -999);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
198 vector= codebook + stage*size*16 + best_vector[stage]*size;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
199 for(j=0; j<size; j++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
200 block[stage+1][j] = block[stage][j] - vector[j];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
201 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
202 block_sum[stage+1]= block_sum[stage] - best_vector_sum;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
203 best_vector_score +=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
204 lambda*(+ 1 + 4*count
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
205 + multistage_vlc[1+count][1]
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
206 + mean_vlc[best_vector_mean][1]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
207
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
208 if(best_vector_score < best_score){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
209 best_score= best_vector_score;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
210 best_count= count;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
211 best_mean= best_vector_mean;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
212 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
213 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
214 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
215
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
216 split=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
217 if(best_score > threshold && level){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
218 int score=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
219 int offset= (level&1) ? stride*h/2 : w/2;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
220 PutBitContext backup[6];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
221
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
222 for(i=level-1; i>=0; i--){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
223 backup[i]= s->reorder_pb[i];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
224 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
225 score += encode_block(s, src , ref , decoded , stride, level-1, threshold>>1, lambda, intra);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
226 score += encode_block(s, src + offset, ref + offset, decoded + offset, stride, level-1, threshold>>1, lambda, intra);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
227 score += lambda;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
228
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
229 if(score < best_score){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
230 best_score= score;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
231 split=1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
232 }else{
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
233 for(i=level-1; i>=0; i--){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
234 s->reorder_pb[i]= backup[i];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
235 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
236 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
237 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
238 if (level > 0)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
239 put_bits(&s->reorder_pb[level], 1, split);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
240
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
241 if(!split){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
242 assert((best_mean >= 0 && best_mean<256) || !intra);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
243 assert(best_mean >= -256 && best_mean<256);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
244 assert(best_count >=0 && best_count<7);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
245 assert(level<4 || best_count==0);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
246
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
247 /* output the encoding */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
248 put_bits(&s->reorder_pb[level],
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
249 multistage_vlc[1 + best_count][1],
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
250 multistage_vlc[1 + best_count][0]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
251 put_bits(&s->reorder_pb[level], mean_vlc[best_mean][1],
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
252 mean_vlc[best_mean][0]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
253
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
254 for (i = 0; i < best_count; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
255 assert(best_vector[i]>=0 && best_vector[i]<16);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
256 put_bits(&s->reorder_pb[level], 4, best_vector[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
257 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
258
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
259 for(y=0; y<h; y++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
260 for(x=0; x<w; x++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
261 decoded[x + y*stride]= src[x + y*stride] - block[best_count][x + w*y] + best_mean;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
262 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
263 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
264 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
265
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
266 return best_score;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
267 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
268
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
269
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
270 static int svq1_encode_plane(SVQ1Context *s, int plane, unsigned char *src_plane, unsigned char *ref_plane, unsigned char *decoded_plane,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
271 int width, int height, int src_stride, int stride)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
272 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
273 int x, y;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
274 int i;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
275 int block_width, block_height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
276 int level;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
277 int threshold[6];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
278 const int lambda= (s->picture.quality*s->picture.quality) >> (2*FF_LAMBDA_SHIFT);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
279
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
280 /* figure out the acceptable level thresholds in advance */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
281 threshold[5] = QUALITY_THRESHOLD;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
282 for (level = 4; level >= 0; level--)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
283 threshold[level] = threshold[level + 1] * THRESHOLD_MULTIPLIER;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
284
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
285 block_width = (width + 15) / 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
286 block_height = (height + 15) / 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
287
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
288 if(s->picture.pict_type == FF_P_TYPE){
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
289 s->m.avctx= s->avctx;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
290 s->m.current_picture_ptr= &s->m.current_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
291 s->m.last_picture_ptr = &s->m.last_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
292 s->m.last_picture.data[0]= ref_plane;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
293 s->m.linesize=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
294 s->m.last_picture.linesize[0]=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
295 s->m.new_picture.linesize[0]=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
296 s->m.current_picture.linesize[0]= stride;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
297 s->m.width= width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
298 s->m.height= height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
299 s->m.mb_width= block_width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
300 s->m.mb_height= block_height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
301 s->m.mb_stride= s->m.mb_width+1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
302 s->m.b8_stride= 2*s->m.mb_width+1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
303 s->m.f_code=1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
304 s->m.pict_type= s->picture.pict_type;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
305 s->m.me_method= s->avctx->me_method;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
306 s->m.me.scene_change_score=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
307 s->m.flags= s->avctx->flags;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
308 // s->m.out_format = FMT_H263;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
309 // s->m.unrestricted_mv= 1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
310
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
311 s->m.lambda= s->picture.quality;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
312 s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
313 s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
314
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
315 if(!s->motion_val8[plane]){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
316 s->motion_val8 [plane]= av_mallocz((s->m.b8_stride*block_height*2 + 2)*2*sizeof(int16_t));
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
317 s->motion_val16[plane]= av_mallocz((s->m.mb_stride*(block_height + 2) + 1)*2*sizeof(int16_t));
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
318 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
319
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
320 s->m.mb_type= s->mb_type;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
321
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
322 //dummies, to avoid segfaults
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
323 s->m.current_picture.mb_mean= (uint8_t *)s->dummy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
324 s->m.current_picture.mb_var= (uint16_t*)s->dummy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
325 s->m.current_picture.mc_mb_var= (uint16_t*)s->dummy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
326 s->m.current_picture.mb_type= s->dummy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
327
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
328 s->m.current_picture.motion_val[0]= s->motion_val8[plane] + 2;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
329 s->m.p_mv_table= s->motion_val16[plane] + s->m.mb_stride + 1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
330 s->m.dsp= s->dsp; //move
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
331 ff_init_me(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
332
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
333 s->m.me.dia_size= s->avctx->dia_size;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
334 s->m.first_slice_line=1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
335 for (y = 0; y < block_height; y++) {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
336 uint8_t src[stride*16];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
337
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
338 s->m.new_picture.data[0]= src - y*16*stride; //ugly
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
339 s->m.mb_y= y;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
340
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
341 for(i=0; i<16 && i + 16*y<height; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
342 memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
343 for(x=width; x<16*block_width; x++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
344 src[i*stride+x]= src[i*stride+x-1];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
345 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
346 for(; i<16 && i + 16*y<16*block_height; i++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
347 memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
348
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
349 for (x = 0; x < block_width; x++) {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
350 s->m.mb_x= x;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
351 ff_init_block_index(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
352 ff_update_block_index(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
353
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
354 ff_estimate_p_frame_motion(&s->m, x, y);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
355 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
356 s->m.first_slice_line=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
357 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
358
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
359 ff_fix_long_p_mvs(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
360 ff_fix_long_mvs(&s->m, NULL, 0, s->m.p_mv_table, s->m.f_code, CANDIDATE_MB_TYPE_INTER, 0);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
361 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
362
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
363 s->m.first_slice_line=1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
364 for (y = 0; y < block_height; y++) {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
365 uint8_t src[stride*16];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
366
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
367 for(i=0; i<16 && i + 16*y<height; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
368 memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
369 for(x=width; x<16*block_width; x++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
370 src[i*stride+x]= src[i*stride+x-1];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
371 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
372 for(; i<16 && i + 16*y<16*block_height; i++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
373 memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
374
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
375 s->m.mb_y= y;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
376 for (x = 0; x < block_width; x++) {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
377 uint8_t reorder_buffer[3][6][7*32];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
378 int count[3][6];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
379 int offset = y * 16 * stride + x * 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
380 uint8_t *decoded= decoded_plane + offset;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
381 uint8_t *ref= ref_plane + offset;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
382 int score[4]={0,0,0,0}, best;
8211
b9e82845e7eb svq1enc: move scratch buffer from stack to context to ensure alignment
mru
parents: 7040
diff changeset
383 uint8_t *temp = s->scratchbuf;
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
384
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
385 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 3000){ //FIXME check size
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
386 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
387 return -1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
388 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
389
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
390 s->m.mb_x= x;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
391 ff_init_block_index(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
392 ff_update_block_index(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
393
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
394 if(s->picture.pict_type == FF_I_TYPE || (s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTRA)){
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
395 for(i=0; i<6; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
396 init_put_bits(&s->reorder_pb[i], reorder_buffer[0][i], 7*32);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
397 }
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
398 if(s->picture.pict_type == FF_P_TYPE){
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
399 const uint8_t *vlc= ff_svq1_block_type_vlc[SVQ1_BLOCK_INTRA];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
400 put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
401 score[0]= vlc[1]*lambda;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
402 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
403 score[0]+= encode_block(s, src+16*x, NULL, temp, stride, 5, 64, lambda, 1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
404 for(i=0; i<6; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
405 count[0][i]= put_bits_count(&s->reorder_pb[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
406 flush_put_bits(&s->reorder_pb[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
407 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
408 }else
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
409 score[0]= INT_MAX;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
410
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
411 best=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
412
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
413 if(s->picture.pict_type == FF_P_TYPE){
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
414 const uint8_t *vlc= ff_svq1_block_type_vlc[SVQ1_BLOCK_INTER];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
415 int mx, my, pred_x, pred_y, dxy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
416 int16_t *motion_ptr;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
417
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
418 motion_ptr= h263_pred_motion(&s->m, 0, 0, &pred_x, &pred_y);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
419 if(s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTER){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
420 for(i=0; i<6; i++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
421 init_put_bits(&s->reorder_pb[i], reorder_buffer[1][i], 7*32);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
422
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
423 put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
424
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
425 s->m.pb= s->reorder_pb[5];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
426 mx= motion_ptr[0];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
427 my= motion_ptr[1];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
428 assert(mx>=-32 && mx<=31);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
429 assert(my>=-32 && my<=31);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
430 assert(pred_x>=-32 && pred_x<=31);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
431 assert(pred_y>=-32 && pred_y<=31);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
432 ff_h263_encode_motion(&s->m, mx - pred_x, 1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
433 ff_h263_encode_motion(&s->m, my - pred_y, 1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
434 s->reorder_pb[5]= s->m.pb;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
435 score[1] += lambda*put_bits_count(&s->reorder_pb[5]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
436
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
437 dxy= (mx&1) + 2*(my&1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
438
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
439 s->dsp.put_pixels_tab[0][dxy](temp+16, ref + (mx>>1) + stride*(my>>1), stride, 16);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
440
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
441 score[1]+= encode_block(s, src+16*x, temp+16, decoded, stride, 5, 64, lambda, 0);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
442 best= score[1] <= score[0];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
443
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
444 vlc= ff_svq1_block_type_vlc[SVQ1_BLOCK_SKIP];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
445 score[2]= s->dsp.sse[0](NULL, src+16*x, ref, stride, 16);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
446 score[2]+= vlc[1]*lambda;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
447 if(score[2] < score[best] && mx==0 && my==0){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
448 best=2;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
449 s->dsp.put_pixels_tab[0][0](decoded, ref, stride, 16);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
450 for(i=0; i<6; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
451 count[2][i]=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
452 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
453 put_bits(&s->pb, vlc[1], vlc[0]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
454 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
455 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
456
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
457 if(best==1){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
458 for(i=0; i<6; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
459 count[1][i]= put_bits_count(&s->reorder_pb[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
460 flush_put_bits(&s->reorder_pb[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
461 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
462 }else{
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
463 motion_ptr[0 ] = motion_ptr[1 ]=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
464 motion_ptr[2 ] = motion_ptr[3 ]=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
465 motion_ptr[0+2*s->m.b8_stride] = motion_ptr[1+2*s->m.b8_stride]=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
466 motion_ptr[2+2*s->m.b8_stride] = motion_ptr[3+2*s->m.b8_stride]=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
467 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
468 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
469
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
470 s->rd_total += score[best];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
471
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
472 for(i=5; i>=0; i--){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
473 ff_copy_bits(&s->pb, reorder_buffer[best][i], count[best][i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
474 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
475 if(best==0){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
476 s->dsp.put_pixels_tab[0][0](decoded, temp, stride, 16);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
477 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
478 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
479 s->m.first_slice_line=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
480 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
481 return 0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
482 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
483
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6481
diff changeset
484 static av_cold int svq1_encode_init(AVCodecContext *avctx)
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
485 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
486 SVQ1Context * const s = avctx->priv_data;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
487
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
488 dsputil_init(&s->dsp, avctx);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
489 avctx->coded_frame= (AVFrame*)&s->picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
490
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
491 s->frame_width = avctx->width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
492 s->frame_height = avctx->height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
493
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
494 s->y_block_width = (s->frame_width + 15) / 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
495 s->y_block_height = (s->frame_height + 15) / 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
496
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
497 s->c_block_width = (s->frame_width / 4 + 15) / 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
498 s->c_block_height = (s->frame_height / 4 + 15) / 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
499
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
500 s->avctx= avctx;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
501 s->m.avctx= avctx;
10042
37485479bb6e fix a crash in SVQ1 with cmp!=sad
lorenm
parents: 8718
diff changeset
502 s->m.me.temp =
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
503 s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
504 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
505 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
506 s->mb_type = av_mallocz((s->y_block_width+1)*s->y_block_height*sizeof(int16_t));
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
507 s->dummy = av_mallocz((s->y_block_width+1)*s->y_block_height*sizeof(int32_t));
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
508 h263_encode_init(&s->m); //mv_penalty
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
509
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
510 return 0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
511 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
512
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
513 static int svq1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
514 int buf_size, void *data)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
515 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
516 SVQ1Context * const s = avctx->priv_data;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
517 AVFrame *pict = data;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
518 AVFrame * const p= (AVFrame*)&s->picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
519 AVFrame temp;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
520 int i;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
521
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
522 if(avctx->pix_fmt != PIX_FMT_YUV410P){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
523 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
524 return -1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
525 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
526
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
527 if(!s->current_picture.data[0]){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
528 avctx->get_buffer(avctx, &s->current_picture);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
529 avctx->get_buffer(avctx, &s->last_picture);
8211
b9e82845e7eb svq1enc: move scratch buffer from stack to context to ensure alignment
mru
parents: 7040
diff changeset
530 s->scratchbuf = av_malloc(s->current_picture.linesize[0] * 16);
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
531 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
532
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
533 temp= s->current_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
534 s->current_picture= s->last_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
535 s->last_picture= temp;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
536
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
537 init_put_bits(&s->pb, buf, buf_size);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
538
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
539 *p = *pict;
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
540 p->pict_type = avctx->gop_size && avctx->frame_number % avctx->gop_size ? FF_P_TYPE : FF_I_TYPE;
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
541 p->key_frame = p->pict_type == FF_I_TYPE;
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
542
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
543 svq1_write_header(s, p->pict_type);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
544 for(i=0; i<3; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
545 if(svq1_encode_plane(s, i,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
546 s->picture.data[i], s->last_picture.data[i], s->current_picture.data[i],
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
547 s->frame_width / (i?4:1), s->frame_height / (i?4:1),
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
548 s->picture.linesize[i], s->current_picture.linesize[i]) < 0)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
549 return -1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
550 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
551
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
552 // align_put_bits(&s->pb);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
553 while(put_bits_count(&s->pb) & 31)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
554 put_bits(&s->pb, 1, 0);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
555
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
556 flush_put_bits(&s->pb);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
557
6750
c93570aeb3eb Remove unnecessary parentheses from return calls.
diego
parents: 6712
diff changeset
558 return put_bits_count(&s->pb) / 8;
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
559 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
560
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6481
diff changeset
561 static av_cold int svq1_encode_end(AVCodecContext *avctx)
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
562 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
563 SVQ1Context * const s = avctx->priv_data;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
564 int i;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
565
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
566 av_log(avctx, AV_LOG_DEBUG, "RD: %f\n", s->rd_total/(double)(avctx->width*avctx->height*avctx->frame_number));
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
567
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
568 av_freep(&s->m.me.scratchpad);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
569 av_freep(&s->m.me.map);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
570 av_freep(&s->m.me.score_map);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
571 av_freep(&s->mb_type);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
572 av_freep(&s->dummy);
8211
b9e82845e7eb svq1enc: move scratch buffer from stack to context to ensure alignment
mru
parents: 7040
diff changeset
573 av_freep(&s->scratchbuf);
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
574
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
575 for(i=0; i<3; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
576 av_freep(&s->motion_val8[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
577 av_freep(&s->motion_val16[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
578 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
579
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
580 return 0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
581 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
582
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
583
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
584 AVCodec svq1_encoder = {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
585 "svq1",
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
586 CODEC_TYPE_VIDEO,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
587 CODEC_ID_SVQ1,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
588 sizeof(SVQ1Context),
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
589 svq1_encode_init,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
590 svq1_encode_frame,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
591 svq1_encode_end,
10146
38cfe222e1a4 Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents: 10042
diff changeset
592 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV410P, PIX_FMT_NONE},
10441
0a6fc654cfb6 Add additional long names for the Sorenson Vector Quantizer 1 decoder
stefano
parents: 10146
diff changeset
593 .long_name= NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
594 };