ffv1.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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
  24. * FF Video Codec 1 (a 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. #include "libavutil/avassert.h"
  34. #define MAX_PLANES 4
  35. #define CONTEXT_SIZE 32
  36. #define MAX_QUANT_TABLES 8
  37. #define MAX_CONTEXT_INPUTS 5
  38. extern const uint8_t ff_log2_run[41];
  39. static const int8_t quant3[256]={
  40. 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  44. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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2,-2,-2,-2,-2,-2,-2,
  109. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
  110. };
  111. static const int8_t quant9[256]={
  112. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  113. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  114. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  115. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  116. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  117. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  118. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  119. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  120. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  121. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  122. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-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,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  125. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  126. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
  127. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  128. };
  129. static const int8_t quant9_10bit[256]={
  130. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  131. 2, 2, 2, 2, 2, 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, 6,
  174. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  175. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  176. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  177. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  178. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  179. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  180. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  181. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  182. };
  183. static const uint8_t ver2_state[256]= {
  184. 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
  185. 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
  186. 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
  187. 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
  188. 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
  189. 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
  190. 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
  191. 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
  192. 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
  193. 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
  194. 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
  195. 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
  196. 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
  197. 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
  198. 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
  199. 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
  200. };
  201. typedef struct VlcState{
  202. int16_t drift;
  203. uint16_t error_sum;
  204. int8_t bias;
  205. uint8_t count;
  206. } VlcState;
  207. typedef struct PlaneContext{
  208. int16_t quant_table[MAX_CONTEXT_INPUTS][256];
  209. int quant_table_index;
  210. int context_count;
  211. uint8_t (*state)[CONTEXT_SIZE];
  212. VlcState *vlc_state;
  213. uint8_t interlace_bit_state[2];
  214. } PlaneContext;
  215. #define MAX_SLICES 256
  216. typedef struct FFV1Context{
  217. AVCodecContext *avctx;
  218. RangeCoder c;
  219. GetBitContext gb;
  220. PutBitContext pb;
  221. uint64_t rc_stat[256][2];
  222. uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
  223. int version;
  224. int width, height;
  225. int chroma_h_shift, chroma_v_shift;
  226. int flags;
  227. int picture_number;
  228. AVFrame picture;
  229. int plane_count;
  230. int ac; ///< 1=range coder <-> 0=golomb rice
  231. PlaneContext plane[MAX_PLANES];
  232. int16_t quant_table[MAX_CONTEXT_INPUTS][256];
  233. int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
  234. int context_count[MAX_QUANT_TABLES];
  235. uint8_t state_transition[256];
  236. uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
  237. int run_index;
  238. int colorspace;
  239. int16_t *sample_buffer;
  240. int gob_count;
  241. int packed_at_lsb;
  242. int quant_table_count;
  243. DSPContext dsp;
  244. struct FFV1Context *slice_context[MAX_SLICES];
  245. int slice_count;
  246. int num_v_slices;
  247. int num_h_slices;
  248. int slice_width;
  249. int slice_height;
  250. int slice_x;
  251. int slice_y;
  252. }FFV1Context;
  253. static av_always_inline int fold(int diff, int bits){
  254. if(bits==8)
  255. diff= (int8_t)diff;
  256. else{
  257. diff+= 1<<(bits-1);
  258. diff&=(1<<bits)-1;
  259. diff-= 1<<(bits-1);
  260. }
  261. return diff;
  262. }
  263. static inline int predict(int16_t *src, int16_t *last)
  264. {
  265. const int LT= last[-1];
  266. const int T= last[ 0];
  267. const int L = src[-1];
  268. return mid_pred(L, L + T - LT, T);
  269. }
  270. static inline int get_context(PlaneContext *p, int16_t *src,
  271. int16_t *last, int16_t *last2)
  272. {
  273. const int LT= last[-1];
  274. const int T= last[ 0];
  275. const int RT= last[ 1];
  276. const int L = src[-1];
  277. if(p->quant_table[3][127]){
  278. const int TT= last2[0];
  279. const int LL= src[-2];
  280. return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF]
  281. +p->quant_table[3][(LL-L) & 0xFF] + p->quant_table[4][(TT-T) & 0xFF];
  282. }else
  283. return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF];
  284. }
  285. static void find_best_state(uint8_t best_state[256][256], const uint8_t one_state[256]){
  286. int i,j,k,m;
  287. double l2tab[256];
  288. for(i=1; i<256; i++)
  289. l2tab[i]= log2(i/256.0);
  290. for(i=0; i<256; i++){
  291. double best_len[256];
  292. double p= i/256.0;
  293. for(j=0; j<256; j++)
  294. best_len[j]= 1<<30;
  295. for(j=FFMAX(i-10,1); j<FFMIN(i+11,256); j++){
  296. double occ[256]={0};
  297. double len=0;
  298. occ[j]=1.0;
  299. for(k=0; k<256; k++){
  300. double newocc[256]={0};
  301. for(m=0; m<256; m++){
  302. if(occ[m]){
  303. len -=occ[m]*( p *l2tab[ m]
  304. + (1-p)*l2tab[256-m]);
  305. }
  306. }
  307. if(len < best_len[k]){
  308. best_len[k]= len;
  309. best_state[i][k]= j;
  310. }
  311. for(m=0; m<256; m++){
  312. if(occ[m]){
  313. newocc[ one_state[ m]] += occ[m]* p ;
  314. newocc[256-one_state[256-m]] += occ[m]*(1-p);
  315. }
  316. }
  317. memcpy(occ, newocc, sizeof(occ));
  318. }
  319. }
  320. }
  321. }
  322. static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2]){
  323. int i;
  324. #define put_rac(C,S,B) \
  325. do{\
  326. if(rc_stat){\
  327. rc_stat[*(S)][B]++;\
  328. rc_stat2[(S)-state][B]++;\
  329. }\
  330. put_rac(C,S,B);\
  331. }while(0)
  332. if(v){
  333. const int a= FFABS(v);
  334. const int e= av_log2(a);
  335. put_rac(c, state+0, 0);
  336. if(e<=9){
  337. for(i=0; i<e; i++){
  338. put_rac(c, state+1+i, 1); //1..10
  339. }
  340. put_rac(c, state+1+i, 0);
  341. for(i=e-1; i>=0; i--){
  342. put_rac(c, state+22+i, (a>>i)&1); //22..31
  343. }
  344. if(is_signed)
  345. put_rac(c, state+11 + e, v < 0); //11..21
  346. }else{
  347. for(i=0; i<e; i++){
  348. put_rac(c, state+1+FFMIN(i,9), 1); //1..10
  349. }
  350. put_rac(c, state+1+9, 0);
  351. for(i=e-1; i>=0; i--){
  352. put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
  353. }
  354. if(is_signed)
  355. put_rac(c, state+11 + 10, v < 0); //11..21
  356. }
  357. }else{
  358. put_rac(c, state+0, 1);
  359. }
  360. #undef put_rac
  361. }
  362. static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  363. put_symbol_inline(c, state, v, is_signed, NULL, NULL);
  364. }
  365. static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
  366. if(get_rac(c, state+0))
  367. return 0;
  368. else{
  369. int i, e, a;
  370. e= 0;
  371. while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
  372. e++;
  373. }
  374. a= 1;
  375. for(i=e-1; i>=0; i--){
  376. a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
  377. }
  378. e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
  379. return (a^e)-e;
  380. }
  381. }
  382. static int av_noinline get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  383. return get_symbol_inline(c, state, is_signed);
  384. }
  385. static inline void update_vlc_state(VlcState * const state, const int v){
  386. int drift= state->drift;
  387. int count= state->count;
  388. state->error_sum += FFABS(v);
  389. drift += v;
  390. if(count == 128){ //FIXME variable
  391. count >>= 1;
  392. drift >>= 1;
  393. state->error_sum >>= 1;
  394. }
  395. count++;
  396. if(drift <= -count){
  397. if(state->bias > -128) state->bias--;
  398. drift += count;
  399. if(drift <= -count)
  400. drift= -count + 1;
  401. }else if(drift > 0){
  402. if(state->bias < 127) state->bias++;
  403. drift -= count;
  404. if(drift > 0)
  405. drift= 0;
  406. }
  407. state->drift= drift;
  408. state->count= count;
  409. }
  410. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  411. int i, k, code;
  412. //printf("final: %d ", v);
  413. v = fold(v - state->bias, bits);
  414. i= state->count;
  415. k=0;
  416. while(i < state->error_sum){ //FIXME optimize
  417. k++;
  418. i += i;
  419. }
  420. assert(k<=8);
  421. #if 0 // JPEG LS
  422. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  423. else code= v;
  424. #else
  425. code= v ^ ((2*state->drift + state->count)>>31);
  426. #endif
  427. //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);
  428. set_sr_golomb(pb, code, k, 12, bits);
  429. update_vlc_state(state, v);
  430. }
  431. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  432. int k, i, v, ret;
  433. i= state->count;
  434. k=0;
  435. while(i < state->error_sum){ //FIXME optimize
  436. k++;
  437. i += i;
  438. }
  439. assert(k<=8);
  440. v= get_sr_golomb(gb, k, 12, bits);
  441. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  442. #if 0 // JPEG LS
  443. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  444. #else
  445. v ^= ((2*state->drift + state->count)>>31);
  446. #endif
  447. ret= fold(v + state->bias, bits);
  448. update_vlc_state(state, v);
  449. //printf("final: %d\n", ret);
  450. return ret;
  451. }
  452. #if CONFIG_FFV1_ENCODER
  453. static av_always_inline int encode_line(FFV1Context *s, int w,
  454. int16_t *sample[2],
  455. int plane_index, int bits)
  456. {
  457. PlaneContext * const p= &s->plane[plane_index];
  458. RangeCoder * const c= &s->c;
  459. int x;
  460. int run_index= s->run_index;
  461. int run_count=0;
  462. int run_mode=0;
  463. if(s->ac){
  464. if(c->bytestream_end - c->bytestream < w*20){
  465. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  466. return -1;
  467. }
  468. }else{
  469. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  470. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  471. return -1;
  472. }
  473. }
  474. for(x=0; x<w; x++){
  475. int diff, context;
  476. context= get_context(p, sample[0]+x, sample[1]+x, sample[2]+x);
  477. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  478. if(context < 0){
  479. context = -context;
  480. diff= -diff;
  481. }
  482. diff= fold(diff, bits);
  483. if(s->ac){
  484. if(s->flags & CODEC_FLAG_PASS1){
  485. put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat, s->rc_stat2[p->quant_table_index][context]);
  486. }else{
  487. put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
  488. }
  489. }else{
  490. if(context == 0) run_mode=1;
  491. if(run_mode){
  492. if(diff){
  493. while(run_count >= 1<<ff_log2_run[run_index]){
  494. run_count -= 1<<ff_log2_run[run_index];
  495. run_index++;
  496. put_bits(&s->pb, 1, 1);
  497. }
  498. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  499. if(run_index) run_index--;
  500. run_count=0;
  501. run_mode=0;
  502. if(diff>0) diff--;
  503. }else{
  504. run_count++;
  505. }
  506. }
  507. // 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));
  508. if(run_mode == 0)
  509. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  510. }
  511. }
  512. if(run_mode){
  513. while(run_count >= 1<<ff_log2_run[run_index]){
  514. run_count -= 1<<ff_log2_run[run_index];
  515. run_index++;
  516. put_bits(&s->pb, 1, 1);
  517. }
  518. if(run_count)
  519. put_bits(&s->pb, 1, 1);
  520. }
  521. s->run_index= run_index;
  522. return 0;
  523. }
  524. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  525. int x,y,i;
  526. const int ring_size= s->avctx->context_model ? 3 : 2;
  527. int16_t *sample[3];
  528. s->run_index=0;
  529. memset(s->sample_buffer, 0, ring_size*(w+6)*sizeof(*s->sample_buffer));
  530. for(y=0; y<h; y++){
  531. for(i=0; i<ring_size; i++)
  532. sample[i]= s->sample_buffer + (w+6)*((h+i-y)%ring_size) + 3;
  533. sample[0][-1]= sample[1][0 ];
  534. sample[1][ w]= sample[1][w-1];
  535. //{START_TIMER
  536. if(s->avctx->bits_per_raw_sample<=8){
  537. for(x=0; x<w; x++){
  538. sample[0][x]= src[x + stride*y];
  539. }
  540. encode_line(s, w, sample, plane_index, 8);
  541. }else{
  542. if(s->packed_at_lsb){
  543. for(x=0; x<w; x++){
  544. sample[0][x]= ((uint16_t*)(src + stride*y))[x];
  545. }
  546. }else{
  547. for(x=0; x<w; x++){
  548. sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->avctx->bits_per_raw_sample);
  549. }
  550. }
  551. encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  552. }
  553. //STOP_TIMER("encode line")}
  554. }
  555. }
  556. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  557. int x, y, p, i;
  558. const int ring_size= s->avctx->context_model ? 3 : 2;
  559. int16_t *sample[3][3];
  560. s->run_index=0;
  561. memset(s->sample_buffer, 0, ring_size*3*(w+6)*sizeof(*s->sample_buffer));
  562. for(y=0; y<h; y++){
  563. for(i=0; i<ring_size; i++)
  564. for(p=0; p<3; p++)
  565. sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
  566. for(x=0; x<w; x++){
  567. int v= src[x + stride*y];
  568. int b= v&0xFF;
  569. int g= (v>>8)&0xFF;
  570. int r= (v>>16)&0xFF;
  571. b -= g;
  572. r -= g;
  573. g += (b + r)>>2;
  574. b += 0x100;
  575. r += 0x100;
  576. // assert(g>=0 && b>=0 && r>=0);
  577. // assert(g<256 && b<512 && r<512);
  578. sample[0][0][x]= g;
  579. sample[1][0][x]= b;
  580. sample[2][0][x]= r;
  581. }
  582. for(p=0; p<3; p++){
  583. sample[p][0][-1]= sample[p][1][0 ];
  584. sample[p][1][ w]= sample[p][1][w-1];
  585. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  586. }
  587. }
  588. }
  589. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  590. int last=0;
  591. int i;
  592. uint8_t state[CONTEXT_SIZE];
  593. memset(state, 128, sizeof(state));
  594. for(i=1; i<128 ; i++){
  595. if(quant_table[i] != quant_table[i-1]){
  596. put_symbol(c, state, i-last-1, 0);
  597. last= i;
  598. }
  599. }
  600. put_symbol(c, state, i-last-1, 0);
  601. }
  602. static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
  603. int i;
  604. for(i=0; i<5; i++)
  605. write_quant_table(c, quant_table[i]);
  606. }
  607. static void write_header(FFV1Context *f){
  608. uint8_t state[CONTEXT_SIZE];
  609. int i, j;
  610. RangeCoder * const c= &f->slice_context[0]->c;
  611. memset(state, 128, sizeof(state));
  612. if(f->version < 2){
  613. put_symbol(c, state, f->version, 0);
  614. put_symbol(c, state, f->ac, 0);
  615. if(f->ac>1){
  616. for(i=1; i<256; i++){
  617. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  618. }
  619. }
  620. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  621. if(f->version>0)
  622. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  623. put_rac(c, state, 1); //chroma planes
  624. put_symbol(c, state, f->chroma_h_shift, 0);
  625. put_symbol(c, state, f->chroma_v_shift, 0);
  626. put_rac(c, state, 0); //no transparency plane
  627. write_quant_tables(c, f->quant_table);
  628. }else{
  629. put_symbol(c, state, f->slice_count, 0);
  630. for(i=0; i<f->slice_count; i++){
  631. FFV1Context *fs= f->slice_context[i];
  632. put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
  633. put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
  634. put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
  635. put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
  636. for(j=0; j<f->plane_count; j++){
  637. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  638. av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
  639. }
  640. }
  641. }
  642. }
  643. #endif /* CONFIG_FFV1_ENCODER */
  644. static av_cold int common_init(AVCodecContext *avctx){
  645. FFV1Context *s = avctx->priv_data;
  646. s->avctx= avctx;
  647. s->flags= avctx->flags;
  648. avcodec_get_frame_defaults(&s->picture);
  649. dsputil_init(&s->dsp, avctx);
  650. s->width = avctx->width;
  651. s->height= avctx->height;
  652. assert(s->width && s->height);
  653. //defaults
  654. s->num_h_slices=1;
  655. s->num_v_slices=1;
  656. return 0;
  657. }
  658. static int init_slice_state(FFV1Context *f){
  659. int i, j;
  660. for(i=0; i<f->slice_count; i++){
  661. FFV1Context *fs= f->slice_context[i];
  662. for(j=0; j<f->plane_count; j++){
  663. PlaneContext * const p= &fs->plane[j];
  664. if(fs->ac){
  665. if(!p-> state) p-> state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  666. if(!p-> state)
  667. return AVERROR(ENOMEM);
  668. }else{
  669. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  670. if(!p->vlc_state)
  671. return AVERROR(ENOMEM);
  672. }
  673. }
  674. if (fs->ac>1){
  675. //FIXME only redo if state_transition changed
  676. for(j=1; j<256; j++){
  677. fs->c.one_state [ j]= fs->state_transition[j];
  678. fs->c.zero_state[256-j]= 256-fs->c.one_state [j];
  679. }
  680. }
  681. }
  682. return 0;
  683. }
  684. static av_cold int init_slice_contexts(FFV1Context *f){
  685. int i;
  686. f->slice_count= f->num_h_slices * f->num_v_slices;
  687. for(i=0; i<f->slice_count; i++){
  688. FFV1Context *fs= av_mallocz(sizeof(*fs));
  689. int sx= i % f->num_h_slices;
  690. int sy= i / f->num_h_slices;
  691. int sxs= f->avctx->width * sx / f->num_h_slices;
  692. int sxe= f->avctx->width *(sx+1) / f->num_h_slices;
  693. int sys= f->avctx->height* sy / f->num_v_slices;
  694. int sye= f->avctx->height*(sy+1) / f->num_v_slices;
  695. f->slice_context[i]= fs;
  696. memcpy(fs, f, sizeof(*fs));
  697. memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
  698. fs->slice_width = sxe - sxs;
  699. fs->slice_height= sye - sys;
  700. fs->slice_x = sxs;
  701. fs->slice_y = sys;
  702. fs->sample_buffer = av_malloc(9 * (fs->width+6) * sizeof(*fs->sample_buffer));
  703. if (!fs->sample_buffer)
  704. return AVERROR(ENOMEM);
  705. }
  706. return 0;
  707. }
  708. static int allocate_initial_states(FFV1Context *f){
  709. int i;
  710. for(i=0; i<f->quant_table_count; i++){
  711. f->initial_states[i]= av_malloc(f->context_count[i]*sizeof(*f->initial_states[i]));
  712. if(!f->initial_states[i])
  713. return AVERROR(ENOMEM);
  714. memset(f->initial_states[i], 128, f->context_count[i]*sizeof(*f->initial_states[i]));
  715. }
  716. return 0;
  717. }
  718. #if CONFIG_FFV1_ENCODER
  719. static int write_extra_header(FFV1Context *f){
  720. RangeCoder * const c= &f->c;
  721. uint8_t state[CONTEXT_SIZE];
  722. int i, j, k;
  723. uint8_t state2[32][CONTEXT_SIZE];
  724. memset(state2, 128, sizeof(state2));
  725. memset(state, 128, sizeof(state));
  726. f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000 + (11*11*5*5*5+11*11*11)*32);
  727. ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
  728. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  729. put_symbol(c, state, f->version, 0);
  730. put_symbol(c, state, f->ac, 0);
  731. if(f->ac>1){
  732. for(i=1; i<256; i++){
  733. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  734. }
  735. }
  736. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  737. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  738. put_rac(c, state, 1); //chroma planes
  739. put_symbol(c, state, f->chroma_h_shift, 0);
  740. put_symbol(c, state, f->chroma_v_shift, 0);
  741. put_rac(c, state, 0); //no transparency plane
  742. put_symbol(c, state, f->num_h_slices-1, 0);
  743. put_symbol(c, state, f->num_v_slices-1, 0);
  744. put_symbol(c, state, f->quant_table_count, 0);
  745. for(i=0; i<f->quant_table_count; i++)
  746. write_quant_tables(c, f->quant_tables[i]);
  747. for(i=0; i<f->quant_table_count; i++){
  748. for(j=0; j<f->context_count[i]*CONTEXT_SIZE; j++)
  749. if(f->initial_states[i] && f->initial_states[i][0][j] != 128)
  750. break;
  751. if(j<f->context_count[i]*CONTEXT_SIZE){
  752. put_rac(c, state, 1);
  753. for(j=0; j<f->context_count[i]; j++){
  754. for(k=0; k<CONTEXT_SIZE; k++){
  755. int pred= j ? f->initial_states[i][j-1][k] : 128;
  756. put_symbol(c, state2[k], (int8_t)(f->initial_states[i][j][k]-pred), 1);
  757. }
  758. }
  759. }else{
  760. put_rac(c, state, 0);
  761. }
  762. }
  763. f->avctx->extradata_size= ff_rac_terminate(c);
  764. return 0;
  765. }
  766. static int sort_stt(FFV1Context *s, uint8_t stt[256]){
  767. int i,i2,changed,print=0;
  768. do{
  769. changed=0;
  770. for(i=12; i<244; i++){
  771. for(i2=i+1; i2<245 && i2<i+4; i2++){
  772. #define COST(old, new) \
  773. s->rc_stat[old][0]*-log2((256-(new))/256.0)\
  774. +s->rc_stat[old][1]*-log2( (new) /256.0)
  775. #define COST2(old, new) \
  776. COST(old, new)\
  777. +COST(256-(old), 256-(new))
  778. double size0= COST2(i, i ) + COST2(i2, i2);
  779. double sizeX= COST2(i, i2) + COST2(i2, i );
  780. if(sizeX < size0 && i!=128 && i2!=128){
  781. int j;
  782. FFSWAP(int, stt[ i], stt[ i2]);
  783. FFSWAP(int, s->rc_stat[i ][0],s->rc_stat[ i2][0]);
  784. FFSWAP(int, s->rc_stat[i ][1],s->rc_stat[ i2][1]);
  785. if(i != 256-i2){
  786. FFSWAP(int, stt[256-i], stt[256-i2]);
  787. FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
  788. FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
  789. }
  790. for(j=1; j<256; j++){
  791. if (stt[j] == i ) stt[j] = i2;
  792. else if(stt[j] == i2) stt[j] = i ;
  793. if(i != 256-i2){
  794. if (stt[256-j] == 256-i ) stt[256-j] = 256-i2;
  795. else if(stt[256-j] == 256-i2) stt[256-j] = 256-i ;
  796. }
  797. }
  798. print=changed=1;
  799. }
  800. }
  801. }
  802. }while(changed);
  803. return print;
  804. }
  805. static av_cold int encode_init(AVCodecContext *avctx)
  806. {
  807. FFV1Context *s = avctx->priv_data;
  808. int i, j, k, m;
  809. common_init(avctx);
  810. s->version=0;
  811. s->ac= avctx->coder_type ? 2:0;
  812. if(s->ac>1)
  813. for(i=1; i<256; i++)
  814. s->state_transition[i]=ver2_state[i];
  815. s->plane_count=2;
  816. for(i=0; i<256; i++){
  817. s->quant_table_count=2;
  818. if(avctx->bits_per_raw_sample <=8){
  819. s->quant_tables[0][0][i]= quant11[i];
  820. s->quant_tables[0][1][i]= 11*quant11[i];
  821. s->quant_tables[0][2][i]= 11*11*quant11[i];
  822. s->quant_tables[1][0][i]= quant11[i];
  823. s->quant_tables[1][1][i]= 11*quant11[i];
  824. s->quant_tables[1][2][i]= 11*11*quant5 [i];
  825. s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
  826. s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
  827. }else{
  828. s->quant_tables[0][0][i]= quant9_10bit[i];
  829. s->quant_tables[0][1][i]= 11*quant9_10bit[i];
  830. s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
  831. s->quant_tables[1][0][i]= quant9_10bit[i];
  832. s->quant_tables[1][1][i]= 11*quant9_10bit[i];
  833. s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
  834. s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
  835. s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
  836. }
  837. }
  838. s->context_count[0]= (11*11*11+1)/2;
  839. s->context_count[1]= (11*11*5*5*5+1)/2;
  840. memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
  841. for(i=0; i<s->plane_count; i++){
  842. PlaneContext * const p= &s->plane[i];
  843. memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
  844. p->quant_table_index= avctx->context_model;
  845. p->context_count= s->context_count[p->quant_table_index];
  846. }
  847. if(allocate_initial_states(s) < 0)
  848. return AVERROR(ENOMEM);
  849. avctx->coded_frame= &s->picture;
  850. switch(avctx->pix_fmt){
  851. case PIX_FMT_YUV420P9:
  852. case PIX_FMT_YUV420P10:
  853. case PIX_FMT_YUV422P10:
  854. s->packed_at_lsb = 1;
  855. case PIX_FMT_YUV444P16:
  856. case PIX_FMT_YUV422P16:
  857. case PIX_FMT_YUV420P16:
  858. if(avctx->bits_per_raw_sample <=8){
  859. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  860. return -1;
  861. }
  862. if(!s->ac){
  863. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  864. return -1;
  865. }
  866. s->version= FFMAX(s->version, 1);
  867. case PIX_FMT_YUV444P:
  868. case PIX_FMT_YUV422P:
  869. case PIX_FMT_YUV420P:
  870. case PIX_FMT_YUV411P:
  871. case PIX_FMT_YUV410P:
  872. s->colorspace= 0;
  873. break;
  874. case PIX_FMT_RGB32:
  875. s->colorspace= 1;
  876. break;
  877. default:
  878. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  879. return -1;
  880. }
  881. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  882. s->picture_number=0;
  883. if(avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
  884. for(i=0; i<s->quant_table_count; i++){
  885. s->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*s->rc_stat2[i]));
  886. if(!s->rc_stat2[i])
  887. return AVERROR(ENOMEM);
  888. }
  889. }
  890. if(avctx->stats_in){
  891. char *p= avctx->stats_in;
  892. uint8_t best_state[256][256];
  893. int gob_count=0;
  894. char *next;
  895. av_assert0(s->version>=2);
  896. for(;;){
  897. for(j=0; j<256; j++){
  898. for(i=0; i<2; i++){
  899. s->rc_stat[j][i]= strtol(p, &next, 0);
  900. if(next==p){
  901. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d [%s]\n", j,i,p);
  902. return -1;
  903. }
  904. p=next;
  905. }
  906. }
  907. for(i=0; i<s->quant_table_count; i++){
  908. for(j=0; j<s->context_count[i]; j++){
  909. for(k=0; k<32; k++){
  910. for(m=0; m<2; m++){
  911. s->rc_stat2[i][j][k][m]= strtol(p, &next, 0);
  912. if(next==p){
  913. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d %d %d [%s]\n", i,j,k,m,p);
  914. return -1;
  915. }
  916. p=next;
  917. }
  918. }
  919. }
  920. }
  921. gob_count= strtol(p, &next, 0);
  922. if(next==p || gob_count <0){
  923. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
  924. return -1;
  925. }
  926. p=next;
  927. while(*p=='\n' || *p==' ') p++;
  928. if(p[0]==0) break;
  929. }
  930. sort_stt(s, s->state_transition);
  931. find_best_state(best_state, s->state_transition);
  932. for(i=0; i<s->quant_table_count; i++){
  933. for(j=0; j<s->context_count[i]; j++){
  934. for(k=0; k<32; k++){
  935. double p= 128;
  936. if(s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]){
  937. p=256.0*s->rc_stat2[i][j][k][1] / (s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]);
  938. }
  939. s->initial_states[i][j][k]= best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1])/gob_count, 0, 255)];
  940. }
  941. }
  942. }
  943. }
  944. if(s->version>1){
  945. s->num_h_slices=2;
  946. s->num_v_slices=2;
  947. write_extra_header(s);
  948. }
  949. if(init_slice_contexts(s) < 0)
  950. return -1;
  951. if(init_slice_state(s) < 0)
  952. return -1;
  953. #define STATS_OUT_SIZE 1024*1024*6
  954. if(avctx->flags & CODEC_FLAG_PASS1){
  955. avctx->stats_out= av_mallocz(STATS_OUT_SIZE);
  956. for(i=0; i<s->quant_table_count; i++){
  957. for(j=0; j<s->slice_count; j++){
  958. FFV1Context *sf= s->slice_context[j];
  959. av_assert0(!sf->rc_stat2[i]);
  960. sf->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*sf->rc_stat2[i]));
  961. if(!sf->rc_stat2[i])
  962. return AVERROR(ENOMEM);
  963. }
  964. }
  965. }
  966. return 0;
  967. }
  968. #endif /* CONFIG_FFV1_ENCODER */
  969. static void clear_state(FFV1Context *f){
  970. int i, si, j;
  971. for(si=0; si<f->slice_count; si++){
  972. FFV1Context *fs= f->slice_context[si];
  973. for(i=0; i<f->plane_count; i++){
  974. PlaneContext *p= &fs->plane[i];
  975. p->interlace_bit_state[0]= 128;
  976. p->interlace_bit_state[1]= 128;
  977. if(fs->ac){
  978. if(f->initial_states[p->quant_table_index]){
  979. memcpy(p->state, f->initial_states[p->quant_table_index], CONTEXT_SIZE*p->context_count);
  980. }else
  981. memset(p->state, 128, CONTEXT_SIZE*p->context_count);
  982. }else{
  983. for(j=0; j<p->context_count; j++){
  984. p->vlc_state[j].drift= 0;
  985. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  986. p->vlc_state[j].bias= 0;
  987. p->vlc_state[j].count= 1;
  988. }
  989. }
  990. }
  991. }
  992. }
  993. #if CONFIG_FFV1_ENCODER
  994. static int encode_slice(AVCodecContext *c, void *arg){
  995. FFV1Context *fs= *(void**)arg;
  996. FFV1Context *f= fs->avctx->priv_data;
  997. int width = fs->slice_width;
  998. int height= fs->slice_height;
  999. int x= fs->slice_x;
  1000. int y= fs->slice_y;
  1001. AVFrame * const p= &f->picture;
  1002. if(f->colorspace==0){
  1003. const int chroma_width = -((-width )>>f->chroma_h_shift);
  1004. const int chroma_height= -((-height)>>f->chroma_v_shift);
  1005. const int cx= x>>f->chroma_h_shift;
  1006. const int cy= y>>f->chroma_v_shift;
  1007. encode_plane(fs, p->data[0] + x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1008. encode_plane(fs, p->data[1] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1009. encode_plane(fs, p->data[2] + cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  1010. }else{
  1011. encode_rgb_frame(fs, (uint32_t*)(p->data[0]) + x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
  1012. }
  1013. emms_c();
  1014. return 0;
  1015. }
  1016. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  1017. FFV1Context *f = avctx->priv_data;
  1018. RangeCoder * const c= &f->slice_context[0]->c;
  1019. AVFrame *pict = data;
  1020. AVFrame * const p= &f->picture;
  1021. int used_count= 0;
  1022. uint8_t keystate=128;
  1023. uint8_t *buf_p;
  1024. int i;
  1025. ff_init_range_encoder(c, buf, buf_size);
  1026. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1027. *p = *pict;
  1028. p->pict_type= AV_PICTURE_TYPE_I;
  1029. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  1030. put_rac(c, &keystate, 1);
  1031. p->key_frame= 1;
  1032. f->gob_count++;
  1033. write_header(f);
  1034. clear_state(f);
  1035. }else{
  1036. put_rac(c, &keystate, 0);
  1037. p->key_frame= 0;
  1038. }
  1039. if(!f->ac){
  1040. used_count += ff_rac_terminate(c);
  1041. //printf("pos=%d\n", used_count);
  1042. init_put_bits(&f->slice_context[0]->pb, buf + used_count, buf_size - used_count);
  1043. }else if (f->ac>1){
  1044. int i;
  1045. for(i=1; i<256; i++){
  1046. c->one_state[i]= f->state_transition[i];
  1047. c->zero_state[256-i]= 256-c->one_state[i];
  1048. }
  1049. }
  1050. for(i=1; i<f->slice_count; i++){
  1051. FFV1Context *fs= f->slice_context[i];
  1052. uint8_t *start= buf + (buf_size-used_count)*i/f->slice_count;
  1053. int len= buf_size/f->slice_count;
  1054. if(fs->ac){
  1055. ff_init_range_encoder(&fs->c, start, len);
  1056. }else{
  1057. init_put_bits(&fs->pb, start, len);
  1058. }
  1059. }
  1060. avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1061. buf_p=buf;
  1062. for(i=0; i<f->slice_count; i++){
  1063. FFV1Context *fs= f->slice_context[i];
  1064. int bytes;
  1065. if(fs->ac){
  1066. uint8_t state=128;
  1067. put_rac(&fs->c, &state, 0);
  1068. bytes= ff_rac_terminate(&fs->c);
  1069. }else{
  1070. flush_put_bits(&fs->pb); //nicer padding FIXME
  1071. bytes= used_count + (put_bits_count(&fs->pb)+7)/8;
  1072. used_count= 0;
  1073. }
  1074. if(i>0){
  1075. av_assert0(bytes < buf_size/f->slice_count);
  1076. memmove(buf_p, fs->ac ? fs->c.bytestream_start : fs->pb.buf, bytes);
  1077. av_assert0(bytes < (1<<24));
  1078. AV_WB24(buf_p+bytes, bytes);
  1079. bytes+=3;
  1080. }
  1081. buf_p += bytes;
  1082. }
  1083. if((avctx->flags&CODEC_FLAG_PASS1) && (f->picture_number&31)==0){
  1084. int j, k, m;
  1085. char *p= avctx->stats_out;
  1086. char *end= p + STATS_OUT_SIZE;
  1087. memset(f->rc_stat, 0, sizeof(f->rc_stat));
  1088. for(i=0; i<f->quant_table_count; i++)
  1089. memset(f->rc_stat2[i], 0, f->context_count[i]*sizeof(*f->rc_stat2[i]));
  1090. for(j=0; j<f->slice_count; j++){
  1091. FFV1Context *fs= f->slice_context[j];
  1092. for(i=0; i<256; i++){
  1093. f->rc_stat[i][0] += fs->rc_stat[i][0];
  1094. f->rc_stat[i][1] += fs->rc_stat[i][1];
  1095. }
  1096. for(i=0; i<f->quant_table_count; i++){
  1097. for(k=0; k<f->context_count[i]; k++){
  1098. for(m=0; m<32; m++){
  1099. f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
  1100. f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
  1101. }
  1102. }
  1103. }
  1104. }
  1105. for(j=0; j<256; j++){
  1106. snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat[j][0], f->rc_stat[j][1]);
  1107. p+= strlen(p);
  1108. }
  1109. snprintf(p, end-p, "\n");
  1110. for(i=0; i<f->quant_table_count; i++){
  1111. for(j=0; j<f->context_count[i]; j++){
  1112. for(m=0; m<32; m++){
  1113. snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
  1114. p+= strlen(p);
  1115. }
  1116. }
  1117. }
  1118. snprintf(p, end-p, "%d\n", f->gob_count);
  1119. } else if(avctx->flags&CODEC_FLAG_PASS1)
  1120. avctx->stats_out[0] = '\0';
  1121. f->picture_number++;
  1122. return buf_p-buf;
  1123. }
  1124. #endif /* CONFIG_FFV1_ENCODER */
  1125. static av_cold int common_end(AVCodecContext *avctx){
  1126. FFV1Context *s = avctx->priv_data;
  1127. int i, j;
  1128. if (avctx->codec->decode && s->picture.data[0])
  1129. avctx->release_buffer(avctx, &s->picture);
  1130. for(j=0; j<s->slice_count; j++){
  1131. FFV1Context *fs= s->slice_context[j];
  1132. for(i=0; i<s->plane_count; i++){
  1133. PlaneContext *p= &fs->plane[i];
  1134. av_freep(&p->state);
  1135. av_freep(&p->vlc_state);
  1136. }
  1137. av_freep(&fs->sample_buffer);
  1138. }
  1139. av_freep(&avctx->stats_out);
  1140. for(j=0; j<s->quant_table_count; j++){
  1141. av_freep(&s->initial_states[j]);
  1142. for(i=0; i<s->slice_count; i++){
  1143. FFV1Context *sf= s->slice_context[i];
  1144. av_freep(&sf->rc_stat2[j]);
  1145. }
  1146. av_freep(&s->rc_stat2[j]);
  1147. }
  1148. for(i=0; i<s->slice_count; i++){
  1149. av_freep(&s->slice_context[i]);
  1150. }
  1151. return 0;
  1152. }
  1153. static av_always_inline void decode_line(FFV1Context *s, int w,
  1154. int16_t *sample[2],
  1155. int plane_index, int bits)
  1156. {
  1157. PlaneContext * const p= &s->plane[plane_index];
  1158. RangeCoder * const c= &s->c;
  1159. int x;
  1160. int run_count=0;
  1161. int run_mode=0;
  1162. int run_index= s->run_index;
  1163. for(x=0; x<w; x++){
  1164. int diff, context, sign;
  1165. context= get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
  1166. if(context < 0){
  1167. context= -context;
  1168. sign=1;
  1169. }else
  1170. sign=0;
  1171. av_assert2(context < p->context_count);
  1172. if(s->ac){
  1173. diff= get_symbol_inline(c, p->state[context], 1);
  1174. }else{
  1175. if(context == 0 && run_mode==0) run_mode=1;
  1176. if(run_mode){
  1177. if(run_count==0 && run_mode==1){
  1178. if(get_bits1(&s->gb)){
  1179. run_count = 1<<ff_log2_run[run_index];
  1180. if(x + run_count <= w) run_index++;
  1181. }else{
  1182. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  1183. else run_count=0;
  1184. if(run_index) run_index--;
  1185. run_mode=2;
  1186. }
  1187. }
  1188. run_count--;
  1189. if(run_count < 0){
  1190. run_mode=0;
  1191. run_count=0;
  1192. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  1193. if(diff>=0) diff++;
  1194. }else
  1195. diff=0;
  1196. }else
  1197. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  1198. // 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));
  1199. }
  1200. if(sign) diff= -diff;
  1201. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  1202. }
  1203. s->run_index= run_index;
  1204. }
  1205. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  1206. int x, y;
  1207. int16_t *sample[2];
  1208. sample[0]=s->sample_buffer +3;
  1209. sample[1]=s->sample_buffer+w+6+3;
  1210. s->run_index=0;
  1211. memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
  1212. for(y=0; y<h; y++){
  1213. int16_t *temp = sample[0]; //FIXME try a normal buffer
  1214. sample[0]= sample[1];
  1215. sample[1]= temp;
  1216. sample[1][-1]= sample[0][0 ];
  1217. sample[0][ w]= sample[0][w-1];
  1218. //{START_TIMER
  1219. if(s->avctx->bits_per_raw_sample <= 8){
  1220. decode_line(s, w, sample, plane_index, 8);
  1221. for(x=0; x<w; x++){
  1222. src[x + stride*y]= sample[1][x];
  1223. }
  1224. }else{
  1225. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  1226. if(s->packed_at_lsb){
  1227. for(x=0; x<w; x++){
  1228. ((uint16_t*)(src + stride*y))[x]= sample[1][x];
  1229. }
  1230. }else{
  1231. for(x=0; x<w; x++){
  1232. ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  1233. }
  1234. }
  1235. }
  1236. //STOP_TIMER("decode-line")}
  1237. }
  1238. }
  1239. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  1240. int x, y, p;
  1241. int16_t *sample[3][2];
  1242. for(x=0; x<3; x++){
  1243. sample[x][0] = s->sample_buffer + x*2 *(w+6) + 3;
  1244. sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
  1245. }
  1246. s->run_index=0;
  1247. memset(s->sample_buffer, 0, 6*(w+6)*sizeof(*s->sample_buffer));
  1248. for(y=0; y<h; y++){
  1249. for(p=0; p<3; p++){
  1250. int16_t *temp = sample[p][0]; //FIXME try a normal buffer
  1251. sample[p][0]= sample[p][1];
  1252. sample[p][1]= temp;
  1253. sample[p][1][-1]= sample[p][0][0 ];
  1254. sample[p][0][ w]= sample[p][0][w-1];
  1255. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  1256. }
  1257. for(x=0; x<w; x++){
  1258. int g= sample[0][1][x];
  1259. int b= sample[1][1][x];
  1260. int r= sample[2][1][x];
  1261. // assert(g>=0 && b>=0 && r>=0);
  1262. // assert(g<256 && b<512 && r<512);
  1263. b -= 0x100;
  1264. r -= 0x100;
  1265. g -= (b + r)>>2;
  1266. b += g;
  1267. r += g;
  1268. src[x + stride*y]= b + (g<<8) + (r<<16) + (0xFF<<24);
  1269. }
  1270. }
  1271. }
  1272. static int decode_slice(AVCodecContext *c, void *arg){
  1273. FFV1Context *fs= *(void**)arg;
  1274. FFV1Context *f= fs->avctx->priv_data;
  1275. int width = fs->slice_width;
  1276. int height= fs->slice_height;
  1277. int x= fs->slice_x;
  1278. int y= fs->slice_y;
  1279. AVFrame * const p= &f->picture;
  1280. av_assert1(width && height);
  1281. if(f->colorspace==0){
  1282. const int chroma_width = -((-width )>>f->chroma_h_shift);
  1283. const int chroma_height= -((-height)>>f->chroma_v_shift);
  1284. const int cx= x>>f->chroma_h_shift;
  1285. const int cy= y>>f->chroma_v_shift;
  1286. decode_plane(fs, p->data[0] + x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1287. decode_plane(fs, p->data[1] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1288. decode_plane(fs, p->data[2] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[2], 1);
  1289. }else{
  1290. decode_rgb_frame(fs, (uint32_t*)p->data[0] + x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
  1291. }
  1292. emms_c();
  1293. return 0;
  1294. }
  1295. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  1296. int v;
  1297. int i=0;
  1298. uint8_t state[CONTEXT_SIZE];
  1299. memset(state, 128, sizeof(state));
  1300. for(v=0; i<128 ; v++){
  1301. int len= get_symbol(c, state, 0) + 1;
  1302. if(len + i > 128) return -1;
  1303. while(len--){
  1304. quant_table[i] = scale*v;
  1305. i++;
  1306. //printf("%2d ",v);
  1307. //if(i%16==0) printf("\n");
  1308. }
  1309. }
  1310. for(i=1; i<128; i++){
  1311. quant_table[256-i]= -quant_table[i];
  1312. }
  1313. quant_table[128]= -quant_table[127];
  1314. return 2*v - 1;
  1315. }
  1316. static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
  1317. int i;
  1318. int context_count=1;
  1319. for(i=0; i<5; i++){
  1320. context_count*= read_quant_table(c, quant_table[i], context_count);
  1321. if(context_count > 32768U){
  1322. return -1;
  1323. }
  1324. }
  1325. return (context_count+1)/2;
  1326. }
  1327. static int read_extra_header(FFV1Context *f){
  1328. RangeCoder * const c= &f->c;
  1329. uint8_t state[CONTEXT_SIZE];
  1330. int i, j, k;
  1331. uint8_t state2[32][CONTEXT_SIZE];
  1332. memset(state2, 128, sizeof(state2));
  1333. memset(state, 128, sizeof(state));
  1334. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  1335. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1336. f->version= get_symbol(c, state, 0);
  1337. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1338. if(f->ac>1){
  1339. for(i=1; i<256; i++){
  1340. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1341. }
  1342. }
  1343. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1344. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1345. get_rac(c, state); //no chroma = false
  1346. f->chroma_h_shift= get_symbol(c, state, 0);
  1347. f->chroma_v_shift= get_symbol(c, state, 0);
  1348. get_rac(c, state); //transparency plane
  1349. f->plane_count= 2;
  1350. f->num_h_slices= 1 + get_symbol(c, state, 0);
  1351. f->num_v_slices= 1 + get_symbol(c, state, 0);
  1352. if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
  1353. av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
  1354. return -1;
  1355. }
  1356. f->quant_table_count= get_symbol(c, state, 0);
  1357. if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  1358. return -1;
  1359. for(i=0; i<f->quant_table_count; i++){
  1360. if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
  1361. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1362. return -1;
  1363. }
  1364. }
  1365. if(allocate_initial_states(f) < 0)
  1366. return AVERROR(ENOMEM);
  1367. for(i=0; i<f->quant_table_count; i++){
  1368. if(get_rac(c, state)){
  1369. for(j=0; j<f->context_count[i]; j++){
  1370. for(k=0; k<CONTEXT_SIZE; k++){
  1371. int pred= j ? f->initial_states[i][j-1][k] : 128;
  1372. f->initial_states[i][j][k]= (pred+get_symbol(c, state2[k], 1))&0xFF;
  1373. }
  1374. }
  1375. }
  1376. }
  1377. return 0;
  1378. }
  1379. static int read_header(FFV1Context *f){
  1380. uint8_t state[CONTEXT_SIZE];
  1381. int i, j, context_count;
  1382. RangeCoder * const c= &f->slice_context[0]->c;
  1383. memset(state, 128, sizeof(state));
  1384. if(f->version < 2){
  1385. f->version= get_symbol(c, state, 0);
  1386. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1387. if(f->ac>1){
  1388. for(i=1; i<256; i++){
  1389. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1390. }
  1391. }
  1392. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1393. if(f->version>0)
  1394. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1395. get_rac(c, state); //no chroma = false
  1396. f->chroma_h_shift= get_symbol(c, state, 0);
  1397. f->chroma_v_shift= get_symbol(c, state, 0);
  1398. get_rac(c, state); //transparency plane
  1399. f->plane_count= 2;
  1400. }
  1401. if(f->colorspace==0){
  1402. if(f->avctx->bits_per_raw_sample<=8){
  1403. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1404. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  1405. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  1406. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  1407. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  1408. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  1409. default:
  1410. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1411. return -1;
  1412. }
  1413. }else if(f->avctx->bits_per_raw_sample==9) {
  1414. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1415. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  1416. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  1417. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P9 ; f->packed_at_lsb=1; break;
  1418. default:
  1419. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1420. return -1;
  1421. }
  1422. }else if(f->avctx->bits_per_raw_sample==10) {
  1423. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1424. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  1425. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P10; f->packed_at_lsb=1; break;
  1426. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P10; f->packed_at_lsb=1; break;
  1427. default:
  1428. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1429. return -1;
  1430. }
  1431. }else {
  1432. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1433. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  1434. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  1435. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  1436. default:
  1437. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1438. return -1;
  1439. }
  1440. }
  1441. }else if(f->colorspace==1){
  1442. if(f->chroma_h_shift || f->chroma_v_shift){
  1443. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  1444. return -1;
  1445. }
  1446. f->avctx->pix_fmt= PIX_FMT_RGB32;
  1447. }else{
  1448. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  1449. return -1;
  1450. }
  1451. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  1452. if(f->version < 2){
  1453. context_count= read_quant_tables(c, f->quant_table);
  1454. if(context_count < 0){
  1455. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1456. return -1;
  1457. }
  1458. }else{
  1459. f->slice_count= get_symbol(c, state, 0);
  1460. if(f->slice_count > (unsigned)MAX_SLICES)
  1461. return -1;
  1462. }
  1463. for(j=0; j<f->slice_count; j++){
  1464. FFV1Context *fs= f->slice_context[j];
  1465. fs->ac= f->ac;
  1466. fs->packed_at_lsb= f->packed_at_lsb;
  1467. if(f->version >= 2){
  1468. fs->slice_x = get_symbol(c, state, 0) *f->width ;
  1469. fs->slice_y = get_symbol(c, state, 0) *f->height;
  1470. fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
  1471. fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
  1472. fs->slice_x /= f->num_h_slices;
  1473. fs->slice_y /= f->num_v_slices;
  1474. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  1475. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  1476. if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  1477. return -1;
  1478. if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  1479. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  1480. return -1;
  1481. }
  1482. for(i=0; i<f->plane_count; i++){
  1483. PlaneContext * const p= &fs->plane[i];
  1484. if(f->version >= 2){
  1485. int idx=get_symbol(c, state, 0);
  1486. if(idx > (unsigned)f->quant_table_count){
  1487. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  1488. return -1;
  1489. }
  1490. p->quant_table_index= idx;
  1491. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  1492. context_count= f->context_count[idx];
  1493. }else{
  1494. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  1495. }
  1496. if(p->context_count < context_count){
  1497. av_freep(&p->state);
  1498. av_freep(&p->vlc_state);
  1499. }
  1500. p->context_count= context_count;
  1501. }
  1502. }
  1503. return 0;
  1504. }
  1505. static av_cold int decode_init(AVCodecContext *avctx)
  1506. {
  1507. FFV1Context *f = avctx->priv_data;
  1508. common_init(avctx);
  1509. if(avctx->extradata && read_extra_header(f) < 0)
  1510. return -1;
  1511. if(init_slice_contexts(f) < 0)
  1512. return -1;
  1513. return 0;
  1514. }
  1515. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  1516. const uint8_t *buf = avpkt->data;
  1517. int buf_size = avpkt->size;
  1518. FFV1Context *f = avctx->priv_data;
  1519. RangeCoder * const c= &f->slice_context[0]->c;
  1520. AVFrame * const p= &f->picture;
  1521. int bytes_read, i;
  1522. uint8_t keystate= 128;
  1523. const uint8_t *buf_p;
  1524. AVFrame *picture = data;
  1525. /* release previously stored data */
  1526. if (p->data[0])
  1527. avctx->release_buffer(avctx, p);
  1528. ff_init_range_decoder(c, buf, buf_size);
  1529. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1530. p->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
  1531. if(get_rac(c, &keystate)){
  1532. p->key_frame= 1;
  1533. if(read_header(f) < 0)
  1534. return -1;
  1535. if(init_slice_state(f) < 0)
  1536. return -1;
  1537. clear_state(f);
  1538. }else{
  1539. p->key_frame= 0;
  1540. }
  1541. if(f->ac>1){
  1542. int i;
  1543. for(i=1; i<256; i++){
  1544. c->one_state[i]= f->state_transition[i];
  1545. c->zero_state[256-i]= 256-c->one_state[i];
  1546. }
  1547. }
  1548. p->reference= 0;
  1549. if(avctx->get_buffer(avctx, p) < 0){
  1550. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1551. return -1;
  1552. }
  1553. if(avctx->debug&FF_DEBUG_PICT_INFO)
  1554. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  1555. if(!f->ac){
  1556. bytes_read = c->bytestream - c->bytestream_start - 1;
  1557. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  1558. //printf("pos=%d\n", bytes_read);
  1559. init_get_bits(&f->slice_context[0]->gb, buf + bytes_read, buf_size - bytes_read);
  1560. } else {
  1561. bytes_read = 0; /* avoid warning */
  1562. }
  1563. buf_p= buf + buf_size;
  1564. for(i=f->slice_count-1; i>0; i--){
  1565. FFV1Context *fs= f->slice_context[i];
  1566. int v= AV_RB24(buf_p-3)+3;
  1567. if(buf_p - buf <= v){
  1568. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  1569. return -1;
  1570. }
  1571. buf_p -= v;
  1572. if(fs->ac){
  1573. ff_init_range_decoder(&fs->c, buf_p, v);
  1574. }else{
  1575. init_get_bits(&fs->gb, buf_p, v);
  1576. }
  1577. }
  1578. avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1579. f->picture_number++;
  1580. *picture= *p;
  1581. *data_size = sizeof(AVFrame);
  1582. return buf_size;
  1583. }
  1584. AVCodec ff_ffv1_decoder = {
  1585. "ffv1",
  1586. AVMEDIA_TYPE_VIDEO,
  1587. CODEC_ID_FFV1,
  1588. sizeof(FFV1Context),
  1589. decode_init,
  1590. NULL,
  1591. common_end,
  1592. decode_frame,
  1593. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ | CODEC_CAP_SLICE_THREADS,
  1594. NULL,
  1595. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1596. };
  1597. #if CONFIG_FFV1_ENCODER
  1598. AVCodec ff_ffv1_encoder = {
  1599. "ffv1",
  1600. AVMEDIA_TYPE_VIDEO,
  1601. CODEC_ID_FFV1,
  1602. sizeof(FFV1Context),
  1603. encode_init,
  1604. encode_frame,
  1605. common_end,
  1606. .capabilities = CODEC_CAP_SLICE_THREADS,
  1607. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, PIX_FMT_YUV420P16, PIX_FMT_YUV422P16, PIX_FMT_YUV444P16, PIX_FMT_YUV420P9, PIX_FMT_YUV420P10, PIX_FMT_YUV422P10, PIX_FMT_NONE},
  1608. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1609. };
  1610. #endif