You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1045 lines
30KB

  1. /*
  2. * FFV1 codec for libavcodec
  3. *
  4. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/ffv1.c
  24. * FF Video Codec 1 (an experimental lossless codec)
  25. */
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "put_bits.h"
  29. #include "dsputil.h"
  30. #include "rangecoder.h"
  31. #include "golomb.h"
  32. #include "mathops.h"
  33. #define MAX_PLANES 4
  34. #define CONTEXT_SIZE 32
  35. extern const uint8_t ff_log2_run[32];
  36. static const int8_t quant3[256]={
  37. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  39. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  40. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  41. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  42. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  43. 1, 1, 1, 1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
  108. static const int8_t quant11[256]={
  109. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  110. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  111. 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  112. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  113. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  114. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  115. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  116. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  117. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  118. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  119. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  120. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  121. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  122. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
  123. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  124. -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
  125. };
  126. static const int8_t quant13[256]={
  127. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  128. 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  129. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  130. 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  131. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  132. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  133. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  134. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  135. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  136. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  137. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  138. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  139. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  140. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  141. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  142. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  143. };
  144. typedef struct VlcState{
  145. int16_t drift;
  146. uint16_t error_sum;
  147. int8_t bias;
  148. uint8_t count;
  149. } VlcState;
  150. typedef struct PlaneContext{
  151. int context_count;
  152. uint8_t (*state)[CONTEXT_SIZE];
  153. VlcState *vlc_state;
  154. uint8_t interlace_bit_state[2];
  155. } PlaneContext;
  156. typedef struct FFV1Context{
  157. AVCodecContext *avctx;
  158. RangeCoder c;
  159. GetBitContext gb;
  160. PutBitContext pb;
  161. int version;
  162. int width, height;
  163. int chroma_h_shift, chroma_v_shift;
  164. int flags;
  165. int picture_number;
  166. AVFrame picture;
  167. int plane_count;
  168. int ac; ///< 1-> CABAC 0-> golomb rice
  169. PlaneContext plane[MAX_PLANES];
  170. int16_t quant_table[5][256];
  171. int run_index;
  172. int colorspace;
  173. DSPContext dsp;
  174. }FFV1Context;
  175. static av_always_inline int fold(int diff, int bits){
  176. if(bits==8)
  177. diff= (int8_t)diff;
  178. else{
  179. diff+= 1<<(bits-1);
  180. diff&=(1<<bits)-1;
  181. diff-= 1<<(bits-1);
  182. }
  183. return diff;
  184. }
  185. static inline int predict(int_fast16_t *src, int_fast16_t *last){
  186. const int LT= last[-1];
  187. const int T= last[ 0];
  188. const int L = src[-1];
  189. return mid_pred(L, L + T - LT, T);
  190. }
  191. static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
  192. const int LT= last[-1];
  193. const int T= last[ 0];
  194. const int RT= last[ 1];
  195. const int L = src[-1];
  196. if(f->quant_table[3][127]){
  197. const int TT= last2[0];
  198. const int LL= src[-2];
  199. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
  200. +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
  201. }else
  202. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
  203. }
  204. static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  205. int i;
  206. if(v){
  207. const int a= FFABS(v);
  208. const int e= av_log2(a);
  209. put_rac(c, state+0, 0);
  210. assert(e<=9);
  211. for(i=0; i<e; i++){
  212. put_rac(c, state+1+i, 1); //1..10
  213. }
  214. put_rac(c, state+1+i, 0);
  215. for(i=e-1; i>=0; i--){
  216. put_rac(c, state+22+i, (a>>i)&1); //22..31
  217. }
  218. if(is_signed)
  219. put_rac(c, state+11 + e, v < 0); //11..21
  220. }else{
  221. put_rac(c, state+0, 1);
  222. }
  223. }
  224. static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  225. if(get_rac(c, state+0))
  226. return 0;
  227. else{
  228. int i, e, a;
  229. e= 0;
  230. while(get_rac(c, state+1 + e)){ //1..10
  231. e++;
  232. }
  233. assert(e<=9);
  234. a= 1;
  235. for(i=e-1; i>=0; i--){
  236. a += a + get_rac(c, state+22 + i); //22..31
  237. }
  238. if(is_signed && get_rac(c, state+11 + e)) //11..21
  239. return -a;
  240. else
  241. return a;
  242. }
  243. }
  244. static inline void update_vlc_state(VlcState * const state, const int v){
  245. int drift= state->drift;
  246. int count= state->count;
  247. state->error_sum += FFABS(v);
  248. drift += v;
  249. if(count == 128){ //FIXME variable
  250. count >>= 1;
  251. drift >>= 1;
  252. state->error_sum >>= 1;
  253. }
  254. count++;
  255. if(drift <= -count){
  256. if(state->bias > -128) state->bias--;
  257. drift += count;
  258. if(drift <= -count)
  259. drift= -count + 1;
  260. }else if(drift > 0){
  261. if(state->bias < 127) state->bias++;
  262. drift -= count;
  263. if(drift > 0)
  264. drift= 0;
  265. }
  266. state->drift= drift;
  267. state->count= count;
  268. }
  269. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  270. int i, k, code;
  271. //printf("final: %d ", v);
  272. v = fold(v - state->bias, bits);
  273. i= state->count;
  274. k=0;
  275. while(i < state->error_sum){ //FIXME optimize
  276. k++;
  277. i += i;
  278. }
  279. assert(k<=8);
  280. #if 0 // JPEG LS
  281. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  282. else code= v;
  283. #else
  284. code= v ^ ((2*state->drift + state->count)>>31);
  285. #endif
  286. //printf("v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code, state->bias, state->error_sum, state->drift, state->count, k);
  287. set_sr_golomb(pb, code, k, 12, bits);
  288. update_vlc_state(state, v);
  289. }
  290. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  291. int k, i, v, ret;
  292. i= state->count;
  293. k=0;
  294. while(i < state->error_sum){ //FIXME optimize
  295. k++;
  296. i += i;
  297. }
  298. assert(k<=8);
  299. v= get_sr_golomb(gb, k, 12, bits);
  300. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  301. #if 0 // JPEG LS
  302. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  303. #else
  304. v ^= ((2*state->drift + state->count)>>31);
  305. #endif
  306. ret= fold(v + state->bias, bits);
  307. update_vlc_state(state, v);
  308. //printf("final: %d\n", ret);
  309. return ret;
  310. }
  311. #if CONFIG_FFV1_ENCODER
  312. static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  313. PlaneContext * const p= &s->plane[plane_index];
  314. RangeCoder * const c= &s->c;
  315. int x;
  316. int run_index= s->run_index;
  317. int run_count=0;
  318. int run_mode=0;
  319. if(s->ac){
  320. if(c->bytestream_end - c->bytestream < w*20){
  321. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  322. return -1;
  323. }
  324. }else{
  325. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  326. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  327. return -1;
  328. }
  329. }
  330. for(x=0; x<w; x++){
  331. int diff, context;
  332. context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
  333. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  334. if(context < 0){
  335. context = -context;
  336. diff= -diff;
  337. }
  338. diff= fold(diff, bits);
  339. if(s->ac){
  340. put_symbol(c, p->state[context], diff, 1);
  341. }else{
  342. if(context == 0) run_mode=1;
  343. if(run_mode){
  344. if(diff){
  345. while(run_count >= 1<<ff_log2_run[run_index]){
  346. run_count -= 1<<ff_log2_run[run_index];
  347. run_index++;
  348. put_bits(&s->pb, 1, 1);
  349. }
  350. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  351. if(run_index) run_index--;
  352. run_count=0;
  353. run_mode=0;
  354. if(diff>0) diff--;
  355. }else{
  356. run_count++;
  357. }
  358. }
  359. // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, (int)put_bits_count(&s->pb));
  360. if(run_mode == 0)
  361. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  362. }
  363. }
  364. if(run_mode){
  365. while(run_count >= 1<<ff_log2_run[run_index]){
  366. run_count -= 1<<ff_log2_run[run_index];
  367. run_index++;
  368. put_bits(&s->pb, 1, 1);
  369. }
  370. if(run_count)
  371. put_bits(&s->pb, 1, 1);
  372. }
  373. s->run_index= run_index;
  374. return 0;
  375. }
  376. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  377. int x,y,i;
  378. const int ring_size= s->avctx->context_model ? 3 : 2;
  379. int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
  380. s->run_index=0;
  381. memset(sample_buffer, 0, sizeof(sample_buffer));
  382. for(y=0; y<h; y++){
  383. for(i=0; i<ring_size; i++)
  384. sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
  385. sample[0][-1]= sample[1][0 ];
  386. sample[1][ w]= sample[1][w-1];
  387. //{START_TIMER
  388. for(x=0; x<w; x++){
  389. sample[0][x]= src[x + stride*y];
  390. }
  391. encode_line(s, w, sample, plane_index, 8);
  392. //STOP_TIMER("encode line")}
  393. }
  394. }
  395. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  396. int x, y, p, i;
  397. const int ring_size= s->avctx->context_model ? 3 : 2;
  398. int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
  399. s->run_index=0;
  400. memset(sample_buffer, 0, sizeof(sample_buffer));
  401. for(y=0; y<h; y++){
  402. for(i=0; i<ring_size; i++)
  403. for(p=0; p<3; p++)
  404. sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
  405. for(x=0; x<w; x++){
  406. int v= src[x + stride*y];
  407. int b= v&0xFF;
  408. int g= (v>>8)&0xFF;
  409. int r= (v>>16)&0xFF;
  410. b -= g;
  411. r -= g;
  412. g += (b + r)>>2;
  413. b += 0x100;
  414. r += 0x100;
  415. // assert(g>=0 && b>=0 && r>=0);
  416. // assert(g<256 && b<512 && r<512);
  417. sample[0][0][x]= g;
  418. sample[1][0][x]= b;
  419. sample[2][0][x]= r;
  420. }
  421. for(p=0; p<3; p++){
  422. sample[p][0][-1]= sample[p][1][0 ];
  423. sample[p][1][ w]= sample[p][1][w-1];
  424. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  425. }
  426. }
  427. }
  428. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  429. int last=0;
  430. int i;
  431. uint8_t state[CONTEXT_SIZE];
  432. memset(state, 128, sizeof(state));
  433. for(i=1; i<128 ; i++){
  434. if(quant_table[i] != quant_table[i-1]){
  435. put_symbol(c, state, i-last-1, 0);
  436. last= i;
  437. }
  438. }
  439. put_symbol(c, state, i-last-1, 0);
  440. }
  441. static void write_header(FFV1Context *f){
  442. uint8_t state[CONTEXT_SIZE];
  443. int i;
  444. RangeCoder * const c= &f->c;
  445. memset(state, 128, sizeof(state));
  446. put_symbol(c, state, f->version, 0);
  447. put_symbol(c, state, f->avctx->coder_type, 0);
  448. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  449. put_rac(c, state, 1); //chroma planes
  450. put_symbol(c, state, f->chroma_h_shift, 0);
  451. put_symbol(c, state, f->chroma_v_shift, 0);
  452. put_rac(c, state, 0); //no transparency plane
  453. for(i=0; i<5; i++)
  454. write_quant_table(c, f->quant_table[i]);
  455. }
  456. #endif /* CONFIG_FFV1_ENCODER */
  457. static av_cold int common_init(AVCodecContext *avctx){
  458. FFV1Context *s = avctx->priv_data;
  459. int width, height;
  460. s->avctx= avctx;
  461. s->flags= avctx->flags;
  462. dsputil_init(&s->dsp, avctx);
  463. width= s->width= avctx->width;
  464. height= s->height= avctx->height;
  465. assert(width && height);
  466. return 0;
  467. }
  468. #if CONFIG_FFV1_ENCODER
  469. static av_cold int encode_init(AVCodecContext *avctx)
  470. {
  471. FFV1Context *s = avctx->priv_data;
  472. int i;
  473. common_init(avctx);
  474. s->version=0;
  475. s->ac= avctx->coder_type;
  476. s->plane_count=2;
  477. for(i=0; i<256; i++){
  478. s->quant_table[0][i]= quant11[i];
  479. s->quant_table[1][i]= 11*quant11[i];
  480. if(avctx->context_model==0){
  481. s->quant_table[2][i]= 11*11*quant11[i];
  482. s->quant_table[3][i]=
  483. s->quant_table[4][i]=0;
  484. }else{
  485. s->quant_table[2][i]= 11*11*quant5 [i];
  486. s->quant_table[3][i]= 5*11*11*quant5 [i];
  487. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  488. }
  489. }
  490. for(i=0; i<s->plane_count; i++){
  491. PlaneContext * const p= &s->plane[i];
  492. if(avctx->context_model==0){
  493. p->context_count= (11*11*11+1)/2;
  494. }else{
  495. p->context_count= (11*11*5*5*5+1)/2;
  496. }
  497. if(s->ac){
  498. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  499. }else{
  500. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  501. }
  502. }
  503. avctx->coded_frame= &s->picture;
  504. switch(avctx->pix_fmt){
  505. case PIX_FMT_YUV444P:
  506. case PIX_FMT_YUV422P:
  507. case PIX_FMT_YUV420P:
  508. case PIX_FMT_YUV411P:
  509. case PIX_FMT_YUV410P:
  510. s->colorspace= 0;
  511. break;
  512. case PIX_FMT_RGB32:
  513. s->colorspace= 1;
  514. break;
  515. default:
  516. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  517. return -1;
  518. }
  519. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  520. s->picture_number=0;
  521. return 0;
  522. }
  523. #endif /* CONFIG_FFV1_ENCODER */
  524. static void clear_state(FFV1Context *f){
  525. int i, j;
  526. for(i=0; i<f->plane_count; i++){
  527. PlaneContext *p= &f->plane[i];
  528. p->interlace_bit_state[0]= 128;
  529. p->interlace_bit_state[1]= 128;
  530. for(j=0; j<p->context_count; j++){
  531. if(f->ac){
  532. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  533. }else{
  534. p->vlc_state[j].drift= 0;
  535. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  536. p->vlc_state[j].bias= 0;
  537. p->vlc_state[j].count= 1;
  538. }
  539. }
  540. }
  541. }
  542. #if CONFIG_FFV1_ENCODER
  543. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  544. FFV1Context *f = avctx->priv_data;
  545. RangeCoder * const c= &f->c;
  546. AVFrame *pict = data;
  547. const int width= f->width;
  548. const int height= f->height;
  549. AVFrame * const p= &f->picture;
  550. int used_count= 0;
  551. uint8_t keystate=128;
  552. ff_init_range_encoder(c, buf, buf_size);
  553. // ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  554. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  555. *p = *pict;
  556. p->pict_type= FF_I_TYPE;
  557. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  558. put_rac(c, &keystate, 1);
  559. p->key_frame= 1;
  560. write_header(f);
  561. clear_state(f);
  562. }else{
  563. put_rac(c, &keystate, 0);
  564. p->key_frame= 0;
  565. }
  566. if(!f->ac){
  567. used_count += ff_rac_terminate(c);
  568. //printf("pos=%d\n", used_count);
  569. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  570. }
  571. if(f->colorspace==0){
  572. const int chroma_width = -((-width )>>f->chroma_h_shift);
  573. const int chroma_height= -((-height)>>f->chroma_v_shift);
  574. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  575. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  576. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  577. }else{
  578. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  579. }
  580. emms_c();
  581. f->picture_number++;
  582. if(f->ac){
  583. return ff_rac_terminate(c);
  584. }else{
  585. flush_put_bits(&f->pb); //nicer padding FIXME
  586. return used_count + (put_bits_count(&f->pb)+7)/8;
  587. }
  588. }
  589. #endif /* CONFIG_FFV1_ENCODER */
  590. static av_cold int common_end(AVCodecContext *avctx){
  591. FFV1Context *s = avctx->priv_data;
  592. int i;
  593. for(i=0; i<s->plane_count; i++){
  594. PlaneContext *p= &s->plane[i];
  595. av_freep(&p->state);
  596. av_freep(&p->vlc_state);
  597. }
  598. return 0;
  599. }
  600. static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  601. PlaneContext * const p= &s->plane[plane_index];
  602. RangeCoder * const c= &s->c;
  603. int x;
  604. int run_count=0;
  605. int run_mode=0;
  606. int run_index= s->run_index;
  607. for(x=0; x<w; x++){
  608. int diff, context, sign;
  609. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  610. if(context < 0){
  611. context= -context;
  612. sign=1;
  613. }else
  614. sign=0;
  615. if(s->ac){
  616. diff= get_symbol(c, p->state[context], 1);
  617. }else{
  618. if(context == 0 && run_mode==0) run_mode=1;
  619. if(run_mode){
  620. if(run_count==0 && run_mode==1){
  621. if(get_bits1(&s->gb)){
  622. run_count = 1<<ff_log2_run[run_index];
  623. if(x + run_count <= w) run_index++;
  624. }else{
  625. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  626. else run_count=0;
  627. if(run_index) run_index--;
  628. run_mode=2;
  629. }
  630. }
  631. run_count--;
  632. if(run_count < 0){
  633. run_mode=0;
  634. run_count=0;
  635. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  636. if(diff>=0) diff++;
  637. }else
  638. diff=0;
  639. }else
  640. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  641. // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, get_bits_count(&s->gb));
  642. }
  643. if(sign) diff= -diff;
  644. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  645. }
  646. s->run_index= run_index;
  647. }
  648. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  649. int x, y;
  650. int_fast16_t sample_buffer[2][w+6];
  651. int_fast16_t *sample[2];
  652. sample[0]=sample_buffer[0]+3;
  653. sample[1]=sample_buffer[1]+3;
  654. s->run_index=0;
  655. memset(sample_buffer, 0, sizeof(sample_buffer));
  656. for(y=0; y<h; y++){
  657. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  658. sample[0]= sample[1];
  659. sample[1]= temp;
  660. sample[1][-1]= sample[0][0 ];
  661. sample[0][ w]= sample[0][w-1];
  662. //{START_TIMER
  663. decode_line(s, w, sample, plane_index, 8);
  664. for(x=0; x<w; x++){
  665. src[x + stride*y]= sample[1][x];
  666. }
  667. //STOP_TIMER("decode-line")}
  668. }
  669. }
  670. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  671. int x, y, p;
  672. int_fast16_t sample_buffer[3][2][w+6];
  673. int_fast16_t *sample[3][2];
  674. for(x=0; x<3; x++){
  675. sample[x][0] = sample_buffer[x][0]+3;
  676. sample[x][1] = sample_buffer[x][1]+3;
  677. }
  678. s->run_index=0;
  679. memset(sample_buffer, 0, sizeof(sample_buffer));
  680. for(y=0; y<h; y++){
  681. for(p=0; p<3; p++){
  682. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  683. sample[p][0]= sample[p][1];
  684. sample[p][1]= temp;
  685. sample[p][1][-1]= sample[p][0][0 ];
  686. sample[p][0][ w]= sample[p][0][w-1];
  687. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  688. }
  689. for(x=0; x<w; x++){
  690. int g= sample[0][1][x];
  691. int b= sample[1][1][x];
  692. int r= sample[2][1][x];
  693. // assert(g>=0 && b>=0 && r>=0);
  694. // assert(g<256 && b<512 && r<512);
  695. b -= 0x100;
  696. r -= 0x100;
  697. g -= (b + r)>>2;
  698. b += g;
  699. r += g;
  700. src[x + stride*y]= b + (g<<8) + (r<<16);
  701. }
  702. }
  703. }
  704. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  705. int v;
  706. int i=0;
  707. uint8_t state[CONTEXT_SIZE];
  708. memset(state, 128, sizeof(state));
  709. for(v=0; i<128 ; v++){
  710. int len= get_symbol(c, state, 0) + 1;
  711. if(len + i > 128) return -1;
  712. while(len--){
  713. quant_table[i] = scale*v;
  714. i++;
  715. //printf("%2d ",v);
  716. //if(i%16==0) printf("\n");
  717. }
  718. }
  719. for(i=1; i<128; i++){
  720. quant_table[256-i]= -quant_table[i];
  721. }
  722. quant_table[128]= -quant_table[127];
  723. return 2*v - 1;
  724. }
  725. static int read_header(FFV1Context *f){
  726. uint8_t state[CONTEXT_SIZE];
  727. int i, context_count;
  728. RangeCoder * const c= &f->c;
  729. memset(state, 128, sizeof(state));
  730. f->version= get_symbol(c, state, 0);
  731. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  732. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  733. get_rac(c, state); //no chroma = false
  734. f->chroma_h_shift= get_symbol(c, state, 0);
  735. f->chroma_v_shift= get_symbol(c, state, 0);
  736. get_rac(c, state); //transparency plane
  737. f->plane_count= 2;
  738. if(f->colorspace==0){
  739. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  740. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  741. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  742. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  743. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  744. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  745. default:
  746. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  747. return -1;
  748. }
  749. }else if(f->colorspace==1){
  750. if(f->chroma_h_shift || f->chroma_v_shift){
  751. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  752. return -1;
  753. }
  754. f->avctx->pix_fmt= PIX_FMT_RGB32;
  755. }else{
  756. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  757. return -1;
  758. }
  759. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  760. context_count=1;
  761. for(i=0; i<5; i++){
  762. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  763. if(context_count < 0 || context_count > 32768){
  764. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  765. return -1;
  766. }
  767. }
  768. context_count= (context_count+1)/2;
  769. for(i=0; i<f->plane_count; i++){
  770. PlaneContext * const p= &f->plane[i];
  771. p->context_count= context_count;
  772. if(f->ac){
  773. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  774. }else{
  775. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  776. }
  777. }
  778. return 0;
  779. }
  780. static av_cold int decode_init(AVCodecContext *avctx)
  781. {
  782. // FFV1Context *s = avctx->priv_data;
  783. common_init(avctx);
  784. return 0;
  785. }
  786. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  787. const uint8_t *buf = avpkt->data;
  788. int buf_size = avpkt->size;
  789. FFV1Context *f = avctx->priv_data;
  790. RangeCoder * const c= &f->c;
  791. const int width= f->width;
  792. const int height= f->height;
  793. AVFrame * const p= &f->picture;
  794. int bytes_read;
  795. uint8_t keystate= 128;
  796. AVFrame *picture = data;
  797. ff_init_range_decoder(c, buf, buf_size);
  798. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  799. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  800. if(get_rac(c, &keystate)){
  801. p->key_frame= 1;
  802. if(read_header(f) < 0)
  803. return -1;
  804. clear_state(f);
  805. }else{
  806. p->key_frame= 0;
  807. }
  808. if(!f->plane[0].state && !f->plane[0].vlc_state)
  809. return -1;
  810. p->reference= 0;
  811. if(avctx->get_buffer(avctx, p) < 0){
  812. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  813. return -1;
  814. }
  815. if(avctx->debug&FF_DEBUG_PICT_INFO)
  816. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  817. if(!f->ac){
  818. bytes_read = c->bytestream - c->bytestream_start - 1;
  819. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  820. //printf("pos=%d\n", bytes_read);
  821. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  822. } else {
  823. bytes_read = 0; /* avoid warning */
  824. }
  825. if(f->colorspace==0){
  826. const int chroma_width = -((-width )>>f->chroma_h_shift);
  827. const int chroma_height= -((-height)>>f->chroma_v_shift);
  828. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  829. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  830. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  831. }else{
  832. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  833. }
  834. emms_c();
  835. f->picture_number++;
  836. *picture= *p;
  837. avctx->release_buffer(avctx, p); //FIXME
  838. *data_size = sizeof(AVFrame);
  839. if(f->ac){
  840. bytes_read= c->bytestream - c->bytestream_start - 1;
  841. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  842. }else{
  843. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  844. }
  845. return bytes_read;
  846. }
  847. AVCodec ffv1_decoder = {
  848. "ffv1",
  849. CODEC_TYPE_VIDEO,
  850. CODEC_ID_FFV1,
  851. sizeof(FFV1Context),
  852. decode_init,
  853. NULL,
  854. common_end,
  855. decode_frame,
  856. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  857. NULL,
  858. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
  859. };
  860. #if CONFIG_FFV1_ENCODER
  861. AVCodec ffv1_encoder = {
  862. "ffv1",
  863. CODEC_TYPE_VIDEO,
  864. CODEC_ID_FFV1,
  865. sizeof(FFV1Context),
  866. encode_init,
  867. encode_frame,
  868. common_end,
  869. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, PIX_FMT_NONE},
  870. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
  871. };
  872. #endif