2
0

bitstream.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "vp8/common/header.h"
  11. #include "encodemv.h"
  12. #include "vp8/common/entropymode.h"
  13. #include "vp8/common/findnearmv.h"
  14. #include "mcomp.h"
  15. #include "vp8/common/systemdependent.h"
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <limits.h>
  19. #include "vpx/vpx_encoder.h"
  20. #include "vpx_mem/vpx_mem.h"
  21. #include "bitstream.h"
  22. #include "defaultcoefcounts.h"
  23. #include "vp8/common/common.h"
  24. const int vp8cx_base_skip_false_prob[128] = {
  25. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  26. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  27. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  28. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 251, 248, 244, 240,
  29. 236, 232, 229, 225, 221, 217, 213, 208, 204, 199, 194, 190, 187, 183, 179,
  30. 175, 172, 168, 164, 160, 157, 153, 149, 145, 142, 138, 134, 130, 127, 124,
  31. 120, 117, 114, 110, 107, 104, 101, 98, 95, 92, 89, 86, 83, 80, 77,
  32. 74, 71, 68, 65, 62, 59, 56, 53, 50, 47, 44, 41, 38, 35, 32,
  33. 30, 28, 26, 24, 22, 20, 18, 16,
  34. };
  35. #if defined(SECTIONBITS_OUTPUT)
  36. unsigned __int64 Sectionbits[500];
  37. #endif
  38. #ifdef VP8_ENTROPY_STATS
  39. int intra_mode_stats[10][10][10];
  40. static unsigned int tree_update_hist[BLOCK_TYPES][COEF_BANDS]
  41. [PREV_COEF_CONTEXTS][ENTROPY_NODES][2];
  42. extern unsigned int active_section;
  43. #endif
  44. #ifdef MODE_STATS
  45. int count_mb_seg[4] = { 0, 0, 0, 0 };
  46. #endif
  47. static void update_mode(vp8_writer *const w, int n, vp8_token tok[/* n */],
  48. vp8_tree tree, vp8_prob Pnew[/* n-1 */],
  49. vp8_prob Pcur[/* n-1 */],
  50. unsigned int bct[/* n-1 */][2],
  51. const unsigned int num_events[/* n */]) {
  52. unsigned int new_b = 0, old_b = 0;
  53. int i = 0;
  54. vp8_tree_probs_from_distribution(n--, tok, tree, Pnew, bct, num_events, 256,
  55. 1);
  56. do {
  57. new_b += vp8_cost_branch(bct[i], Pnew[i]);
  58. old_b += vp8_cost_branch(bct[i], Pcur[i]);
  59. } while (++i < n);
  60. if (new_b + (n << 8) < old_b) {
  61. int j = 0;
  62. vp8_write_bit(w, 1);
  63. do {
  64. const vp8_prob p = Pnew[j];
  65. vp8_write_literal(w, Pcur[j] = p ? p : 1, 8);
  66. } while (++j < n);
  67. } else
  68. vp8_write_bit(w, 0);
  69. }
  70. static void update_mbintra_mode_probs(VP8_COMP *cpi) {
  71. VP8_COMMON *const x = &cpi->common;
  72. vp8_writer *const w = cpi->bc;
  73. {
  74. vp8_prob Pnew[VP8_YMODES - 1];
  75. unsigned int bct[VP8_YMODES - 1][2];
  76. update_mode(w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree, Pnew,
  77. x->fc.ymode_prob, bct, (unsigned int *)cpi->mb.ymode_count);
  78. }
  79. {
  80. vp8_prob Pnew[VP8_UV_MODES - 1];
  81. unsigned int bct[VP8_UV_MODES - 1][2];
  82. update_mode(w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree, Pnew,
  83. x->fc.uv_mode_prob, bct, (unsigned int *)cpi->mb.uv_mode_count);
  84. }
  85. }
  86. static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
  87. vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
  88. }
  89. static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
  90. vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
  91. }
  92. static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p) {
  93. vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
  94. }
  95. static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p) {
  96. vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
  97. }
  98. static void write_split(vp8_writer *bc, int x) {
  99. vp8_write_token(bc, vp8_mbsplit_tree, vp8_mbsplit_probs,
  100. vp8_mbsplit_encodings + x);
  101. }
  102. void vp8_pack_tokens(vp8_writer *w, const TOKENEXTRA *p, int xcount) {
  103. const TOKENEXTRA *stop = p + xcount;
  104. unsigned int split;
  105. int shift;
  106. int count = w->count;
  107. unsigned int range = w->range;
  108. unsigned int lowvalue = w->lowvalue;
  109. while (p < stop) {
  110. const int t = p->Token;
  111. vp8_token *a = vp8_coef_encodings + t;
  112. const vp8_extra_bit_struct *b = vp8_extra_bits + t;
  113. int i = 0;
  114. const unsigned char *pp = p->context_tree;
  115. int v = a->value;
  116. int n = a->Len;
  117. if (p->skip_eob_node) {
  118. n--;
  119. i = 2;
  120. }
  121. do {
  122. const int bb = (v >> --n) & 1;
  123. split = 1 + (((range - 1) * pp[i >> 1]) >> 8);
  124. i = vp8_coef_tree[i + bb];
  125. if (bb) {
  126. lowvalue += split;
  127. range = range - split;
  128. } else {
  129. range = split;
  130. }
  131. shift = vp8_norm[range];
  132. range <<= shift;
  133. count += shift;
  134. if (count >= 0) {
  135. int offset = shift - count;
  136. if ((lowvalue << (offset - 1)) & 0x80000000) {
  137. int x = w->pos - 1;
  138. while (x >= 0 && w->buffer[x] == 0xff) {
  139. w->buffer[x] = (unsigned char)0;
  140. x--;
  141. }
  142. w->buffer[x] += 1;
  143. }
  144. validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
  145. w->buffer[w->pos++] = (lowvalue >> (24 - offset));
  146. lowvalue <<= offset;
  147. shift = count;
  148. lowvalue &= 0xffffff;
  149. count -= 8;
  150. }
  151. lowvalue <<= shift;
  152. } while (n);
  153. if (b->base_val) {
  154. const int e = p->Extra, L = b->Len;
  155. if (L) {
  156. const unsigned char *proba = b->prob;
  157. const int v2 = e >> 1;
  158. int n2 = L; /* number of bits in v2, assumed nonzero */
  159. i = 0;
  160. do {
  161. const int bb = (v2 >> --n2) & 1;
  162. split = 1 + (((range - 1) * proba[i >> 1]) >> 8);
  163. i = b->tree[i + bb];
  164. if (bb) {
  165. lowvalue += split;
  166. range = range - split;
  167. } else {
  168. range = split;
  169. }
  170. shift = vp8_norm[range];
  171. range <<= shift;
  172. count += shift;
  173. if (count >= 0) {
  174. int offset = shift - count;
  175. if ((lowvalue << (offset - 1)) & 0x80000000) {
  176. int x = w->pos - 1;
  177. while (x >= 0 && w->buffer[x] == 0xff) {
  178. w->buffer[x] = (unsigned char)0;
  179. x--;
  180. }
  181. w->buffer[x] += 1;
  182. }
  183. validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
  184. w->buffer[w->pos++] = (lowvalue >> (24 - offset));
  185. lowvalue <<= offset;
  186. shift = count;
  187. lowvalue &= 0xffffff;
  188. count -= 8;
  189. }
  190. lowvalue <<= shift;
  191. } while (n2);
  192. }
  193. {
  194. split = (range + 1) >> 1;
  195. if (e & 1) {
  196. lowvalue += split;
  197. range = range - split;
  198. } else {
  199. range = split;
  200. }
  201. range <<= 1;
  202. if ((lowvalue & 0x80000000)) {
  203. int x = w->pos - 1;
  204. while (x >= 0 && w->buffer[x] == 0xff) {
  205. w->buffer[x] = (unsigned char)0;
  206. x--;
  207. }
  208. w->buffer[x] += 1;
  209. }
  210. lowvalue <<= 1;
  211. if (!++count) {
  212. count = -8;
  213. validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
  214. w->buffer[w->pos++] = (lowvalue >> 24);
  215. lowvalue &= 0xffffff;
  216. }
  217. }
  218. }
  219. ++p;
  220. }
  221. w->count = count;
  222. w->lowvalue = lowvalue;
  223. w->range = range;
  224. }
  225. static void write_partition_size(unsigned char *cx_data, int size) {
  226. signed char csize;
  227. csize = size & 0xff;
  228. *cx_data = csize;
  229. csize = (size >> 8) & 0xff;
  230. *(cx_data + 1) = csize;
  231. csize = (size >> 16) & 0xff;
  232. *(cx_data + 2) = csize;
  233. }
  234. static void pack_tokens_into_partitions(VP8_COMP *cpi, unsigned char *cx_data,
  235. unsigned char *cx_data_end,
  236. int num_part) {
  237. int i;
  238. unsigned char *ptr = cx_data;
  239. unsigned char *ptr_end = cx_data_end;
  240. vp8_writer *w;
  241. for (i = 0; i < num_part; ++i) {
  242. int mb_row;
  243. w = cpi->bc + i + 1;
  244. vp8_start_encode(w, ptr, ptr_end);
  245. for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part) {
  246. const TOKENEXTRA *p = cpi->tplist[mb_row].start;
  247. const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
  248. int tokens = (int)(stop - p);
  249. vp8_pack_tokens(w, p, tokens);
  250. }
  251. vp8_stop_encode(w);
  252. ptr += w->pos;
  253. }
  254. }
  255. #if CONFIG_MULTITHREAD
  256. static void pack_mb_row_tokens(VP8_COMP *cpi, vp8_writer *w) {
  257. int mb_row;
  258. for (mb_row = 0; mb_row < cpi->common.mb_rows; ++mb_row) {
  259. const TOKENEXTRA *p = cpi->tplist[mb_row].start;
  260. const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
  261. int tokens = (int)(stop - p);
  262. vp8_pack_tokens(w, p, tokens);
  263. }
  264. }
  265. #endif // CONFIG_MULTITHREAD
  266. static void write_mv_ref(vp8_writer *w, MB_PREDICTION_MODE m,
  267. const vp8_prob *p) {
  268. assert(NEARESTMV <= m && m <= SPLITMV);
  269. vp8_write_token(w, vp8_mv_ref_tree, p,
  270. vp8_mv_ref_encoding_array + (m - NEARESTMV));
  271. }
  272. static void write_sub_mv_ref(vp8_writer *w, B_PREDICTION_MODE m,
  273. const vp8_prob *p) {
  274. assert(LEFT4X4 <= m && m <= NEW4X4);
  275. vp8_write_token(w, vp8_sub_mv_ref_tree, p,
  276. vp8_sub_mv_ref_encoding_array + (m - LEFT4X4));
  277. }
  278. static void write_mv(vp8_writer *w, const MV *mv, const int_mv *ref,
  279. const MV_CONTEXT *mvc) {
  280. MV e;
  281. e.row = mv->row - ref->as_mv.row;
  282. e.col = mv->col - ref->as_mv.col;
  283. vp8_encode_motion_vector(w, &e, mvc);
  284. }
  285. static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi,
  286. const MACROBLOCKD *x) {
  287. /* Encode the MB segment id. */
  288. if (x->segmentation_enabled && x->update_mb_segmentation_map) {
  289. switch (mi->segment_id) {
  290. case 0:
  291. vp8_write(w, 0, x->mb_segment_tree_probs[0]);
  292. vp8_write(w, 0, x->mb_segment_tree_probs[1]);
  293. break;
  294. case 1:
  295. vp8_write(w, 0, x->mb_segment_tree_probs[0]);
  296. vp8_write(w, 1, x->mb_segment_tree_probs[1]);
  297. break;
  298. case 2:
  299. vp8_write(w, 1, x->mb_segment_tree_probs[0]);
  300. vp8_write(w, 0, x->mb_segment_tree_probs[2]);
  301. break;
  302. case 3:
  303. vp8_write(w, 1, x->mb_segment_tree_probs[0]);
  304. vp8_write(w, 1, x->mb_segment_tree_probs[2]);
  305. break;
  306. /* TRAP.. This should not happen */
  307. default:
  308. vp8_write(w, 0, x->mb_segment_tree_probs[0]);
  309. vp8_write(w, 0, x->mb_segment_tree_probs[1]);
  310. break;
  311. }
  312. }
  313. }
  314. void vp8_convert_rfct_to_prob(VP8_COMP *const cpi) {
  315. const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
  316. const int rf_intra = rfct[INTRA_FRAME];
  317. const int rf_inter =
  318. rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
  319. /* Calculate the probabilities used to code the ref frame based on usage */
  320. if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter))) {
  321. cpi->prob_intra_coded = 1;
  322. }
  323. cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
  324. if (!cpi->prob_last_coded) cpi->prob_last_coded = 1;
  325. cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
  326. ? (rfct[GOLDEN_FRAME] * 255) /
  327. (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
  328. : 128;
  329. if (!cpi->prob_gf_coded) cpi->prob_gf_coded = 1;
  330. }
  331. static void pack_inter_mode_mvs(VP8_COMP *const cpi) {
  332. VP8_COMMON *const pc = &cpi->common;
  333. vp8_writer *const w = cpi->bc;
  334. const MV_CONTEXT *mvc = pc->fc.mvc;
  335. MODE_INFO *m = pc->mi;
  336. const int mis = pc->mode_info_stride;
  337. int mb_row = -1;
  338. int prob_skip_false = 0;
  339. cpi->mb.partition_info = cpi->mb.pi;
  340. vp8_convert_rfct_to_prob(cpi);
  341. #ifdef VP8_ENTROPY_STATS
  342. active_section = 1;
  343. #endif
  344. if (pc->mb_no_coeff_skip) {
  345. int total_mbs = pc->mb_rows * pc->mb_cols;
  346. prob_skip_false = (total_mbs - cpi->mb.skip_true_count) * 256 / total_mbs;
  347. if (prob_skip_false <= 1) prob_skip_false = 1;
  348. if (prob_skip_false > 255) prob_skip_false = 255;
  349. cpi->prob_skip_false = prob_skip_false;
  350. vp8_write_literal(w, prob_skip_false, 8);
  351. }
  352. vp8_write_literal(w, cpi->prob_intra_coded, 8);
  353. vp8_write_literal(w, cpi->prob_last_coded, 8);
  354. vp8_write_literal(w, cpi->prob_gf_coded, 8);
  355. update_mbintra_mode_probs(cpi);
  356. vp8_write_mvprobs(cpi);
  357. while (++mb_row < pc->mb_rows) {
  358. int mb_col = -1;
  359. while (++mb_col < pc->mb_cols) {
  360. const MB_MODE_INFO *const mi = &m->mbmi;
  361. const MV_REFERENCE_FRAME rf = mi->ref_frame;
  362. const MB_PREDICTION_MODE mode = mi->mode;
  363. MACROBLOCKD *xd = &cpi->mb.e_mbd;
  364. /* Distance of Mb to the various image edges.
  365. * These specified to 8th pel as they are always compared to MV
  366. * values that are in 1/8th pel units
  367. */
  368. xd->mb_to_left_edge = -((mb_col * 16) << 3);
  369. xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
  370. xd->mb_to_top_edge = -((mb_row * 16) << 3);
  371. xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
  372. #ifdef VP8_ENTROPY_STATS
  373. active_section = 9;
  374. #endif
  375. if (cpi->mb.e_mbd.update_mb_segmentation_map) {
  376. write_mb_features(w, mi, &cpi->mb.e_mbd);
  377. }
  378. if (pc->mb_no_coeff_skip) {
  379. vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false);
  380. }
  381. if (rf == INTRA_FRAME) {
  382. vp8_write(w, 0, cpi->prob_intra_coded);
  383. #ifdef VP8_ENTROPY_STATS
  384. active_section = 6;
  385. #endif
  386. write_ymode(w, mode, pc->fc.ymode_prob);
  387. if (mode == B_PRED) {
  388. int j = 0;
  389. do {
  390. write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob);
  391. } while (++j < 16);
  392. }
  393. write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
  394. } else /* inter coded */
  395. {
  396. int_mv best_mv;
  397. vp8_prob mv_ref_p[VP8_MVREFS - 1];
  398. vp8_write(w, 1, cpi->prob_intra_coded);
  399. if (rf == LAST_FRAME)
  400. vp8_write(w, 0, cpi->prob_last_coded);
  401. else {
  402. vp8_write(w, 1, cpi->prob_last_coded);
  403. vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, cpi->prob_gf_coded);
  404. }
  405. {
  406. int_mv n1, n2;
  407. int ct[4];
  408. vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf,
  409. cpi->common.ref_frame_sign_bias);
  410. vp8_clamp_mv2(&best_mv, xd);
  411. vp8_mv_ref_probs(mv_ref_p, ct);
  412. #ifdef VP8_ENTROPY_STATS
  413. accum_mv_refs(mode, ct);
  414. #endif
  415. }
  416. #ifdef VP8_ENTROPY_STATS
  417. active_section = 3;
  418. #endif
  419. write_mv_ref(w, mode, mv_ref_p);
  420. switch (mode) /* new, split require MVs */
  421. {
  422. case NEWMV:
  423. #ifdef VP8_ENTROPY_STATS
  424. active_section = 5;
  425. #endif
  426. write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
  427. break;
  428. case SPLITMV: {
  429. int j = 0;
  430. #ifdef MODE_STATS
  431. ++count_mb_seg[mi->partitioning];
  432. #endif
  433. write_split(w, mi->partitioning);
  434. do {
  435. B_PREDICTION_MODE blockmode;
  436. int_mv blockmv;
  437. const int *const L = vp8_mbsplits[mi->partitioning];
  438. int k = -1; /* first block in subset j */
  439. int mv_contz;
  440. int_mv leftmv, abovemv;
  441. blockmode = cpi->mb.partition_info->bmi[j].mode;
  442. blockmv = cpi->mb.partition_info->bmi[j].mv;
  443. while (j != L[++k]) {
  444. assert(k < 16);
  445. }
  446. leftmv.as_int = left_block_mv(m, k);
  447. abovemv.as_int = above_block_mv(m, k, mis);
  448. mv_contz = vp8_mv_cont(&leftmv, &abovemv);
  449. write_sub_mv_ref(w, blockmode, vp8_sub_mv_ref_prob2[mv_contz]);
  450. if (blockmode == NEW4X4) {
  451. #ifdef VP8_ENTROPY_STATS
  452. active_section = 11;
  453. #endif
  454. write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *)mvc);
  455. }
  456. } while (++j < cpi->mb.partition_info->count);
  457. break;
  458. }
  459. default: break;
  460. }
  461. }
  462. ++m;
  463. cpi->mb.partition_info++;
  464. }
  465. ++m; /* skip L prediction border */
  466. cpi->mb.partition_info++;
  467. }
  468. }
  469. static void write_kfmodes(VP8_COMP *cpi) {
  470. vp8_writer *const bc = cpi->bc;
  471. const VP8_COMMON *const c = &cpi->common;
  472. /* const */
  473. MODE_INFO *m = c->mi;
  474. int mb_row = -1;
  475. int prob_skip_false = 0;
  476. if (c->mb_no_coeff_skip) {
  477. int total_mbs = c->mb_rows * c->mb_cols;
  478. prob_skip_false = (total_mbs - cpi->mb.skip_true_count) * 256 / total_mbs;
  479. if (prob_skip_false <= 1) prob_skip_false = 1;
  480. if (prob_skip_false >= 255) prob_skip_false = 255;
  481. cpi->prob_skip_false = prob_skip_false;
  482. vp8_write_literal(bc, prob_skip_false, 8);
  483. }
  484. while (++mb_row < c->mb_rows) {
  485. int mb_col = -1;
  486. while (++mb_col < c->mb_cols) {
  487. const int ym = m->mbmi.mode;
  488. if (cpi->mb.e_mbd.update_mb_segmentation_map) {
  489. write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd);
  490. }
  491. if (c->mb_no_coeff_skip) {
  492. vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
  493. }
  494. kfwrite_ymode(bc, ym, vp8_kf_ymode_prob);
  495. if (ym == B_PRED) {
  496. const int mis = c->mode_info_stride;
  497. int i = 0;
  498. do {
  499. const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
  500. const B_PREDICTION_MODE L = left_block_mode(m, i);
  501. const int bm = m->bmi[i].as_mode;
  502. #ifdef VP8_ENTROPY_STATS
  503. ++intra_mode_stats[A][L][bm];
  504. #endif
  505. write_bmode(bc, bm, vp8_kf_bmode_prob[A][L]);
  506. } while (++i < 16);
  507. }
  508. write_uv_mode(bc, (m++)->mbmi.uv_mode, vp8_kf_uv_mode_prob);
  509. }
  510. m++; /* skip L prediction border */
  511. }
  512. }
  513. #if 0
  514. /* This function is used for debugging probability trees. */
  515. static void print_prob_tree(vp8_prob
  516. coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES])
  517. {
  518. /* print coef probability tree */
  519. int i,j,k,l;
  520. FILE* f = fopen("enc_tree_probs.txt", "a");
  521. fprintf(f, "{\n");
  522. for (i = 0; i < BLOCK_TYPES; ++i)
  523. {
  524. fprintf(f, " {\n");
  525. for (j = 0; j < COEF_BANDS; ++j)
  526. {
  527. fprintf(f, " {\n");
  528. for (k = 0; k < PREV_COEF_CONTEXTS; ++k)
  529. {
  530. fprintf(f, " {");
  531. for (l = 0; l < ENTROPY_NODES; ++l)
  532. {
  533. fprintf(f, "%3u, ",
  534. (unsigned int)(coef_probs [i][j][k][l]));
  535. }
  536. fprintf(f, " }\n");
  537. }
  538. fprintf(f, " }\n");
  539. }
  540. fprintf(f, " }\n");
  541. }
  542. fprintf(f, "}\n");
  543. fclose(f);
  544. }
  545. #endif
  546. static void sum_probs_over_prev_coef_context(
  547. const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
  548. unsigned int *out) {
  549. int i, j;
  550. for (i = 0; i < MAX_ENTROPY_TOKENS; ++i) {
  551. for (j = 0; j < PREV_COEF_CONTEXTS; ++j) {
  552. const unsigned int tmp = out[i];
  553. out[i] += probs[j][i];
  554. /* check for wrap */
  555. if (out[i] < tmp) out[i] = UINT_MAX;
  556. }
  557. }
  558. }
  559. static int prob_update_savings(const unsigned int *ct, const vp8_prob oldp,
  560. const vp8_prob newp, const vp8_prob upd) {
  561. const int old_b = vp8_cost_branch(ct, oldp);
  562. const int new_b = vp8_cost_branch(ct, newp);
  563. const int update_b = 8 + ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
  564. return old_b - new_b - update_b;
  565. }
  566. static int independent_coef_context_savings(VP8_COMP *cpi) {
  567. MACROBLOCK *const x = &cpi->mb;
  568. int savings = 0;
  569. int i = 0;
  570. do {
  571. int j = 0;
  572. do {
  573. int k = 0;
  574. unsigned int prev_coef_count_sum[MAX_ENTROPY_TOKENS] = { 0 };
  575. int prev_coef_savings[MAX_ENTROPY_TOKENS] = { 0 };
  576. const unsigned int(*probs)[MAX_ENTROPY_TOKENS];
  577. /* Calculate new probabilities given the constraint that
  578. * they must be equal over the prev coef contexts
  579. */
  580. probs = (const unsigned int(*)[MAX_ENTROPY_TOKENS])x->coef_counts[i][j];
  581. /* Reset to default probabilities at key frames */
  582. if (cpi->common.frame_type == KEY_FRAME) {
  583. probs = default_coef_counts[i][j];
  584. }
  585. sum_probs_over_prev_coef_context(probs, prev_coef_count_sum);
  586. do {
  587. /* at every context */
  588. /* calc probs and branch cts for this frame only */
  589. int t = 0; /* token/prob index */
  590. vp8_tree_probs_from_distribution(
  591. MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
  592. cpi->frame_coef_probs[i][j][k], cpi->frame_branch_ct[i][j][k],
  593. prev_coef_count_sum, 256, 1);
  594. do {
  595. const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
  596. const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
  597. const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
  598. const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  599. const int s = prob_update_savings(ct, oldp, newp, upd);
  600. if (cpi->common.frame_type != KEY_FRAME ||
  601. (cpi->common.frame_type == KEY_FRAME && newp != oldp)) {
  602. prev_coef_savings[t] += s;
  603. }
  604. } while (++t < ENTROPY_NODES);
  605. } while (++k < PREV_COEF_CONTEXTS);
  606. k = 0;
  607. do {
  608. /* We only update probabilities if we can save bits, except
  609. * for key frames where we have to update all probabilities
  610. * to get the equal probabilities across the prev coef
  611. * contexts.
  612. */
  613. if (prev_coef_savings[k] > 0 || cpi->common.frame_type == KEY_FRAME) {
  614. savings += prev_coef_savings[k];
  615. }
  616. } while (++k < ENTROPY_NODES);
  617. } while (++j < COEF_BANDS);
  618. } while (++i < BLOCK_TYPES);
  619. return savings;
  620. }
  621. static int default_coef_context_savings(VP8_COMP *cpi) {
  622. MACROBLOCK *const x = &cpi->mb;
  623. int savings = 0;
  624. int i = 0;
  625. do {
  626. int j = 0;
  627. do {
  628. int k = 0;
  629. do {
  630. /* at every context */
  631. /* calc probs and branch cts for this frame only */
  632. int t = 0; /* token/prob index */
  633. vp8_tree_probs_from_distribution(
  634. MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
  635. cpi->frame_coef_probs[i][j][k], cpi->frame_branch_ct[i][j][k],
  636. x->coef_counts[i][j][k], 256, 1);
  637. do {
  638. const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
  639. const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
  640. const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
  641. const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  642. const int s = prob_update_savings(ct, oldp, newp, upd);
  643. if (s > 0) {
  644. savings += s;
  645. }
  646. } while (++t < ENTROPY_NODES);
  647. } while (++k < PREV_COEF_CONTEXTS);
  648. } while (++j < COEF_BANDS);
  649. } while (++i < BLOCK_TYPES);
  650. return savings;
  651. }
  652. void vp8_calc_ref_frame_costs(int *ref_frame_cost, int prob_intra,
  653. int prob_last, int prob_garf) {
  654. assert(prob_intra >= 0);
  655. assert(prob_intra <= 255);
  656. assert(prob_last >= 0);
  657. assert(prob_last <= 255);
  658. assert(prob_garf >= 0);
  659. assert(prob_garf <= 255);
  660. ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(prob_intra);
  661. ref_frame_cost[LAST_FRAME] =
  662. vp8_cost_one(prob_intra) + vp8_cost_zero(prob_last);
  663. ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(prob_intra) +
  664. vp8_cost_one(prob_last) +
  665. vp8_cost_zero(prob_garf);
  666. ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(prob_intra) +
  667. vp8_cost_one(prob_last) +
  668. vp8_cost_one(prob_garf);
  669. }
  670. int vp8_estimate_entropy_savings(VP8_COMP *cpi) {
  671. int savings = 0;
  672. const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
  673. const int rf_intra = rfct[INTRA_FRAME];
  674. const int rf_inter =
  675. rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
  676. int new_intra, new_last, new_garf, oldtotal, newtotal;
  677. int ref_frame_cost[MAX_REF_FRAMES];
  678. vp8_clear_system_state();
  679. if (cpi->common.frame_type != KEY_FRAME) {
  680. if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter))) new_intra = 1;
  681. new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
  682. new_garf = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
  683. ? (rfct[GOLDEN_FRAME] * 255) /
  684. (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
  685. : 128;
  686. vp8_calc_ref_frame_costs(ref_frame_cost, new_intra, new_last, new_garf);
  687. newtotal = rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
  688. rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
  689. rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
  690. rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
  691. /* old costs */
  692. vp8_calc_ref_frame_costs(ref_frame_cost, cpi->prob_intra_coded,
  693. cpi->prob_last_coded, cpi->prob_gf_coded);
  694. oldtotal = rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
  695. rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
  696. rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
  697. rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
  698. savings += (oldtotal - newtotal) / 256;
  699. }
  700. if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
  701. savings += independent_coef_context_savings(cpi);
  702. } else {
  703. savings += default_coef_context_savings(cpi);
  704. }
  705. return savings;
  706. }
  707. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  708. int vp8_update_coef_context(VP8_COMP *cpi) {
  709. int savings = 0;
  710. if (cpi->common.frame_type == KEY_FRAME) {
  711. /* Reset to default counts/probabilities at key frames */
  712. vp8_copy(cpi->mb.coef_counts, default_coef_counts);
  713. }
  714. if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
  715. savings += independent_coef_context_savings(cpi);
  716. else
  717. savings += default_coef_context_savings(cpi);
  718. return savings;
  719. }
  720. #endif
  721. void vp8_update_coef_probs(VP8_COMP *cpi) {
  722. int i = 0;
  723. #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  724. vp8_writer *const w = cpi->bc;
  725. #endif
  726. int savings = 0;
  727. vp8_clear_system_state();
  728. do {
  729. int j = 0;
  730. do {
  731. int k = 0;
  732. int prev_coef_savings[ENTROPY_NODES] = { 0 };
  733. if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
  734. for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
  735. int t; /* token/prob index */
  736. for (t = 0; t < ENTROPY_NODES; ++t) {
  737. const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
  738. const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
  739. const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
  740. const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  741. prev_coef_savings[t] += prob_update_savings(ct, oldp, newp, upd);
  742. }
  743. }
  744. k = 0;
  745. }
  746. do {
  747. /* note: use result from vp8_estimate_entropy_savings, so no
  748. * need to call vp8_tree_probs_from_distribution here.
  749. */
  750. /* at every context */
  751. /* calc probs and branch cts for this frame only */
  752. int t = 0; /* token/prob index */
  753. do {
  754. const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
  755. vp8_prob *Pold = cpi->common.fc.coef_probs[i][j][k] + t;
  756. const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  757. int s = prev_coef_savings[t];
  758. int u = 0;
  759. if (!(cpi->oxcf.error_resilient_mode &
  760. VPX_ERROR_RESILIENT_PARTITIONS)) {
  761. s = prob_update_savings(cpi->frame_branch_ct[i][j][k][t], *Pold,
  762. newp, upd);
  763. }
  764. if (s > 0) u = 1;
  765. /* Force updates on key frames if the new is different,
  766. * so that we can be sure we end up with equal probabilities
  767. * over the prev coef contexts.
  768. */
  769. if ((cpi->oxcf.error_resilient_mode &
  770. VPX_ERROR_RESILIENT_PARTITIONS) &&
  771. cpi->common.frame_type == KEY_FRAME && newp != *Pold) {
  772. u = 1;
  773. }
  774. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  775. cpi->update_probs[i][j][k][t] = u;
  776. #else
  777. vp8_write(w, u, upd);
  778. #endif
  779. #ifdef VP8_ENTROPY_STATS
  780. ++tree_update_hist[i][j][k][t][u];
  781. #endif
  782. if (u) {
  783. /* send/use new probability */
  784. *Pold = newp;
  785. #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  786. vp8_write_literal(w, newp, 8);
  787. #endif
  788. savings += s;
  789. }
  790. } while (++t < ENTROPY_NODES);
  791. /* Accum token counts for generation of default statistics */
  792. #ifdef VP8_ENTROPY_STATS
  793. t = 0;
  794. do {
  795. context_counters[i][j][k][t] += cpi->coef_counts[i][j][k][t];
  796. } while (++t < MAX_ENTROPY_TOKENS);
  797. #endif
  798. } while (++k < PREV_COEF_CONTEXTS);
  799. } while (++j < COEF_BANDS);
  800. } while (++i < BLOCK_TYPES);
  801. }
  802. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  803. static void pack_coef_probs(VP8_COMP *cpi) {
  804. int i = 0;
  805. vp8_writer *const w = cpi->bc;
  806. do {
  807. int j = 0;
  808. do {
  809. int k = 0;
  810. do {
  811. int t = 0; /* token/prob index */
  812. do {
  813. const vp8_prob newp = cpi->common.fc.coef_probs[i][j][k][t];
  814. const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  815. const char u = cpi->update_probs[i][j][k][t];
  816. vp8_write(w, u, upd);
  817. if (u) {
  818. /* send/use new probability */
  819. vp8_write_literal(w, newp, 8);
  820. }
  821. } while (++t < ENTROPY_NODES);
  822. } while (++k < PREV_COEF_CONTEXTS);
  823. } while (++j < COEF_BANDS);
  824. } while (++i < BLOCK_TYPES);
  825. }
  826. #endif
  827. #ifdef PACKET_TESTING
  828. FILE *vpxlogc = 0;
  829. #endif
  830. static void put_delta_q(vp8_writer *bc, int delta_q) {
  831. if (delta_q != 0) {
  832. vp8_write_bit(bc, 1);
  833. vp8_write_literal(bc, abs(delta_q), 4);
  834. if (delta_q < 0)
  835. vp8_write_bit(bc, 1);
  836. else
  837. vp8_write_bit(bc, 0);
  838. } else
  839. vp8_write_bit(bc, 0);
  840. }
  841. void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest,
  842. unsigned char *dest_end, size_t *size) {
  843. int i, j;
  844. VP8_HEADER oh;
  845. VP8_COMMON *const pc = &cpi->common;
  846. vp8_writer *const bc = cpi->bc;
  847. MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  848. int extra_bytes_packed = 0;
  849. unsigned char *cx_data = dest;
  850. unsigned char *cx_data_end = dest_end;
  851. const int *mb_feature_data_bits;
  852. oh.show_frame = (int)pc->show_frame;
  853. oh.type = (int)pc->frame_type;
  854. oh.version = pc->version;
  855. oh.first_partition_length_in_bytes = 0;
  856. mb_feature_data_bits = vp8_mb_feature_data_bits;
  857. bc[0].error = &pc->error;
  858. validate_buffer(cx_data, 3, cx_data_end, &cpi->common.error);
  859. cx_data += 3;
  860. #if defined(SECTIONBITS_OUTPUT)
  861. Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256;
  862. #endif
  863. /* every keyframe send startcode, width, height, scale factor, clamp
  864. * and color type
  865. */
  866. if (oh.type == KEY_FRAME) {
  867. int v;
  868. validate_buffer(cx_data, 7, cx_data_end, &cpi->common.error);
  869. /* Start / synch code */
  870. cx_data[0] = 0x9D;
  871. cx_data[1] = 0x01;
  872. cx_data[2] = 0x2a;
  873. v = (pc->horiz_scale << 14) | pc->Width;
  874. cx_data[3] = v;
  875. cx_data[4] = v >> 8;
  876. v = (pc->vert_scale << 14) | pc->Height;
  877. cx_data[5] = v;
  878. cx_data[6] = v >> 8;
  879. extra_bytes_packed = 7;
  880. cx_data += extra_bytes_packed;
  881. vp8_start_encode(bc, cx_data, cx_data_end);
  882. /* signal clr type */
  883. vp8_write_bit(bc, 0);
  884. vp8_write_bit(bc, pc->clamp_type);
  885. } else {
  886. vp8_start_encode(bc, cx_data, cx_data_end);
  887. }
  888. /* Signal whether or not Segmentation is enabled */
  889. vp8_write_bit(bc, xd->segmentation_enabled);
  890. /* Indicate which features are enabled */
  891. if (xd->segmentation_enabled) {
  892. /* Signal whether or not the segmentation map is being updated. */
  893. vp8_write_bit(bc, xd->update_mb_segmentation_map);
  894. vp8_write_bit(bc, xd->update_mb_segmentation_data);
  895. if (xd->update_mb_segmentation_data) {
  896. signed char Data;
  897. vp8_write_bit(bc, xd->mb_segement_abs_delta);
  898. /* For each segmentation feature (Quant and loop filter level) */
  899. for (i = 0; i < MB_LVL_MAX; ++i) {
  900. /* For each of the segments */
  901. for (j = 0; j < MAX_MB_SEGMENTS; ++j) {
  902. Data = xd->segment_feature_data[i][j];
  903. /* Frame level data */
  904. if (Data) {
  905. vp8_write_bit(bc, 1);
  906. if (Data < 0) {
  907. Data = -Data;
  908. vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
  909. vp8_write_bit(bc, 1);
  910. } else {
  911. vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
  912. vp8_write_bit(bc, 0);
  913. }
  914. } else
  915. vp8_write_bit(bc, 0);
  916. }
  917. }
  918. }
  919. if (xd->update_mb_segmentation_map) {
  920. /* Write the probs used to decode the segment id for each mb */
  921. for (i = 0; i < MB_FEATURE_TREE_PROBS; ++i) {
  922. int Data = xd->mb_segment_tree_probs[i];
  923. if (Data != 255) {
  924. vp8_write_bit(bc, 1);
  925. vp8_write_literal(bc, Data, 8);
  926. } else
  927. vp8_write_bit(bc, 0);
  928. }
  929. }
  930. }
  931. vp8_write_bit(bc, pc->filter_type);
  932. vp8_write_literal(bc, pc->filter_level, 6);
  933. vp8_write_literal(bc, pc->sharpness_level, 3);
  934. /* Write out loop filter deltas applied at the MB level based on mode
  935. * or ref frame (if they are enabled).
  936. */
  937. vp8_write_bit(bc, xd->mode_ref_lf_delta_enabled);
  938. if (xd->mode_ref_lf_delta_enabled) {
  939. /* Do the deltas need to be updated */
  940. int send_update =
  941. xd->mode_ref_lf_delta_update || cpi->oxcf.error_resilient_mode;
  942. vp8_write_bit(bc, send_update);
  943. if (send_update) {
  944. int Data;
  945. /* Send update */
  946. for (i = 0; i < MAX_REF_LF_DELTAS; ++i) {
  947. Data = xd->ref_lf_deltas[i];
  948. /* Frame level data */
  949. if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i] ||
  950. cpi->oxcf.error_resilient_mode) {
  951. xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i];
  952. vp8_write_bit(bc, 1);
  953. if (Data > 0) {
  954. vp8_write_literal(bc, (Data & 0x3F), 6);
  955. vp8_write_bit(bc, 0); /* sign */
  956. } else {
  957. Data = -Data;
  958. vp8_write_literal(bc, (Data & 0x3F), 6);
  959. vp8_write_bit(bc, 1); /* sign */
  960. }
  961. } else
  962. vp8_write_bit(bc, 0);
  963. }
  964. /* Send update */
  965. for (i = 0; i < MAX_MODE_LF_DELTAS; ++i) {
  966. Data = xd->mode_lf_deltas[i];
  967. if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i] ||
  968. cpi->oxcf.error_resilient_mode) {
  969. xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i];
  970. vp8_write_bit(bc, 1);
  971. if (Data > 0) {
  972. vp8_write_literal(bc, (Data & 0x3F), 6);
  973. vp8_write_bit(bc, 0); /* sign */
  974. } else {
  975. Data = -Data;
  976. vp8_write_literal(bc, (Data & 0x3F), 6);
  977. vp8_write_bit(bc, 1); /* sign */
  978. }
  979. } else
  980. vp8_write_bit(bc, 0);
  981. }
  982. }
  983. }
  984. /* signal here is multi token partition is enabled */
  985. vp8_write_literal(bc, pc->multi_token_partition, 2);
  986. /* Frame Qbaseline quantizer index */
  987. vp8_write_literal(bc, pc->base_qindex, 7);
  988. /* Transmit Dc, Second order and Uv quantizer delta information */
  989. put_delta_q(bc, pc->y1dc_delta_q);
  990. put_delta_q(bc, pc->y2dc_delta_q);
  991. put_delta_q(bc, pc->y2ac_delta_q);
  992. put_delta_q(bc, pc->uvdc_delta_q);
  993. put_delta_q(bc, pc->uvac_delta_q);
  994. /* When there is a key frame all reference buffers are updated using
  995. * the new key frame
  996. */
  997. if (pc->frame_type != KEY_FRAME) {
  998. /* Should the GF or ARF be updated using the transmitted frame
  999. * or buffer
  1000. */
  1001. vp8_write_bit(bc, pc->refresh_golden_frame);
  1002. vp8_write_bit(bc, pc->refresh_alt_ref_frame);
  1003. /* If not being updated from current frame should either GF or ARF
  1004. * be updated from another buffer
  1005. */
  1006. if (!pc->refresh_golden_frame)
  1007. vp8_write_literal(bc, pc->copy_buffer_to_gf, 2);
  1008. if (!pc->refresh_alt_ref_frame)
  1009. vp8_write_literal(bc, pc->copy_buffer_to_arf, 2);
  1010. /* Indicate reference frame sign bias for Golden and ARF frames
  1011. * (always 0 for last frame buffer)
  1012. */
  1013. vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]);
  1014. vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]);
  1015. }
  1016. #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  1017. if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
  1018. if (pc->frame_type == KEY_FRAME) {
  1019. pc->refresh_entropy_probs = 1;
  1020. } else {
  1021. pc->refresh_entropy_probs = 0;
  1022. }
  1023. }
  1024. #endif
  1025. vp8_write_bit(bc, pc->refresh_entropy_probs);
  1026. if (pc->frame_type != KEY_FRAME) vp8_write_bit(bc, pc->refresh_last_frame);
  1027. #ifdef VP8_ENTROPY_STATS
  1028. if (pc->frame_type == INTER_FRAME)
  1029. active_section = 0;
  1030. else
  1031. active_section = 7;
  1032. #endif
  1033. vp8_clear_system_state();
  1034. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  1035. pack_coef_probs(cpi);
  1036. #else
  1037. if (pc->refresh_entropy_probs == 0) {
  1038. /* save a copy for later refresh */
  1039. memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc));
  1040. }
  1041. vp8_update_coef_probs(cpi);
  1042. #endif
  1043. #ifdef VP8_ENTROPY_STATS
  1044. active_section = 2;
  1045. #endif
  1046. /* Write out the mb_no_coeff_skip flag */
  1047. vp8_write_bit(bc, pc->mb_no_coeff_skip);
  1048. if (pc->frame_type == KEY_FRAME) {
  1049. write_kfmodes(cpi);
  1050. #ifdef VP8_ENTROPY_STATS
  1051. active_section = 8;
  1052. #endif
  1053. } else {
  1054. pack_inter_mode_mvs(cpi);
  1055. #ifdef VP8_ENTROPY_STATS
  1056. active_section = 1;
  1057. #endif
  1058. }
  1059. vp8_stop_encode(bc);
  1060. cx_data += bc->pos;
  1061. oh.first_partition_length_in_bytes = cpi->bc->pos;
  1062. /* update frame tag */
  1063. {
  1064. int v = (oh.first_partition_length_in_bytes << 5) | (oh.show_frame << 4) |
  1065. (oh.version << 1) | oh.type;
  1066. dest[0] = v;
  1067. dest[1] = v >> 8;
  1068. dest[2] = v >> 16;
  1069. }
  1070. *size = VP8_HEADER_SIZE + extra_bytes_packed + cpi->bc->pos;
  1071. cpi->partition_sz[0] = (unsigned int)*size;
  1072. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  1073. {
  1074. const int num_part = (1 << pc->multi_token_partition);
  1075. unsigned char *dp = cpi->partition_d[0] + cpi->partition_sz[0];
  1076. if (num_part > 1) {
  1077. /* write token part sizes (all but last) if more than 1 */
  1078. validate_buffer(dp, 3 * (num_part - 1), cpi->partition_d_end[0],
  1079. &pc->error);
  1080. cpi->partition_sz[0] += 3 * (num_part - 1);
  1081. for (i = 1; i < num_part; ++i) {
  1082. write_partition_size(dp, cpi->partition_sz[i]);
  1083. dp += 3;
  1084. }
  1085. }
  1086. if (!cpi->output_partition) {
  1087. /* concatenate partition buffers */
  1088. for (i = 0; i < num_part; ++i) {
  1089. memmove(dp, cpi->partition_d[i + 1], cpi->partition_sz[i + 1]);
  1090. cpi->partition_d[i + 1] = dp;
  1091. dp += cpi->partition_sz[i + 1];
  1092. }
  1093. }
  1094. /* update total size */
  1095. *size = 0;
  1096. for (i = 0; i < num_part + 1; ++i) {
  1097. *size += cpi->partition_sz[i];
  1098. }
  1099. }
  1100. #else
  1101. if (pc->multi_token_partition != ONE_PARTITION) {
  1102. int num_part = 1 << pc->multi_token_partition;
  1103. /* partition size table at the end of first partition */
  1104. cpi->partition_sz[0] += 3 * (num_part - 1);
  1105. *size += 3 * (num_part - 1);
  1106. validate_buffer(cx_data, 3 * (num_part - 1), cx_data_end, &pc->error);
  1107. for (i = 1; i < num_part + 1; ++i) {
  1108. cpi->bc[i].error = &pc->error;
  1109. }
  1110. pack_tokens_into_partitions(cpi, cx_data + 3 * (num_part - 1), cx_data_end,
  1111. num_part);
  1112. for (i = 1; i < num_part; ++i) {
  1113. cpi->partition_sz[i] = cpi->bc[i].pos;
  1114. write_partition_size(cx_data, cpi->partition_sz[i]);
  1115. cx_data += 3;
  1116. *size += cpi->partition_sz[i]; /* add to total */
  1117. }
  1118. /* add last partition to total size */
  1119. cpi->partition_sz[i] = cpi->bc[i].pos;
  1120. *size += cpi->partition_sz[i];
  1121. } else {
  1122. bc[1].error = &pc->error;
  1123. vp8_start_encode(&cpi->bc[1], cx_data, cx_data_end);
  1124. #if CONFIG_MULTITHREAD
  1125. if (cpi->b_multi_threaded) {
  1126. pack_mb_row_tokens(cpi, &cpi->bc[1]);
  1127. } else {
  1128. vp8_pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
  1129. }
  1130. #else
  1131. vp8_pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
  1132. #endif // CONFIG_MULTITHREAD
  1133. vp8_stop_encode(&cpi->bc[1]);
  1134. *size += cpi->bc[1].pos;
  1135. cpi->partition_sz[1] = cpi->bc[1].pos;
  1136. }
  1137. #endif
  1138. }
  1139. #ifdef VP8_ENTROPY_STATS
  1140. void print_tree_update_probs() {
  1141. int i, j, k, l;
  1142. FILE *f = fopen("context.c", "a");
  1143. int Sum;
  1144. fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
  1145. fprintf(f,
  1146. "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] "
  1147. "[PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n");
  1148. for (i = 0; i < BLOCK_TYPES; ++i) {
  1149. fprintf(f, " { \n");
  1150. for (j = 0; j < COEF_BANDS; ++j) {
  1151. fprintf(f, " {\n");
  1152. for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
  1153. fprintf(f, " {");
  1154. for (l = 0; l < ENTROPY_NODES; ++l) {
  1155. Sum =
  1156. tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1];
  1157. if (Sum > 0) {
  1158. if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0)
  1159. fprintf(f, "%3ld, ",
  1160. (tree_update_hist[i][j][k][l][0] * 255) / Sum);
  1161. else
  1162. fprintf(f, "%3ld, ", 1);
  1163. } else
  1164. fprintf(f, "%3ld, ", 128);
  1165. }
  1166. fprintf(f, "},\n");
  1167. }
  1168. fprintf(f, " },\n");
  1169. }
  1170. fprintf(f, " },\n");
  1171. }
  1172. fprintf(f, "};\n");
  1173. fclose(f);
  1174. }
  1175. #endif