annotate zmbvenc.c @ 10543:7ff7a34848bf libavcodec

10l trocadero: ZMBV encoder used zero score to detect whether block should be XORed with source one or not, which went wrong with new block comparing code. So track this condition explicitly.
author kostya
date Fri, 20 Nov 2009 07:22:41 +0000
parents d7ed9dcc78e3
children b9fdb6b4c2dc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
1 /*
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
2 * Zip Motion Blocks Video (ZMBV) encoder
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
3 * Copyright (c) 2006 Konstantin Shishkov
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
4 *
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
5 * This file is part of FFmpeg.
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
6 *
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
11 *
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
16 *
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
20 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
21
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
22 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 8573
diff changeset
23 * @file libavcodec/zmbvenc.c
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
24 * Zip Motion Blocks Video encoder
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
25 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
26
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
27 #include <stdio.h>
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
28 #include <stdlib.h>
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
29
8573
2acf0ae7b041 Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents: 7886
diff changeset
30 #include "libavutil/intreadwrite.h"
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
31 #include "avcodec.h"
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
32
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
33 #include <zlib.h>
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
34
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
35 #define ZMBV_KEYFRAME 1
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
36 #define ZMBV_DELTAPAL 2
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
37
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
38 #define ZMBV_BLOCK 16
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
39
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
40 /**
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
41 * Encoder context
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
42 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
43 typedef struct ZmbvEncContext {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
44 AVCodecContext *avctx;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
45 AVFrame pic;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
46
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
47 int range;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
48 uint8_t *comp_buf, *work_buf;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
49 uint8_t pal[768];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
50 uint32_t pal2[256]; //for quick comparisons
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
51 uint8_t *prev;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
52 int pstride;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
53 int comp_size;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
54 int keyint, curfrm;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
55 z_stream zstream;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
56 } ZmbvEncContext;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
57
6758
a57835341650 Improve motion estimation metric.
kostya
parents: 6713
diff changeset
58 static int score_tab[256];
a57835341650 Improve motion estimation metric.
kostya
parents: 6713
diff changeset
59
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
60 /** Block comparing function
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
61 * XXX should be optimized and moved to DSPContext
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
62 * TODO handle out of edge ME
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
63 */
10543
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
64 static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2, int bw, int bh, int *xored)
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
65 {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
66 int sum = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
67 int i, j;
6758
a57835341650 Improve motion estimation metric.
kostya
parents: 6713
diff changeset
68 uint8_t histogram[256]={0};
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
69
10543
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
70 *xored = 0;
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
71 for(j = 0; j < bh; j++){
10543
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
72 for(i = 0; i < bw; i++){
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
73 int t = src[i] ^ src2[i];
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
74 histogram[t]++;
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
75 *xored |= t;
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
76 }
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
77 src += stride;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
78 src2 += stride2;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
79 }
6758
a57835341650 Improve motion estimation metric.
kostya
parents: 6713
diff changeset
80
a57835341650 Improve motion estimation metric.
kostya
parents: 6713
diff changeset
81 for(i=1; i<256; i++)
a57835341650 Improve motion estimation metric.
kostya
parents: 6713
diff changeset
82 sum+= score_tab[histogram[i]];
a57835341650 Improve motion estimation metric.
kostya
parents: 6713
diff changeset
83
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
84 return sum;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
85 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
86
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
87 /** Motion estimation function
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
88 * TODO make better ME decisions
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
89 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
90 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev, int pstride,
10543
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
91 int x, int y, int *mx, int *my, int *xored)
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
92 {
4647
255affa5bae7 Correctly ME border blocks
kostya
parents: 4380
diff changeset
93 int dx, dy, tx, ty, tv, bv, bw, bh;
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
94
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
95 *mx = *my = 0;
4647
255affa5bae7 Correctly ME border blocks
kostya
parents: 4380
diff changeset
96 bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
255affa5bae7 Correctly ME border blocks
kostya
parents: 4380
diff changeset
97 bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
10543
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
98 bv = block_cmp(src, sstride, prev, pstride, bw, bh, xored);
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
99 if(!bv) return 0;
4647
255affa5bae7 Correctly ME border blocks
kostya
parents: 4380
diff changeset
100 for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
255affa5bae7 Correctly ME border blocks
kostya
parents: 4380
diff changeset
101 for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
102 if(tx == x && ty == y) continue; // we already tested this block
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
103 dx = tx - x;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
104 dy = ty - y;
10543
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
105 tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, bw, bh, xored);
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
106 if(tv < bv){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
107 bv = tv;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
108 *mx = dx;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
109 *my = dy;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
110 if(!bv) return 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
111 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
112 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
113 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
114 return bv;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
115 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
116
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
117 static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
118 {
4827
b3ee9a1526b0 Get rid of unnecessary pointer casts.
diego
parents: 4647
diff changeset
119 ZmbvEncContext * const c = avctx->priv_data;
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
120 AVFrame *pict = data;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
121 AVFrame * const p = &c->pic;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
122 uint8_t *src, *prev;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
123 uint32_t *palptr;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
124 int len = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
125 int keyframe, chpal;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
126 int fl;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
127 int work_size = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
128 int bw, bh;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
129 int i, j;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
130
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
131 keyframe = !c->curfrm;
4287
9900add82642 Deobfuscate expression
kostya
parents: 4285
diff changeset
132 c->curfrm++;
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
133 if(c->curfrm == c->keyint)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
134 c->curfrm = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
135 *p = *pict;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
136 p->pict_type= keyframe ? FF_I_TYPE : FF_P_TYPE;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
137 p->key_frame= keyframe;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
138 chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
139
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
140 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
141 *buf++ = fl; len++;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
142 if(keyframe){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
143 deflateReset(&c->zstream);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
144 *buf++ = 0; len++; // hi ver
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
145 *buf++ = 1; len++; // lo ver
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
146 *buf++ = 1; len++; // comp
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
147 *buf++ = 4; len++; // format - 8bpp
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
148 *buf++ = ZMBV_BLOCK; len++; // block width
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
149 *buf++ = ZMBV_BLOCK; len++; // block height
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
150 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
151 palptr = (uint32_t*)p->data[1];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
152 src = p->data[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
153 prev = c->prev;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
154 if(chpal){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
155 uint8_t tpal[3];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
156 for(i = 0; i < 256; i++){
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 4962
diff changeset
157 AV_WB24(tpal, palptr[i]);
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
158 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
159 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
160 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
161 c->pal[i * 3 + 0] = tpal[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
162 c->pal[i * 3 + 1] = tpal[1];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
163 c->pal[i * 3 + 2] = tpal[2];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
164 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
165 memcpy(c->pal2, p->data[1], 1024);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
166 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
167 if(keyframe){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
168 for(i = 0; i < 256; i++){
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 4962
diff changeset
169 AV_WB24(c->pal+(i*3), palptr[i]);
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
170 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
171 memcpy(c->work_buf, c->pal, 768);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
172 memcpy(c->pal2, p->data[1], 1024);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
173 work_size = 768;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
174 for(i = 0; i < avctx->height; i++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
175 memcpy(c->work_buf + work_size, src, avctx->width);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
176 src += p->linesize[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
177 work_size += avctx->width;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
178 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
179 }else{
10543
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
180 int x, y, bh2, bw2, xored;
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
181 uint8_t *tsrc, *tprev;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
182 uint8_t *mv;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
183 int mx, my, bv;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
184
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
185 bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
186 bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
187 mv = c->work_buf + work_size;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
188 memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
189 work_size += (bw * bh * 2 + 3) & ~3;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
190 /* for now just XOR'ing */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
191 for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
192 bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
193 for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
194 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
195
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
196 tsrc = src + x;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
197 tprev = prev + x;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
198
10543
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
199 bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
200 mv[0] = (mx << 1) | !!xored;
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
201 mv[1] = my << 1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
202 tprev += mx + my * c->pstride;
10543
7ff7a34848bf 10l trocadero: ZMBV encoder used zero score to detect whether block should be
kostya
parents: 10397
diff changeset
203 if(xored){
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
204 for(j = 0; j < bh2; j++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
205 for(i = 0; i < bw2; i++)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
206 c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
207 tsrc += p->linesize[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
208 tprev += c->pstride;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
209 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
210 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
211 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
212 src += p->linesize[0] * ZMBV_BLOCK;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
213 prev += c->pstride * ZMBV_BLOCK;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
214 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
215 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
216 /* save the previous frame */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
217 src = p->data[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
218 prev = c->prev;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
219 for(i = 0; i < avctx->height; i++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
220 memcpy(prev, src, avctx->width);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
221 prev += c->pstride;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
222 src += p->linesize[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
223 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
224
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
225 c->zstream.next_in = c->work_buf;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
226 c->zstream.avail_in = work_size;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
227 c->zstream.total_in = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
228
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
229 c->zstream.next_out = c->comp_buf;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
230 c->zstream.avail_out = c->comp_size;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
231 c->zstream.total_out = 0;
9621
5680832edb83 Remove dead nested assignment found by CSA
banan
parents: 9553
diff changeset
232 if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
233 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
234 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
235 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
236
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
237 memcpy(buf, c->comp_buf, c->zstream.total_out);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
238 return len + c->zstream.total_out;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
239 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
240
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
241
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
242 /**
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
243 * Init zmbv encoder
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
244 */
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 5215
diff changeset
245 static av_cold int encode_init(AVCodecContext *avctx)
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
246 {
4827
b3ee9a1526b0 Get rid of unnecessary pointer casts.
diego
parents: 4647
diff changeset
247 ZmbvEncContext * const c = avctx->priv_data;
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
248 int zret; // Zlib return code
6758
a57835341650 Improve motion estimation metric.
kostya
parents: 6713
diff changeset
249 int i;
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
250 int lvl = 9;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
251
6758
a57835341650 Improve motion estimation metric.
kostya
parents: 6713
diff changeset
252 for(i=1; i<256; i++)
6818
0f70462b5c21 Avoid using log2() freebsd does not support it.
michael
parents: 6788
diff changeset
253 score_tab[i]= -i * log(i/(double)(ZMBV_BLOCK*ZMBV_BLOCK)) * (256/M_LN2);
6758
a57835341650 Improve motion estimation metric.
kostya
parents: 6713
diff changeset
254
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
255 c->avctx = avctx;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
256
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
257 c->curfrm = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
258 c->keyint = avctx->keyint_min;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
259 c->range = 8;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
260 if(avctx->me_range > 0)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
261 c->range = FFMIN(avctx->me_range, 127);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
262
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
263 if(avctx->compression_level >= 0)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
264 lvl = avctx->compression_level;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
265 if(lvl < 0 || lvl > 9){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
266 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
267 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
268 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
269
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
270 // Needed if zlib unused or init aborted before deflateInit
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
271 memset(&(c->zstream), 0, sizeof(z_stream));
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
272 c->comp_size = avctx->width * avctx->height + 1024 +
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
273 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
274 if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
275 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
276 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
277 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
278 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
279 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
280 ((c->comp_size + 63) >> 6) + 11;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
281
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
282 /* Allocate compression buffer */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
283 if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
284 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
285 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
286 }
9686
bc32976d6d9d Move ALIGN macro to libavutil/common.h and use it in various places
conrad
parents: 9621
diff changeset
287 c->pstride = FFALIGN(avctx->width, 16);
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
288 if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
289 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
290 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
291 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
292
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
293 c->zstream.zalloc = Z_NULL;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
294 c->zstream.zfree = Z_NULL;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
295 c->zstream.opaque = Z_NULL;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
296 zret = deflateInit(&(c->zstream), lvl);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
297 if (zret != Z_OK) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
298 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
299 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
300 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
301
7886
90f13ad5d6e3 ZMBV encoder forgot to fill coded frame pointer
kostya
parents: 7040
diff changeset
302 avctx->coded_frame = (AVFrame*)&c->pic;
90f13ad5d6e3 ZMBV encoder forgot to fill coded frame pointer
kostya
parents: 7040
diff changeset
303
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
304 return 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
305 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
306
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
307
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
308
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
309 /**
4368
db099de6638f Fixed a typo, cosmetics.
banan
parents: 4287
diff changeset
310 * Uninit zmbv encoder
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
311 */
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 5215
diff changeset
312 static av_cold int encode_end(AVCodecContext *avctx)
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
313 {
4827
b3ee9a1526b0 Get rid of unnecessary pointer casts.
diego
parents: 4647
diff changeset
314 ZmbvEncContext * const c = avctx->priv_data;
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
315
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
316 av_freep(&c->comp_buf);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
317 av_freep(&c->work_buf);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
318
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
319 deflateEnd(&(c->zstream));
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
320 av_freep(&c->prev);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
321
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
322 return 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
323 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
324
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
325 AVCodec zmbv_encoder = {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
326 "zmbv",
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
327 CODEC_TYPE_VIDEO,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
328 CODEC_ID_ZMBV,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
329 sizeof(ZmbvEncContext),
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
330 encode_init,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
331 encode_frame,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
332 encode_end,
10146
38cfe222e1a4 Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents: 9686
diff changeset
333 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_PAL8, PIX_FMT_NONE},
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6818
diff changeset
334 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
335 };