annotate svq1enc.c @ 12530:63edd10ad4bc libavcodec tip

Try to fix crashes introduced by r25218 r25218 made assumptions about the existence of past reference frames that weren't necessarily true.
author darkshikari
date Tue, 28 Sep 2010 09:06:22 +0000
parents 7dd2a45249a9
children
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 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11560
diff changeset
23 * @file
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"
10828
d0657e337f91 Split H263 encoder and decoder from common code.
michael
parents: 10441
diff changeset
33 #include "h263.h"
10843
f6afc7837f83 Add missing internal.h to files calling ff_match_2uint16().
astrange
parents: 10832
diff changeset
34 #include "internal.h"
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
35
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
36 #include "svq1.h"
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
37 #include "svq1enc_cb.h"
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
38
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
39 #undef NDEBUG
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
40 #include <assert.h>
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
41
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
42
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
43 typedef struct SVQ1Context {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
44 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
45 AVCodecContext *avctx;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
46 DSPContext dsp;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
47 AVFrame picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
48 AVFrame current_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
49 AVFrame last_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
50 PutBitContext pb;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
51 GetBitContext gb;
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 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
54
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
55 int frame_width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
56 int frame_height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
57
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
58 /* Y plane block dimensions */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
59 int y_block_width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
60 int y_block_height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
61
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
62 /* U & V plane (C planes) block dimensions */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
63 int c_block_width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
64 int c_block_height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
65
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
66 uint16_t *mb_type;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
67 uint32_t *dummy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
68 int16_t (*motion_val8[3])[2];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
69 int16_t (*motion_val16[3])[2];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
70
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
71 int64_t rd_total;
8211
b9e82845e7eb svq1enc: move scratch buffer from stack to context to ensure alignment
mru
parents: 7040
diff changeset
72
b9e82845e7eb svq1enc: move scratch buffer from stack to context to ensure alignment
mru
parents: 7040
diff changeset
73 uint8_t *scratchbuf;
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
74 } SVQ1Context;
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 static void svq1_write_header(SVQ1Context *s, int frame_type)
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 int i;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
79
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
80 /* frame code */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
81 put_bits(&s->pb, 22, 0x20);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
82
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
83 /* temporal reference (sure hope this is a "don't care") */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
84 put_bits(&s->pb, 8, 0x00);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
85
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
86 /* frame type */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
87 put_bits(&s->pb, 2, frame_type - 1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
88
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
89 if (frame_type == FF_I_TYPE) {
5233
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 checksum since frame code is 0x20 */
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 /* no embedded string either */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
94
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
95 /* output 5 unknown bits (2 + 2 + 1) */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
96 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
97
10832
f20726a6d538 Add a function to match a 2 element vector of uint16_t and use it in h263 and svq1
michael
parents: 10828
diff changeset
98 i= ff_match_2uint16(ff_svq1_frame_size_table, FF_ARRAY_ELEMS(ff_svq1_frame_size_table), s->frame_width, s->frame_height);
f20726a6d538 Add a function to match a 2 element vector of uint16_t and use it in h263 and svq1
michael
parents: 10828
diff changeset
99 put_bits(&s->pb, 3, i);
5233
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 if (i == 7)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
102 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
103 put_bits(&s->pb, 12, s->frame_width);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
104 put_bits(&s->pb, 12, s->frame_height);
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 }
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 /* no checksum or extra data (next 2 bits get 0) */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
109 put_bits(&s->pb, 2, 0);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
110 }
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 #define QUALITY_THRESHOLD 100
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
114 #define THRESHOLD_MULTIPLIER 0.6
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
115
8590
7a463923ecd1 Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurel
parents: 8211
diff changeset
116 #if HAVE_ALTIVEC
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
117 #undef vector
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
118 #endif
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
119
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
120 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
121 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
122 int best_vector[6];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
123 int block_sum[7]= {0, 0, 0, 0, 0, 0};
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
124 int w= 2<<((level+2)>>1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
125 int h= 2<<((level+1)>>1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
126 int size=w*h;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
127 int16_t block[7][256];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
128 const int8_t *codebook_sum, *codebook;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
129 const uint16_t (*mean_vlc)[2];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
130 const uint8_t (*multistage_vlc)[2];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
131
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
132 best_score=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
133 //FIXME optimize, this doenst need to be done multiple times
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
134 if(intra){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
135 codebook_sum= svq1_intra_codebook_sum[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
136 codebook= ff_svq1_intra_codebooks[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
137 mean_vlc= ff_svq1_intra_mean_vlc;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
138 multistage_vlc= ff_svq1_intra_multistage_vlc[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
139 for(y=0; y<h; y++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
140 for(x=0; x<w; x++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
141 int v= src[x + y*stride];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
142 block[0][x + w*y]= v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
143 best_score += v*v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
144 block_sum[0] += v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
145 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
146 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
147 }else{
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
148 codebook_sum= svq1_inter_codebook_sum[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
149 codebook= ff_svq1_inter_codebooks[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
150 mean_vlc= ff_svq1_inter_mean_vlc + 256;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
151 multistage_vlc= ff_svq1_inter_multistage_vlc[level];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
152 for(y=0; y<h; y++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
153 for(x=0; x<w; x++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
154 int v= src[x + y*stride] - ref[x + y*stride];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
155 block[0][x + w*y]= v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
156 best_score += v*v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
157 block_sum[0] += v;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
158 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
159 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
160 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
161
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
162 best_count=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
163 best_score -= ((block_sum[0]*block_sum[0])>>(level+3));
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
164 best_mean= (block_sum[0] + (size>>1)) >> (level+3);
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 if(level<4){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
167 for(count=1; count<7; count++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
168 int best_vector_score= INT_MAX;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
169 int best_vector_sum=-999, best_vector_mean=-999;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
170 const int stage= count-1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
171 const int8_t *vector;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
172
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
173 for(i=0; i<16; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
174 int sum= codebook_sum[stage*16 + i];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
175 int sqr, diff, score;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
176
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
177 vector = codebook + stage*size*16 + i*size;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
178 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
179 diff= block_sum[stage] - sum;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
180 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
181 if(score < best_vector_score){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
182 int mean= (diff + (size>>1)) >> (level+3);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
183 assert(mean >-300 && mean<300);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
184 mean= av_clip(mean, intra?0:-256, 255);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
185 best_vector_score= score;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
186 best_vector[stage]= i;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
187 best_vector_sum= sum;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
188 best_vector_mean= mean;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
189 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
190 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
191 assert(best_vector_mean != -999);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
192 vector= codebook + stage*size*16 + best_vector[stage]*size;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
193 for(j=0; j<size; j++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
194 block[stage+1][j] = block[stage][j] - vector[j];
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 block_sum[stage+1]= block_sum[stage] - best_vector_sum;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
197 best_vector_score +=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
198 lambda*(+ 1 + 4*count
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
199 + multistage_vlc[1+count][1]
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
200 + mean_vlc[best_vector_mean][1]);
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 if(best_vector_score < best_score){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
203 best_score= best_vector_score;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
204 best_count= count;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
205 best_mean= best_vector_mean;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
206 }
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 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
209
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
210 split=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
211 if(best_score > threshold && level){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
212 int score=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
213 int offset= (level&1) ? stride*h/2 : w/2;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
214 PutBitContext backup[6];
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 for(i=level-1; i>=0; i--){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
217 backup[i]= s->reorder_pb[i];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
218 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
219 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
220 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
221 score += lambda;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
222
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
223 if(score < best_score){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
224 best_score= score;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
225 split=1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
226 }else{
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
227 for(i=level-1; i>=0; i--){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
228 s->reorder_pb[i]= backup[i];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
229 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
230 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
231 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
232 if (level > 0)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
233 put_bits(&s->reorder_pb[level], 1, split);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
234
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
235 if(!split){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
236 assert((best_mean >= 0 && best_mean<256) || !intra);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
237 assert(best_mean >= -256 && best_mean<256);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
238 assert(best_count >=0 && best_count<7);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
239 assert(level<4 || best_count==0);
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 /* output the encoding */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
242 put_bits(&s->reorder_pb[level],
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
243 multistage_vlc[1 + best_count][1],
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
244 multistage_vlc[1 + best_count][0]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
245 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
246 mean_vlc[best_mean][0]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
247
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
248 for (i = 0; i < best_count; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
249 assert(best_vector[i]>=0 && best_vector[i]<16);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
250 put_bits(&s->reorder_pb[level], 4, best_vector[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
251 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
252
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
253 for(y=0; y<h; y++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
254 for(x=0; x<w; x++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
255 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
256 }
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
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
260 return best_score;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
261 }
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 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
265 int width, int height, int src_stride, int stride)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
266 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
267 int x, y;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
268 int i;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
269 int block_width, block_height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
270 int level;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
271 int threshold[6];
10855
423410fc29b1 Reduce stack usage in svq1_encode_plane(). Reuse context scratch buffer
zuxy
parents: 10843
diff changeset
272 uint8_t *src = s->scratchbuf + stride * 16;
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
273 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
274
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
275 /* figure out the acceptable level thresholds in advance */
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
276 threshold[5] = QUALITY_THRESHOLD;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
277 for (level = 4; level >= 0; level--)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
278 threshold[level] = threshold[level + 1] * THRESHOLD_MULTIPLIER;
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 block_width = (width + 15) / 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
281 block_height = (height + 15) / 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
282
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
283 if(s->picture.pict_type == FF_P_TYPE){
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
284 s->m.avctx= s->avctx;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
285 s->m.current_picture_ptr= &s->m.current_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
286 s->m.last_picture_ptr = &s->m.last_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
287 s->m.last_picture.data[0]= ref_plane;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
288 s->m.linesize=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
289 s->m.last_picture.linesize[0]=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
290 s->m.new_picture.linesize[0]=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
291 s->m.current_picture.linesize[0]= stride;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
292 s->m.width= width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
293 s->m.height= height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
294 s->m.mb_width= block_width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
295 s->m.mb_height= block_height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
296 s->m.mb_stride= s->m.mb_width+1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
297 s->m.b8_stride= 2*s->m.mb_width+1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
298 s->m.f_code=1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
299 s->m.pict_type= s->picture.pict_type;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
300 s->m.me_method= s->avctx->me_method;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
301 s->m.me.scene_change_score=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
302 s->m.flags= s->avctx->flags;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
303 // s->m.out_format = FMT_H263;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
304 // s->m.unrestricted_mv= 1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
305
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
306 s->m.lambda= s->picture.quality;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
307 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
308 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
309
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
310 if(!s->motion_val8[plane]){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
311 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
312 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
313 }
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 s->m.mb_type= s->mb_type;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
316
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
317 //dummies, to avoid segfaults
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
318 s->m.current_picture.mb_mean= (uint8_t *)s->dummy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
319 s->m.current_picture.mb_var= (uint16_t*)s->dummy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
320 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
321 s->m.current_picture.mb_type= s->dummy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
322
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
323 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
324 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
325 s->m.dsp= s->dsp; //move
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
326 ff_init_me(&s->m);
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.me.dia_size= s->avctx->dia_size;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
329 s->m.first_slice_line=1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
330 for (y = 0; y < block_height; y++) {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
331 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
332 s->m.mb_y= y;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
333
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
334 for(i=0; i<16 && i + 16*y<height; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
335 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
336 for(x=width; x<16*block_width; x++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
337 src[i*stride+x]= src[i*stride+x-1];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
338 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
339 for(; i<16 && i + 16*y<16*block_height; i++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
340 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
341
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
342 for (x = 0; x < block_width; x++) {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
343 s->m.mb_x= x;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
344 ff_init_block_index(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
345 ff_update_block_index(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
346
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
347 ff_estimate_p_frame_motion(&s->m, x, y);
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 s->m.first_slice_line=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
350 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
351
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
352 ff_fix_long_p_mvs(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
353 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
354 }
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=1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
357 for (y = 0; y < block_height; y++) {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
358 for(i=0; i<16 && i + 16*y<height; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
359 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
360 for(x=width; x<16*block_width; x++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
361 src[i*stride+x]= src[i*stride+x-1];
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 for(; i<16 && i + 16*y<16*block_height; i++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
364 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
365
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
366 s->m.mb_y= y;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
367 for (x = 0; x < block_width; x++) {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
368 uint8_t reorder_buffer[3][6][7*32];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
369 int count[3][6];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
370 int offset = y * 16 * stride + x * 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
371 uint8_t *decoded= decoded_plane + offset;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
372 uint8_t *ref= ref_plane + offset;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
373 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
374 uint8_t *temp = s->scratchbuf;
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
375
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
376 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
377 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
378 return -1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
379 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
380
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
381 s->m.mb_x= x;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
382 ff_init_block_index(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
383 ff_update_block_index(&s->m);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
384
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
385 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
386 for(i=0; i<6; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
387 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
388 }
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
389 if(s->picture.pict_type == FF_P_TYPE){
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
390 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
391 put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
392 score[0]= vlc[1]*lambda;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
393 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
394 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
395 for(i=0; i<6; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
396 count[0][i]= put_bits_count(&s->reorder_pb[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
397 flush_put_bits(&s->reorder_pb[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
398 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
399 }else
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
400 score[0]= INT_MAX;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
401
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
402 best=0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
403
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
404 if(s->picture.pict_type == FF_P_TYPE){
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
405 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
406 int mx, my, pred_x, pred_y, dxy;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
407 int16_t *motion_ptr;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
408
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
409 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
410 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
411 for(i=0; i<6; i++)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
412 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
413
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
414 put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
415
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
416 s->m.pb= s->reorder_pb[5];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
417 mx= motion_ptr[0];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
418 my= motion_ptr[1];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
419 assert(mx>=-32 && mx<=31);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
420 assert(my>=-32 && my<=31);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
421 assert(pred_x>=-32 && pred_x<=31);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
422 assert(pred_y>=-32 && pred_y<=31);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
423 ff_h263_encode_motion(&s->m, mx - pred_x, 1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
424 ff_h263_encode_motion(&s->m, my - pred_y, 1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
425 s->reorder_pb[5]= s->m.pb;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
426 score[1] += lambda*put_bits_count(&s->reorder_pb[5]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
427
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
428 dxy= (mx&1) + 2*(my&1);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
429
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
430 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
431
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
432 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
433 best= score[1] <= score[0];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
434
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
435 vlc= ff_svq1_block_type_vlc[SVQ1_BLOCK_SKIP];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
436 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
437 score[2]+= vlc[1]*lambda;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
438 if(score[2] < score[best] && mx==0 && my==0){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
439 best=2;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
440 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
441 for(i=0; i<6; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
442 count[2][i]=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 put_bits(&s->pb, vlc[1], vlc[0]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
445 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
446 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
447
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
448 if(best==1){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
449 for(i=0; i<6; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
450 count[1][i]= put_bits_count(&s->reorder_pb[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
451 flush_put_bits(&s->reorder_pb[i]);
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 }else{
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
454 motion_ptr[0 ] = motion_ptr[1 ]=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
455 motion_ptr[2 ] = motion_ptr[3 ]=
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
456 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
457 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
458 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
459 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
460
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
461 s->rd_total += score[best];
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
462
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
463 for(i=5; i>=0; i--){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
464 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
465 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
466 if(best==0){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
467 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
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->m.first_slice_line=0;
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 return 0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
473 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
474
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6481
diff changeset
475 static av_cold int svq1_encode_init(AVCodecContext *avctx)
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
476 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
477 SVQ1Context * const s = avctx->priv_data;
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 dsputil_init(&s->dsp, avctx);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
480 avctx->coded_frame= (AVFrame*)&s->picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
481
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
482 s->frame_width = avctx->width;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
483 s->frame_height = avctx->height;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
484
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
485 s->y_block_width = (s->frame_width + 15) / 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
486 s->y_block_height = (s->frame_height + 15) / 16;
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 s->c_block_width = (s->frame_width / 4 + 15) / 16;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
489 s->c_block_height = (s->frame_height / 4 + 15) / 16;
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->avctx= avctx;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
492 s->m.avctx= avctx;
10042
37485479bb6e fix a crash in SVQ1 with cmp!=sad
lorenm
parents: 8718
diff changeset
493 s->m.me.temp =
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
494 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
495 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
496 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
497 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
498 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
499 h263_encode_init(&s->m); //mv_penalty
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
500
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
501 return 0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
502 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
503
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
504 static int svq1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
505 int buf_size, void *data)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
506 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
507 SVQ1Context * const s = avctx->priv_data;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
508 AVFrame *pict = data;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
509 AVFrame * const p= (AVFrame*)&s->picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
510 AVFrame temp;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
511 int i;
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 if(avctx->pix_fmt != PIX_FMT_YUV410P){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
514 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
515 return -1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
516 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
517
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
518 if(!s->current_picture.data[0]){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
519 avctx->get_buffer(avctx, &s->current_picture);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
520 avctx->get_buffer(avctx, &s->last_picture);
10855
423410fc29b1 Reduce stack usage in svq1_encode_plane(). Reuse context scratch buffer
zuxy
parents: 10843
diff changeset
521 s->scratchbuf = av_malloc(s->current_picture.linesize[0] * 16 * 2);
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
522 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
523
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
524 temp= s->current_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
525 s->current_picture= s->last_picture;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
526 s->last_picture= temp;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
527
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
528 init_put_bits(&s->pb, buf, buf_size);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
529
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
530 *p = *pict;
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5233
diff changeset
531 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
532 p->key_frame = p->pict_type == FF_I_TYPE;
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
533
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
534 svq1_write_header(s, p->pict_type);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
535 for(i=0; i<3; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
536 if(svq1_encode_plane(s, i,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
537 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
538 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
539 s->picture.linesize[i], s->current_picture.linesize[i]) < 0)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
540 return -1;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
541 }
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 // align_put_bits(&s->pb);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
544 while(put_bits_count(&s->pb) & 31)
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
545 put_bits(&s->pb, 1, 0);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
546
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
547 flush_put_bits(&s->pb);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
548
6750
c93570aeb3eb Remove unnecessary parentheses from return calls.
diego
parents: 6712
diff changeset
549 return put_bits_count(&s->pb) / 8;
5233
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
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6481
diff changeset
552 static av_cold int svq1_encode_end(AVCodecContext *avctx)
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
553 {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
554 SVQ1Context * const s = avctx->priv_data;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
555 int i;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
556
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
557 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
558
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
559 av_freep(&s->m.me.scratchpad);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
560 av_freep(&s->m.me.map);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
561 av_freep(&s->m.me.score_map);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
562 av_freep(&s->mb_type);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
563 av_freep(&s->dummy);
8211
b9e82845e7eb svq1enc: move scratch buffer from stack to context to ensure alignment
mru
parents: 7040
diff changeset
564 av_freep(&s->scratchbuf);
5233
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 for(i=0; i<3; i++){
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
567 av_freep(&s->motion_val8[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
568 av_freep(&s->motion_val16[i]);
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
569 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
570
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
571 return 0;
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
572 }
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
573
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 AVCodec svq1_encoder = {
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
576 "svq1",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 10855
diff changeset
577 AVMEDIA_TYPE_VIDEO,
5233
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
578 CODEC_ID_SVQ1,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
579 sizeof(SVQ1Context),
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
580 svq1_encode_init,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
581 svq1_encode_frame,
eca08bfad00f split SVQ1 decoder and encoder in their own files
aurel
parents:
diff changeset
582 svq1_encode_end,
10146
38cfe222e1a4 Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents: 10042
diff changeset
583 .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
584 .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
585 };