vp9_bitstream.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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 <assert.h>
  11. #include <stdio.h>
  12. #include <limits.h>
  13. #include "vpx/vpx_encoder.h"
  14. #include "vpx_dsp/bitwriter_buffer.h"
  15. #include "vpx_dsp/vpx_dsp_common.h"
  16. #include "vpx_mem/vpx_mem.h"
  17. #include "vpx_ports/mem_ops.h"
  18. #include "vpx_ports/system_state.h"
  19. #include "vp9/common/vp9_entropy.h"
  20. #include "vp9/common/vp9_entropymode.h"
  21. #include "vp9/common/vp9_entropymv.h"
  22. #include "vp9/common/vp9_mvref_common.h"
  23. #include "vp9/common/vp9_pred_common.h"
  24. #include "vp9/common/vp9_seg_common.h"
  25. #include "vp9/common/vp9_tile_common.h"
  26. #include "vp9/encoder/vp9_cost.h"
  27. #include "vp9/encoder/vp9_bitstream.h"
  28. #include "vp9/encoder/vp9_encodemv.h"
  29. #include "vp9/encoder/vp9_mcomp.h"
  30. #include "vp9/encoder/vp9_segmentation.h"
  31. #include "vp9/encoder/vp9_subexp.h"
  32. #include "vp9/encoder/vp9_tokenize.h"
  33. static const struct vp9_token intra_mode_encodings[INTRA_MODES] = {
  34. { 0, 1 }, { 6, 3 }, { 28, 5 }, { 30, 5 }, { 58, 6 },
  35. { 59, 6 }, { 126, 7 }, { 127, 7 }, { 62, 6 }, { 2, 2 }
  36. };
  37. static const struct vp9_token switchable_interp_encodings[SWITCHABLE_FILTERS] =
  38. { { 0, 1 }, { 2, 2 }, { 3, 2 } };
  39. static const struct vp9_token partition_encodings[PARTITION_TYPES] = {
  40. { 0, 1 }, { 2, 2 }, { 6, 3 }, { 7, 3 }
  41. };
  42. static const struct vp9_token inter_mode_encodings[INTER_MODES] = {
  43. { 2, 2 }, { 6, 3 }, { 0, 1 }, { 7, 3 }
  44. };
  45. static void write_intra_mode(vpx_writer *w, PREDICTION_MODE mode,
  46. const vpx_prob *probs) {
  47. vp9_write_token(w, vp9_intra_mode_tree, probs, &intra_mode_encodings[mode]);
  48. }
  49. static void write_inter_mode(vpx_writer *w, PREDICTION_MODE mode,
  50. const vpx_prob *probs) {
  51. assert(is_inter_mode(mode));
  52. vp9_write_token(w, vp9_inter_mode_tree, probs,
  53. &inter_mode_encodings[INTER_OFFSET(mode)]);
  54. }
  55. static void encode_unsigned_max(struct vpx_write_bit_buffer *wb, int data,
  56. int max) {
  57. vpx_wb_write_literal(wb, data, get_unsigned_bits(max));
  58. }
  59. static void prob_diff_update(const vpx_tree_index *tree,
  60. vpx_prob probs[/*n - 1*/],
  61. const unsigned int counts[/*n - 1*/], int n,
  62. vpx_writer *w) {
  63. int i;
  64. unsigned int branch_ct[32][2];
  65. // Assuming max number of probabilities <= 32
  66. assert(n <= 32);
  67. vp9_tree_probs_from_distribution(tree, branch_ct, counts);
  68. for (i = 0; i < n - 1; ++i)
  69. vp9_cond_prob_diff_update(w, &probs[i], branch_ct[i]);
  70. }
  71. static void write_selected_tx_size(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  72. vpx_writer *w) {
  73. TX_SIZE tx_size = xd->mi[0]->tx_size;
  74. BLOCK_SIZE bsize = xd->mi[0]->sb_type;
  75. const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
  76. const vpx_prob *const tx_probs =
  77. get_tx_probs2(max_tx_size, xd, &cm->fc->tx_probs);
  78. vpx_write(w, tx_size != TX_4X4, tx_probs[0]);
  79. if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
  80. vpx_write(w, tx_size != TX_8X8, tx_probs[1]);
  81. if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
  82. vpx_write(w, tx_size != TX_16X16, tx_probs[2]);
  83. }
  84. }
  85. static int write_skip(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  86. int segment_id, const MODE_INFO *mi, vpx_writer *w) {
  87. if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
  88. return 1;
  89. } else {
  90. const int skip = mi->skip;
  91. vpx_write(w, skip, vp9_get_skip_prob(cm, xd));
  92. return skip;
  93. }
  94. }
  95. static void update_skip_probs(VP9_COMMON *cm, vpx_writer *w,
  96. FRAME_COUNTS *counts) {
  97. int k;
  98. for (k = 0; k < SKIP_CONTEXTS; ++k)
  99. vp9_cond_prob_diff_update(w, &cm->fc->skip_probs[k], counts->skip[k]);
  100. }
  101. static void update_switchable_interp_probs(VP9_COMMON *cm, vpx_writer *w,
  102. FRAME_COUNTS *counts) {
  103. int j;
  104. for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
  105. prob_diff_update(vp9_switchable_interp_tree,
  106. cm->fc->switchable_interp_prob[j],
  107. counts->switchable_interp[j], SWITCHABLE_FILTERS, w);
  108. }
  109. static void pack_mb_tokens(vpx_writer *w, TOKENEXTRA **tp,
  110. const TOKENEXTRA *const stop,
  111. vpx_bit_depth_t bit_depth) {
  112. const TOKENEXTRA *p;
  113. const vp9_extra_bit *const extra_bits =
  114. #if CONFIG_VP9_HIGHBITDEPTH
  115. (bit_depth == VPX_BITS_12)
  116. ? vp9_extra_bits_high12
  117. : (bit_depth == VPX_BITS_10) ? vp9_extra_bits_high10 : vp9_extra_bits;
  118. #else
  119. vp9_extra_bits;
  120. (void)bit_depth;
  121. #endif // CONFIG_VP9_HIGHBITDEPTH
  122. for (p = *tp; p < stop && p->token != EOSB_TOKEN; ++p) {
  123. if (p->token == EOB_TOKEN) {
  124. vpx_write(w, 0, p->context_tree[0]);
  125. continue;
  126. }
  127. vpx_write(w, 1, p->context_tree[0]);
  128. while (p->token == ZERO_TOKEN) {
  129. vpx_write(w, 0, p->context_tree[1]);
  130. ++p;
  131. if (p == stop || p->token == EOSB_TOKEN) {
  132. *tp = (TOKENEXTRA *)(uintptr_t)p + (p->token == EOSB_TOKEN);
  133. return;
  134. }
  135. }
  136. {
  137. const int t = p->token;
  138. const vpx_prob *const context_tree = p->context_tree;
  139. assert(t != ZERO_TOKEN);
  140. assert(t != EOB_TOKEN);
  141. assert(t != EOSB_TOKEN);
  142. vpx_write(w, 1, context_tree[1]);
  143. if (t == ONE_TOKEN) {
  144. vpx_write(w, 0, context_tree[2]);
  145. vpx_write_bit(w, p->extra & 1);
  146. } else { // t >= TWO_TOKEN && t < EOB_TOKEN
  147. const struct vp9_token *const a = &vp9_coef_encodings[t];
  148. const int v = a->value;
  149. const int n = a->len;
  150. const int e = p->extra;
  151. vpx_write(w, 1, context_tree[2]);
  152. vp9_write_tree(w, vp9_coef_con_tree,
  153. vp9_pareto8_full[context_tree[PIVOT_NODE] - 1], v,
  154. n - UNCONSTRAINED_NODES, 0);
  155. if (t >= CATEGORY1_TOKEN) {
  156. const vp9_extra_bit *const b = &extra_bits[t];
  157. const unsigned char *pb = b->prob;
  158. int v = e >> 1;
  159. int n = b->len; // number of bits in v, assumed nonzero
  160. do {
  161. const int bb = (v >> --n) & 1;
  162. vpx_write(w, bb, *pb++);
  163. } while (n);
  164. }
  165. vpx_write_bit(w, e & 1);
  166. }
  167. }
  168. }
  169. *tp = (TOKENEXTRA *)(uintptr_t)p + (p->token == EOSB_TOKEN);
  170. }
  171. static void write_segment_id(vpx_writer *w, const struct segmentation *seg,
  172. int segment_id) {
  173. if (seg->enabled && seg->update_map)
  174. vp9_write_tree(w, vp9_segment_tree, seg->tree_probs, segment_id, 3, 0);
  175. }
  176. // This function encodes the reference frame
  177. static void write_ref_frames(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  178. vpx_writer *w) {
  179. const MODE_INFO *const mi = xd->mi[0];
  180. const int is_compound = has_second_ref(mi);
  181. const int segment_id = mi->segment_id;
  182. // If segment level coding of this signal is disabled...
  183. // or the segment allows multiple reference frame options
  184. if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
  185. assert(!is_compound);
  186. assert(mi->ref_frame[0] ==
  187. get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME));
  188. } else {
  189. // does the feature use compound prediction or not
  190. // (if not specified at the frame/segment level)
  191. if (cm->reference_mode == REFERENCE_MODE_SELECT) {
  192. vpx_write(w, is_compound, vp9_get_reference_mode_prob(cm, xd));
  193. } else {
  194. assert(!is_compound == (cm->reference_mode == SINGLE_REFERENCE));
  195. }
  196. if (is_compound) {
  197. vpx_write(w, mi->ref_frame[0] == GOLDEN_FRAME,
  198. vp9_get_pred_prob_comp_ref_p(cm, xd));
  199. } else {
  200. const int bit0 = mi->ref_frame[0] != LAST_FRAME;
  201. vpx_write(w, bit0, vp9_get_pred_prob_single_ref_p1(cm, xd));
  202. if (bit0) {
  203. const int bit1 = mi->ref_frame[0] != GOLDEN_FRAME;
  204. vpx_write(w, bit1, vp9_get_pred_prob_single_ref_p2(cm, xd));
  205. }
  206. }
  207. }
  208. }
  209. static void pack_inter_mode_mvs(VP9_COMP *cpi, const MODE_INFO *mi,
  210. vpx_writer *w) {
  211. VP9_COMMON *const cm = &cpi->common;
  212. const nmv_context *nmvc = &cm->fc->nmvc;
  213. const MACROBLOCK *const x = &cpi->td.mb;
  214. const MACROBLOCKD *const xd = &x->e_mbd;
  215. const struct segmentation *const seg = &cm->seg;
  216. const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
  217. const PREDICTION_MODE mode = mi->mode;
  218. const int segment_id = mi->segment_id;
  219. const BLOCK_SIZE bsize = mi->sb_type;
  220. const int allow_hp = cm->allow_high_precision_mv;
  221. const int is_inter = is_inter_block(mi);
  222. const int is_compound = has_second_ref(mi);
  223. int skip, ref;
  224. if (seg->update_map) {
  225. if (seg->temporal_update) {
  226. const int pred_flag = mi->seg_id_predicted;
  227. vpx_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
  228. vpx_write(w, pred_flag, pred_prob);
  229. if (!pred_flag) write_segment_id(w, seg, segment_id);
  230. } else {
  231. write_segment_id(w, seg, segment_id);
  232. }
  233. }
  234. skip = write_skip(cm, xd, segment_id, mi, w);
  235. if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME))
  236. vpx_write(w, is_inter, vp9_get_intra_inter_prob(cm, xd));
  237. if (bsize >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT &&
  238. !(is_inter && skip)) {
  239. write_selected_tx_size(cm, xd, w);
  240. }
  241. if (!is_inter) {
  242. if (bsize >= BLOCK_8X8) {
  243. write_intra_mode(w, mode, cm->fc->y_mode_prob[size_group_lookup[bsize]]);
  244. } else {
  245. int idx, idy;
  246. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
  247. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
  248. for (idy = 0; idy < 2; idy += num_4x4_h) {
  249. for (idx = 0; idx < 2; idx += num_4x4_w) {
  250. const PREDICTION_MODE b_mode = mi->bmi[idy * 2 + idx].as_mode;
  251. write_intra_mode(w, b_mode, cm->fc->y_mode_prob[0]);
  252. }
  253. }
  254. }
  255. write_intra_mode(w, mi->uv_mode, cm->fc->uv_mode_prob[mode]);
  256. } else {
  257. const int mode_ctx = mbmi_ext->mode_context[mi->ref_frame[0]];
  258. const vpx_prob *const inter_probs = cm->fc->inter_mode_probs[mode_ctx];
  259. write_ref_frames(cm, xd, w);
  260. // If segment skip is not enabled code the mode.
  261. if (!segfeature_active(seg, segment_id, SEG_LVL_SKIP)) {
  262. if (bsize >= BLOCK_8X8) {
  263. write_inter_mode(w, mode, inter_probs);
  264. }
  265. }
  266. if (cm->interp_filter == SWITCHABLE) {
  267. const int ctx = get_pred_context_switchable_interp(xd);
  268. vp9_write_token(w, vp9_switchable_interp_tree,
  269. cm->fc->switchable_interp_prob[ctx],
  270. &switchable_interp_encodings[mi->interp_filter]);
  271. ++cpi->interp_filter_selected[0][mi->interp_filter];
  272. } else {
  273. assert(mi->interp_filter == cm->interp_filter);
  274. }
  275. if (bsize < BLOCK_8X8) {
  276. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
  277. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
  278. int idx, idy;
  279. for (idy = 0; idy < 2; idy += num_4x4_h) {
  280. for (idx = 0; idx < 2; idx += num_4x4_w) {
  281. const int j = idy * 2 + idx;
  282. const PREDICTION_MODE b_mode = mi->bmi[j].as_mode;
  283. write_inter_mode(w, b_mode, inter_probs);
  284. if (b_mode == NEWMV) {
  285. for (ref = 0; ref < 1 + is_compound; ++ref)
  286. vp9_encode_mv(cpi, w, &mi->bmi[j].as_mv[ref].as_mv,
  287. &mbmi_ext->ref_mvs[mi->ref_frame[ref]][0].as_mv,
  288. nmvc, allow_hp);
  289. }
  290. }
  291. }
  292. } else {
  293. if (mode == NEWMV) {
  294. for (ref = 0; ref < 1 + is_compound; ++ref)
  295. vp9_encode_mv(cpi, w, &mi->mv[ref].as_mv,
  296. &mbmi_ext->ref_mvs[mi->ref_frame[ref]][0].as_mv, nmvc,
  297. allow_hp);
  298. }
  299. }
  300. }
  301. }
  302. static void write_mb_modes_kf(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  303. MODE_INFO **mi_8x8, vpx_writer *w) {
  304. const struct segmentation *const seg = &cm->seg;
  305. const MODE_INFO *const mi = mi_8x8[0];
  306. const MODE_INFO *const above_mi = xd->above_mi;
  307. const MODE_INFO *const left_mi = xd->left_mi;
  308. const BLOCK_SIZE bsize = mi->sb_type;
  309. if (seg->update_map) write_segment_id(w, seg, mi->segment_id);
  310. write_skip(cm, xd, mi->segment_id, mi, w);
  311. if (bsize >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT)
  312. write_selected_tx_size(cm, xd, w);
  313. if (bsize >= BLOCK_8X8) {
  314. write_intra_mode(w, mi->mode, get_y_mode_probs(mi, above_mi, left_mi, 0));
  315. } else {
  316. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
  317. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
  318. int idx, idy;
  319. for (idy = 0; idy < 2; idy += num_4x4_h) {
  320. for (idx = 0; idx < 2; idx += num_4x4_w) {
  321. const int block = idy * 2 + idx;
  322. write_intra_mode(w, mi->bmi[block].as_mode,
  323. get_y_mode_probs(mi, above_mi, left_mi, block));
  324. }
  325. }
  326. }
  327. write_intra_mode(w, mi->uv_mode, vp9_kf_uv_mode_prob[mi->mode]);
  328. }
  329. static void write_modes_b(VP9_COMP *cpi, const TileInfo *const tile,
  330. vpx_writer *w, TOKENEXTRA **tok,
  331. const TOKENEXTRA *const tok_end, int mi_row,
  332. int mi_col) {
  333. const VP9_COMMON *const cm = &cpi->common;
  334. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  335. MODE_INFO *m;
  336. xd->mi = cm->mi_grid_visible + (mi_row * cm->mi_stride + mi_col);
  337. m = xd->mi[0];
  338. cpi->td.mb.mbmi_ext =
  339. cpi->td.mb.mbmi_ext_base + (mi_row * cm->mi_cols + mi_col);
  340. set_mi_row_col(xd, tile, mi_row, num_8x8_blocks_high_lookup[m->sb_type],
  341. mi_col, num_8x8_blocks_wide_lookup[m->sb_type], cm->mi_rows,
  342. cm->mi_cols);
  343. if (frame_is_intra_only(cm)) {
  344. write_mb_modes_kf(cm, xd, xd->mi, w);
  345. } else {
  346. pack_inter_mode_mvs(cpi, m, w);
  347. }
  348. assert(*tok < tok_end);
  349. pack_mb_tokens(w, tok, tok_end, cm->bit_depth);
  350. }
  351. static void write_partition(const VP9_COMMON *const cm,
  352. const MACROBLOCKD *const xd, int hbs, int mi_row,
  353. int mi_col, PARTITION_TYPE p, BLOCK_SIZE bsize,
  354. vpx_writer *w) {
  355. const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
  356. const vpx_prob *const probs = xd->partition_probs[ctx];
  357. const int has_rows = (mi_row + hbs) < cm->mi_rows;
  358. const int has_cols = (mi_col + hbs) < cm->mi_cols;
  359. if (has_rows && has_cols) {
  360. vp9_write_token(w, vp9_partition_tree, probs, &partition_encodings[p]);
  361. } else if (!has_rows && has_cols) {
  362. assert(p == PARTITION_SPLIT || p == PARTITION_HORZ);
  363. vpx_write(w, p == PARTITION_SPLIT, probs[1]);
  364. } else if (has_rows && !has_cols) {
  365. assert(p == PARTITION_SPLIT || p == PARTITION_VERT);
  366. vpx_write(w, p == PARTITION_SPLIT, probs[2]);
  367. } else {
  368. assert(p == PARTITION_SPLIT);
  369. }
  370. }
  371. static void write_modes_sb(VP9_COMP *cpi, const TileInfo *const tile,
  372. vpx_writer *w, TOKENEXTRA **tok,
  373. const TOKENEXTRA *const tok_end, int mi_row,
  374. int mi_col, BLOCK_SIZE bsize) {
  375. const VP9_COMMON *const cm = &cpi->common;
  376. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  377. const int bsl = b_width_log2_lookup[bsize];
  378. const int bs = (1 << bsl) / 4;
  379. PARTITION_TYPE partition;
  380. BLOCK_SIZE subsize;
  381. const MODE_INFO *m = NULL;
  382. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  383. m = cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col];
  384. partition = partition_lookup[bsl][m->sb_type];
  385. write_partition(cm, xd, bs, mi_row, mi_col, partition, bsize, w);
  386. subsize = get_subsize(bsize, partition);
  387. if (subsize < BLOCK_8X8) {
  388. write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
  389. } else {
  390. switch (partition) {
  391. case PARTITION_NONE:
  392. write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
  393. break;
  394. case PARTITION_HORZ:
  395. write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
  396. if (mi_row + bs < cm->mi_rows)
  397. write_modes_b(cpi, tile, w, tok, tok_end, mi_row + bs, mi_col);
  398. break;
  399. case PARTITION_VERT:
  400. write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
  401. if (mi_col + bs < cm->mi_cols)
  402. write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + bs);
  403. break;
  404. case PARTITION_SPLIT:
  405. write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col, subsize);
  406. write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col + bs,
  407. subsize);
  408. write_modes_sb(cpi, tile, w, tok, tok_end, mi_row + bs, mi_col,
  409. subsize);
  410. write_modes_sb(cpi, tile, w, tok, tok_end, mi_row + bs, mi_col + bs,
  411. subsize);
  412. break;
  413. default: assert(0);
  414. }
  415. }
  416. // update partition context
  417. if (bsize >= BLOCK_8X8 &&
  418. (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
  419. update_partition_context(xd, mi_row, mi_col, subsize, bsize);
  420. }
  421. static void write_modes(VP9_COMP *cpi, const TileInfo *const tile,
  422. vpx_writer *w, TOKENEXTRA **tok,
  423. const TOKENEXTRA *const tok_end) {
  424. const VP9_COMMON *const cm = &cpi->common;
  425. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  426. int mi_row, mi_col;
  427. set_partition_probs(cm, xd);
  428. for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
  429. mi_row += MI_BLOCK_SIZE) {
  430. vp9_zero(xd->left_seg_context);
  431. for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
  432. mi_col += MI_BLOCK_SIZE)
  433. write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col, BLOCK_64X64);
  434. }
  435. }
  436. static void build_tree_distribution(VP9_COMP *cpi, TX_SIZE tx_size,
  437. vp9_coeff_stats *coef_branch_ct,
  438. vp9_coeff_probs_model *coef_probs) {
  439. vp9_coeff_count *coef_counts = cpi->td.rd_counts.coef_counts[tx_size];
  440. unsigned int(*eob_branch_ct)[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS] =
  441. cpi->common.counts.eob_branch[tx_size];
  442. int i, j, k, l, m;
  443. for (i = 0; i < PLANE_TYPES; ++i) {
  444. for (j = 0; j < REF_TYPES; ++j) {
  445. for (k = 0; k < COEF_BANDS; ++k) {
  446. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
  447. vp9_tree_probs_from_distribution(vp9_coef_tree,
  448. coef_branch_ct[i][j][k][l],
  449. coef_counts[i][j][k][l]);
  450. coef_branch_ct[i][j][k][l][0][1] =
  451. eob_branch_ct[i][j][k][l] - coef_branch_ct[i][j][k][l][0][0];
  452. for (m = 0; m < UNCONSTRAINED_NODES; ++m)
  453. coef_probs[i][j][k][l][m] =
  454. get_binary_prob(coef_branch_ct[i][j][k][l][m][0],
  455. coef_branch_ct[i][j][k][l][m][1]);
  456. }
  457. }
  458. }
  459. }
  460. }
  461. static void update_coef_probs_common(vpx_writer *const bc, VP9_COMP *cpi,
  462. TX_SIZE tx_size,
  463. vp9_coeff_stats *frame_branch_ct,
  464. vp9_coeff_probs_model *new_coef_probs) {
  465. vp9_coeff_probs_model *old_coef_probs = cpi->common.fc->coef_probs[tx_size];
  466. const vpx_prob upd = DIFF_UPDATE_PROB;
  467. const int entropy_nodes_update = UNCONSTRAINED_NODES;
  468. int i, j, k, l, t;
  469. int stepsize = cpi->sf.coeff_prob_appx_step;
  470. switch (cpi->sf.use_fast_coef_updates) {
  471. case TWO_LOOP: {
  472. /* dry run to see if there is any update at all needed */
  473. int savings = 0;
  474. int update[2] = { 0, 0 };
  475. for (i = 0; i < PLANE_TYPES; ++i) {
  476. for (j = 0; j < REF_TYPES; ++j) {
  477. for (k = 0; k < COEF_BANDS; ++k) {
  478. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
  479. for (t = 0; t < entropy_nodes_update; ++t) {
  480. vpx_prob newp = new_coef_probs[i][j][k][l][t];
  481. const vpx_prob oldp = old_coef_probs[i][j][k][l][t];
  482. int s;
  483. int u = 0;
  484. if (t == PIVOT_NODE)
  485. s = vp9_prob_diff_update_savings_search_model(
  486. frame_branch_ct[i][j][k][l][0], oldp, &newp, upd,
  487. stepsize);
  488. else
  489. s = vp9_prob_diff_update_savings_search(
  490. frame_branch_ct[i][j][k][l][t], oldp, &newp, upd);
  491. if (s > 0 && newp != oldp) u = 1;
  492. if (u)
  493. savings += s - (int)(vp9_cost_zero(upd));
  494. else
  495. savings -= (int)(vp9_cost_zero(upd));
  496. update[u]++;
  497. }
  498. }
  499. }
  500. }
  501. }
  502. // printf("Update %d %d, savings %d\n", update[0], update[1], savings);
  503. /* Is coef updated at all */
  504. if (update[1] == 0 || savings < 0) {
  505. vpx_write_bit(bc, 0);
  506. return;
  507. }
  508. vpx_write_bit(bc, 1);
  509. for (i = 0; i < PLANE_TYPES; ++i) {
  510. for (j = 0; j < REF_TYPES; ++j) {
  511. for (k = 0; k < COEF_BANDS; ++k) {
  512. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
  513. // calc probs and branch cts for this frame only
  514. for (t = 0; t < entropy_nodes_update; ++t) {
  515. vpx_prob newp = new_coef_probs[i][j][k][l][t];
  516. vpx_prob *oldp = old_coef_probs[i][j][k][l] + t;
  517. const vpx_prob upd = DIFF_UPDATE_PROB;
  518. int s;
  519. int u = 0;
  520. if (t == PIVOT_NODE)
  521. s = vp9_prob_diff_update_savings_search_model(
  522. frame_branch_ct[i][j][k][l][0], *oldp, &newp, upd,
  523. stepsize);
  524. else
  525. s = vp9_prob_diff_update_savings_search(
  526. frame_branch_ct[i][j][k][l][t], *oldp, &newp, upd);
  527. if (s > 0 && newp != *oldp) u = 1;
  528. vpx_write(bc, u, upd);
  529. if (u) {
  530. /* send/use new probability */
  531. vp9_write_prob_diff_update(bc, newp, *oldp);
  532. *oldp = newp;
  533. }
  534. }
  535. }
  536. }
  537. }
  538. }
  539. return;
  540. }
  541. case ONE_LOOP_REDUCED: {
  542. int updates = 0;
  543. int noupdates_before_first = 0;
  544. for (i = 0; i < PLANE_TYPES; ++i) {
  545. for (j = 0; j < REF_TYPES; ++j) {
  546. for (k = 0; k < COEF_BANDS; ++k) {
  547. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
  548. // calc probs and branch cts for this frame only
  549. for (t = 0; t < entropy_nodes_update; ++t) {
  550. vpx_prob newp = new_coef_probs[i][j][k][l][t];
  551. vpx_prob *oldp = old_coef_probs[i][j][k][l] + t;
  552. int s;
  553. int u = 0;
  554. if (t == PIVOT_NODE) {
  555. s = vp9_prob_diff_update_savings_search_model(
  556. frame_branch_ct[i][j][k][l][0], *oldp, &newp, upd,
  557. stepsize);
  558. } else {
  559. s = vp9_prob_diff_update_savings_search(
  560. frame_branch_ct[i][j][k][l][t], *oldp, &newp, upd);
  561. }
  562. if (s > 0 && newp != *oldp) u = 1;
  563. updates += u;
  564. if (u == 0 && updates == 0) {
  565. noupdates_before_first++;
  566. continue;
  567. }
  568. if (u == 1 && updates == 1) {
  569. int v;
  570. // first update
  571. vpx_write_bit(bc, 1);
  572. for (v = 0; v < noupdates_before_first; ++v)
  573. vpx_write(bc, 0, upd);
  574. }
  575. vpx_write(bc, u, upd);
  576. if (u) {
  577. /* send/use new probability */
  578. vp9_write_prob_diff_update(bc, newp, *oldp);
  579. *oldp = newp;
  580. }
  581. }
  582. }
  583. }
  584. }
  585. }
  586. if (updates == 0) {
  587. vpx_write_bit(bc, 0); // no updates
  588. }
  589. return;
  590. }
  591. default: assert(0);
  592. }
  593. }
  594. static void update_coef_probs(VP9_COMP *cpi, vpx_writer *w) {
  595. const TX_MODE tx_mode = cpi->common.tx_mode;
  596. const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
  597. TX_SIZE tx_size;
  598. for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size) {
  599. vp9_coeff_stats frame_branch_ct[PLANE_TYPES];
  600. vp9_coeff_probs_model frame_coef_probs[PLANE_TYPES];
  601. if (cpi->td.counts->tx.tx_totals[tx_size] <= 20 ||
  602. (tx_size >= TX_16X16 && cpi->sf.tx_size_search_method == USE_TX_8X8)) {
  603. vpx_write_bit(w, 0);
  604. } else {
  605. build_tree_distribution(cpi, tx_size, frame_branch_ct, frame_coef_probs);
  606. update_coef_probs_common(w, cpi, tx_size, frame_branch_ct,
  607. frame_coef_probs);
  608. }
  609. }
  610. }
  611. static void encode_loopfilter(struct loopfilter *lf,
  612. struct vpx_write_bit_buffer *wb) {
  613. int i;
  614. // Encode the loop filter level and type
  615. vpx_wb_write_literal(wb, lf->filter_level, 6);
  616. vpx_wb_write_literal(wb, lf->sharpness_level, 3);
  617. // Write out loop filter deltas applied at the MB level based on mode or
  618. // ref frame (if they are enabled).
  619. vpx_wb_write_bit(wb, lf->mode_ref_delta_enabled);
  620. if (lf->mode_ref_delta_enabled) {
  621. vpx_wb_write_bit(wb, lf->mode_ref_delta_update);
  622. if (lf->mode_ref_delta_update) {
  623. for (i = 0; i < MAX_REF_LF_DELTAS; i++) {
  624. const int delta = lf->ref_deltas[i];
  625. const int changed = delta != lf->last_ref_deltas[i];
  626. vpx_wb_write_bit(wb, changed);
  627. if (changed) {
  628. lf->last_ref_deltas[i] = delta;
  629. vpx_wb_write_literal(wb, abs(delta) & 0x3F, 6);
  630. vpx_wb_write_bit(wb, delta < 0);
  631. }
  632. }
  633. for (i = 0; i < MAX_MODE_LF_DELTAS; i++) {
  634. const int delta = lf->mode_deltas[i];
  635. const int changed = delta != lf->last_mode_deltas[i];
  636. vpx_wb_write_bit(wb, changed);
  637. if (changed) {
  638. lf->last_mode_deltas[i] = delta;
  639. vpx_wb_write_literal(wb, abs(delta) & 0x3F, 6);
  640. vpx_wb_write_bit(wb, delta < 0);
  641. }
  642. }
  643. }
  644. }
  645. }
  646. static void write_delta_q(struct vpx_write_bit_buffer *wb, int delta_q) {
  647. if (delta_q != 0) {
  648. vpx_wb_write_bit(wb, 1);
  649. vpx_wb_write_literal(wb, abs(delta_q), 4);
  650. vpx_wb_write_bit(wb, delta_q < 0);
  651. } else {
  652. vpx_wb_write_bit(wb, 0);
  653. }
  654. }
  655. static void encode_quantization(const VP9_COMMON *const cm,
  656. struct vpx_write_bit_buffer *wb) {
  657. vpx_wb_write_literal(wb, cm->base_qindex, QINDEX_BITS);
  658. write_delta_q(wb, cm->y_dc_delta_q);
  659. write_delta_q(wb, cm->uv_dc_delta_q);
  660. write_delta_q(wb, cm->uv_ac_delta_q);
  661. }
  662. static void encode_segmentation(VP9_COMMON *cm, MACROBLOCKD *xd,
  663. struct vpx_write_bit_buffer *wb) {
  664. int i, j;
  665. const struct segmentation *seg = &cm->seg;
  666. vpx_wb_write_bit(wb, seg->enabled);
  667. if (!seg->enabled) return;
  668. // Segmentation map
  669. vpx_wb_write_bit(wb, seg->update_map);
  670. if (seg->update_map) {
  671. // Select the coding strategy (temporal or spatial)
  672. vp9_choose_segmap_coding_method(cm, xd);
  673. // Write out probabilities used to decode unpredicted macro-block segments
  674. for (i = 0; i < SEG_TREE_PROBS; i++) {
  675. const int prob = seg->tree_probs[i];
  676. const int update = prob != MAX_PROB;
  677. vpx_wb_write_bit(wb, update);
  678. if (update) vpx_wb_write_literal(wb, prob, 8);
  679. }
  680. // Write out the chosen coding method.
  681. vpx_wb_write_bit(wb, seg->temporal_update);
  682. if (seg->temporal_update) {
  683. for (i = 0; i < PREDICTION_PROBS; i++) {
  684. const int prob = seg->pred_probs[i];
  685. const int update = prob != MAX_PROB;
  686. vpx_wb_write_bit(wb, update);
  687. if (update) vpx_wb_write_literal(wb, prob, 8);
  688. }
  689. }
  690. }
  691. // Segmentation data
  692. vpx_wb_write_bit(wb, seg->update_data);
  693. if (seg->update_data) {
  694. vpx_wb_write_bit(wb, seg->abs_delta);
  695. for (i = 0; i < MAX_SEGMENTS; i++) {
  696. for (j = 0; j < SEG_LVL_MAX; j++) {
  697. const int active = segfeature_active(seg, i, j);
  698. vpx_wb_write_bit(wb, active);
  699. if (active) {
  700. const int data = get_segdata(seg, i, j);
  701. const int data_max = vp9_seg_feature_data_max(j);
  702. if (vp9_is_segfeature_signed(j)) {
  703. encode_unsigned_max(wb, abs(data), data_max);
  704. vpx_wb_write_bit(wb, data < 0);
  705. } else {
  706. encode_unsigned_max(wb, data, data_max);
  707. }
  708. }
  709. }
  710. }
  711. }
  712. }
  713. static void encode_txfm_probs(VP9_COMMON *cm, vpx_writer *w,
  714. FRAME_COUNTS *counts) {
  715. // Mode
  716. vpx_write_literal(w, VPXMIN(cm->tx_mode, ALLOW_32X32), 2);
  717. if (cm->tx_mode >= ALLOW_32X32)
  718. vpx_write_bit(w, cm->tx_mode == TX_MODE_SELECT);
  719. // Probabilities
  720. if (cm->tx_mode == TX_MODE_SELECT) {
  721. int i, j;
  722. unsigned int ct_8x8p[TX_SIZES - 3][2];
  723. unsigned int ct_16x16p[TX_SIZES - 2][2];
  724. unsigned int ct_32x32p[TX_SIZES - 1][2];
  725. for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
  726. tx_counts_to_branch_counts_8x8(counts->tx.p8x8[i], ct_8x8p);
  727. for (j = 0; j < TX_SIZES - 3; j++)
  728. vp9_cond_prob_diff_update(w, &cm->fc->tx_probs.p8x8[i][j], ct_8x8p[j]);
  729. }
  730. for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
  731. tx_counts_to_branch_counts_16x16(counts->tx.p16x16[i], ct_16x16p);
  732. for (j = 0; j < TX_SIZES - 2; j++)
  733. vp9_cond_prob_diff_update(w, &cm->fc->tx_probs.p16x16[i][j],
  734. ct_16x16p[j]);
  735. }
  736. for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
  737. tx_counts_to_branch_counts_32x32(counts->tx.p32x32[i], ct_32x32p);
  738. for (j = 0; j < TX_SIZES - 1; j++)
  739. vp9_cond_prob_diff_update(w, &cm->fc->tx_probs.p32x32[i][j],
  740. ct_32x32p[j]);
  741. }
  742. }
  743. }
  744. static void write_interp_filter(INTERP_FILTER filter,
  745. struct vpx_write_bit_buffer *wb) {
  746. const int filter_to_literal[] = { 1, 0, 2, 3 };
  747. vpx_wb_write_bit(wb, filter == SWITCHABLE);
  748. if (filter != SWITCHABLE)
  749. vpx_wb_write_literal(wb, filter_to_literal[filter], 2);
  750. }
  751. static void fix_interp_filter(VP9_COMMON *cm, FRAME_COUNTS *counts) {
  752. if (cm->interp_filter == SWITCHABLE) {
  753. // Check to see if only one of the filters is actually used
  754. int count[SWITCHABLE_FILTERS];
  755. int i, j, c = 0;
  756. for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
  757. count[i] = 0;
  758. for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
  759. count[i] += counts->switchable_interp[j][i];
  760. c += (count[i] > 0);
  761. }
  762. if (c == 1) {
  763. // Only one filter is used. So set the filter at frame level
  764. for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
  765. if (count[i]) {
  766. cm->interp_filter = i;
  767. break;
  768. }
  769. }
  770. }
  771. }
  772. }
  773. static void write_tile_info(const VP9_COMMON *const cm,
  774. struct vpx_write_bit_buffer *wb) {
  775. int min_log2_tile_cols, max_log2_tile_cols, ones;
  776. vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
  777. // columns
  778. ones = cm->log2_tile_cols - min_log2_tile_cols;
  779. while (ones--) vpx_wb_write_bit(wb, 1);
  780. if (cm->log2_tile_cols < max_log2_tile_cols) vpx_wb_write_bit(wb, 0);
  781. // rows
  782. vpx_wb_write_bit(wb, cm->log2_tile_rows != 0);
  783. if (cm->log2_tile_rows != 0) vpx_wb_write_bit(wb, cm->log2_tile_rows != 1);
  784. }
  785. int vp9_get_refresh_mask(VP9_COMP *cpi) {
  786. if (vp9_preserve_existing_gf(cpi)) {
  787. // We have decided to preserve the previously existing golden frame as our
  788. // new ARF frame. However, in the short term we leave it in the GF slot and,
  789. // if we're updating the GF with the current decoded frame, we save it
  790. // instead to the ARF slot.
  791. // Later, in the function vp9_encoder.c:vp9_update_reference_frames() we
  792. // will swap gld_fb_idx and alt_fb_idx to achieve our objective. We do it
  793. // there so that it can be done outside of the recode loop.
  794. // Note: This is highly specific to the use of ARF as a forward reference,
  795. // and this needs to be generalized as other uses are implemented
  796. // (like RTC/temporal scalability).
  797. return (cpi->refresh_last_frame << cpi->lst_fb_idx) |
  798. (cpi->refresh_golden_frame << cpi->alt_fb_idx);
  799. } else {
  800. int arf_idx = cpi->alt_fb_idx;
  801. if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
  802. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  803. arf_idx = gf_group->arf_update_idx[gf_group->index];
  804. }
  805. return (cpi->refresh_last_frame << cpi->lst_fb_idx) |
  806. (cpi->refresh_golden_frame << cpi->gld_fb_idx) |
  807. (cpi->refresh_alt_ref_frame << arf_idx);
  808. }
  809. }
  810. static size_t encode_tiles(VP9_COMP *cpi, uint8_t *data_ptr) {
  811. VP9_COMMON *const cm = &cpi->common;
  812. vpx_writer residual_bc;
  813. int tile_row, tile_col;
  814. TOKENEXTRA *tok_end;
  815. size_t total_size = 0;
  816. const int tile_cols = 1 << cm->log2_tile_cols;
  817. const int tile_rows = 1 << cm->log2_tile_rows;
  818. memset(cm->above_seg_context, 0,
  819. sizeof(*cm->above_seg_context) * mi_cols_aligned_to_sb(cm->mi_cols));
  820. for (tile_row = 0; tile_row < tile_rows; tile_row++) {
  821. for (tile_col = 0; tile_col < tile_cols; tile_col++) {
  822. int tile_idx = tile_row * tile_cols + tile_col;
  823. TOKENEXTRA *tok = cpi->tile_tok[tile_row][tile_col];
  824. tok_end = cpi->tile_tok[tile_row][tile_col] +
  825. cpi->tok_count[tile_row][tile_col];
  826. if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1)
  827. vpx_start_encode(&residual_bc, data_ptr + total_size + 4);
  828. else
  829. vpx_start_encode(&residual_bc, data_ptr + total_size);
  830. write_modes(cpi, &cpi->tile_data[tile_idx].tile_info, &residual_bc, &tok,
  831. tok_end);
  832. assert(tok == tok_end);
  833. vpx_stop_encode(&residual_bc);
  834. if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) {
  835. // size of this tile
  836. mem_put_be32(data_ptr + total_size, residual_bc.pos);
  837. total_size += 4;
  838. }
  839. total_size += residual_bc.pos;
  840. }
  841. }
  842. return total_size;
  843. }
  844. static void write_render_size(const VP9_COMMON *cm,
  845. struct vpx_write_bit_buffer *wb) {
  846. const int scaling_active =
  847. cm->width != cm->render_width || cm->height != cm->render_height;
  848. vpx_wb_write_bit(wb, scaling_active);
  849. if (scaling_active) {
  850. vpx_wb_write_literal(wb, cm->render_width - 1, 16);
  851. vpx_wb_write_literal(wb, cm->render_height - 1, 16);
  852. }
  853. }
  854. static void write_frame_size(const VP9_COMMON *cm,
  855. struct vpx_write_bit_buffer *wb) {
  856. vpx_wb_write_literal(wb, cm->width - 1, 16);
  857. vpx_wb_write_literal(wb, cm->height - 1, 16);
  858. write_render_size(cm, wb);
  859. }
  860. static void write_frame_size_with_refs(VP9_COMP *cpi,
  861. struct vpx_write_bit_buffer *wb) {
  862. VP9_COMMON *const cm = &cpi->common;
  863. int found = 0;
  864. MV_REFERENCE_FRAME ref_frame;
  865. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  866. YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, ref_frame);
  867. // Set "found" to 0 for temporal svc and for spatial svc key frame
  868. if (cpi->use_svc &&
  869. ((cpi->svc.number_temporal_layers > 1 &&
  870. cpi->oxcf.rc_mode == VPX_CBR) ||
  871. (cpi->svc.number_spatial_layers > 1 &&
  872. cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame) ||
  873. (is_two_pass_svc(cpi) &&
  874. cpi->svc.encode_empty_frame_state == ENCODING &&
  875. cpi->svc.layer_context[0].frames_from_key_frame <
  876. cpi->svc.number_temporal_layers + 1))) {
  877. found = 0;
  878. } else if (cfg != NULL) {
  879. found =
  880. cm->width == cfg->y_crop_width && cm->height == cfg->y_crop_height;
  881. }
  882. vpx_wb_write_bit(wb, found);
  883. if (found) {
  884. break;
  885. }
  886. }
  887. if (!found) {
  888. vpx_wb_write_literal(wb, cm->width - 1, 16);
  889. vpx_wb_write_literal(wb, cm->height - 1, 16);
  890. }
  891. write_render_size(cm, wb);
  892. }
  893. static void write_sync_code(struct vpx_write_bit_buffer *wb) {
  894. vpx_wb_write_literal(wb, VP9_SYNC_CODE_0, 8);
  895. vpx_wb_write_literal(wb, VP9_SYNC_CODE_1, 8);
  896. vpx_wb_write_literal(wb, VP9_SYNC_CODE_2, 8);
  897. }
  898. static void write_profile(BITSTREAM_PROFILE profile,
  899. struct vpx_write_bit_buffer *wb) {
  900. switch (profile) {
  901. case PROFILE_0: vpx_wb_write_literal(wb, 0, 2); break;
  902. case PROFILE_1: vpx_wb_write_literal(wb, 2, 2); break;
  903. case PROFILE_2: vpx_wb_write_literal(wb, 1, 2); break;
  904. case PROFILE_3: vpx_wb_write_literal(wb, 6, 3); break;
  905. default: assert(0);
  906. }
  907. }
  908. static void write_bitdepth_colorspace_sampling(
  909. VP9_COMMON *const cm, struct vpx_write_bit_buffer *wb) {
  910. if (cm->profile >= PROFILE_2) {
  911. assert(cm->bit_depth > VPX_BITS_8);
  912. vpx_wb_write_bit(wb, cm->bit_depth == VPX_BITS_10 ? 0 : 1);
  913. }
  914. vpx_wb_write_literal(wb, cm->color_space, 3);
  915. if (cm->color_space != VPX_CS_SRGB) {
  916. // 0: [16, 235] (i.e. xvYCC), 1: [0, 255]
  917. vpx_wb_write_bit(wb, cm->color_range);
  918. if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
  919. assert(cm->subsampling_x != 1 || cm->subsampling_y != 1);
  920. vpx_wb_write_bit(wb, cm->subsampling_x);
  921. vpx_wb_write_bit(wb, cm->subsampling_y);
  922. vpx_wb_write_bit(wb, 0); // unused
  923. } else {
  924. assert(cm->subsampling_x == 1 && cm->subsampling_y == 1);
  925. }
  926. } else {
  927. assert(cm->profile == PROFILE_1 || cm->profile == PROFILE_3);
  928. vpx_wb_write_bit(wb, 0); // unused
  929. }
  930. }
  931. static void write_uncompressed_header(VP9_COMP *cpi,
  932. struct vpx_write_bit_buffer *wb) {
  933. VP9_COMMON *const cm = &cpi->common;
  934. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  935. vpx_wb_write_literal(wb, VP9_FRAME_MARKER, 2);
  936. write_profile(cm->profile, wb);
  937. vpx_wb_write_bit(wb, 0); // show_existing_frame
  938. vpx_wb_write_bit(wb, cm->frame_type);
  939. vpx_wb_write_bit(wb, cm->show_frame);
  940. vpx_wb_write_bit(wb, cm->error_resilient_mode);
  941. if (cm->frame_type == KEY_FRAME) {
  942. write_sync_code(wb);
  943. write_bitdepth_colorspace_sampling(cm, wb);
  944. write_frame_size(cm, wb);
  945. } else {
  946. // In spatial svc if it's not error_resilient_mode then we need to code all
  947. // visible frames as invisible. But we need to keep the show_frame flag so
  948. // that the publisher could know whether it is supposed to be visible.
  949. // So we will code the show_frame flag as it is. Then code the intra_only
  950. // bit here. This will make the bitstream incompatible. In the player we
  951. // will change to show_frame flag to 0, then add an one byte frame with
  952. // show_existing_frame flag which tells the decoder which frame we want to
  953. // show.
  954. if (!cm->show_frame) vpx_wb_write_bit(wb, cm->intra_only);
  955. if (!cm->error_resilient_mode)
  956. vpx_wb_write_literal(wb, cm->reset_frame_context, 2);
  957. if (cm->intra_only) {
  958. write_sync_code(wb);
  959. // Note for profile 0, 420 8bpp is assumed.
  960. if (cm->profile > PROFILE_0) {
  961. write_bitdepth_colorspace_sampling(cm, wb);
  962. }
  963. vpx_wb_write_literal(wb, vp9_get_refresh_mask(cpi), REF_FRAMES);
  964. write_frame_size(cm, wb);
  965. } else {
  966. MV_REFERENCE_FRAME ref_frame;
  967. vpx_wb_write_literal(wb, vp9_get_refresh_mask(cpi), REF_FRAMES);
  968. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  969. assert(get_ref_frame_map_idx(cpi, ref_frame) != INVALID_IDX);
  970. vpx_wb_write_literal(wb, get_ref_frame_map_idx(cpi, ref_frame),
  971. REF_FRAMES_LOG2);
  972. vpx_wb_write_bit(wb, cm->ref_frame_sign_bias[ref_frame]);
  973. }
  974. write_frame_size_with_refs(cpi, wb);
  975. vpx_wb_write_bit(wb, cm->allow_high_precision_mv);
  976. fix_interp_filter(cm, cpi->td.counts);
  977. write_interp_filter(cm->interp_filter, wb);
  978. }
  979. }
  980. if (!cm->error_resilient_mode) {
  981. vpx_wb_write_bit(wb, cm->refresh_frame_context);
  982. vpx_wb_write_bit(wb, cm->frame_parallel_decoding_mode);
  983. }
  984. vpx_wb_write_literal(wb, cm->frame_context_idx, FRAME_CONTEXTS_LOG2);
  985. encode_loopfilter(&cm->lf, wb);
  986. encode_quantization(cm, wb);
  987. encode_segmentation(cm, xd, wb);
  988. write_tile_info(cm, wb);
  989. }
  990. static size_t write_compressed_header(VP9_COMP *cpi, uint8_t *data) {
  991. VP9_COMMON *const cm = &cpi->common;
  992. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  993. FRAME_CONTEXT *const fc = cm->fc;
  994. FRAME_COUNTS *counts = cpi->td.counts;
  995. vpx_writer header_bc;
  996. vpx_start_encode(&header_bc, data);
  997. if (xd->lossless)
  998. cm->tx_mode = ONLY_4X4;
  999. else
  1000. encode_txfm_probs(cm, &header_bc, counts);
  1001. update_coef_probs(cpi, &header_bc);
  1002. update_skip_probs(cm, &header_bc, counts);
  1003. if (!frame_is_intra_only(cm)) {
  1004. int i;
  1005. for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
  1006. prob_diff_update(vp9_inter_mode_tree, cm->fc->inter_mode_probs[i],
  1007. counts->inter_mode[i], INTER_MODES, &header_bc);
  1008. if (cm->interp_filter == SWITCHABLE)
  1009. update_switchable_interp_probs(cm, &header_bc, counts);
  1010. for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
  1011. vp9_cond_prob_diff_update(&header_bc, &fc->intra_inter_prob[i],
  1012. counts->intra_inter[i]);
  1013. if (cpi->allow_comp_inter_inter) {
  1014. const int use_compound_pred = cm->reference_mode != SINGLE_REFERENCE;
  1015. const int use_hybrid_pred = cm->reference_mode == REFERENCE_MODE_SELECT;
  1016. vpx_write_bit(&header_bc, use_compound_pred);
  1017. if (use_compound_pred) {
  1018. vpx_write_bit(&header_bc, use_hybrid_pred);
  1019. if (use_hybrid_pred)
  1020. for (i = 0; i < COMP_INTER_CONTEXTS; i++)
  1021. vp9_cond_prob_diff_update(&header_bc, &fc->comp_inter_prob[i],
  1022. counts->comp_inter[i]);
  1023. }
  1024. }
  1025. if (cm->reference_mode != COMPOUND_REFERENCE) {
  1026. for (i = 0; i < REF_CONTEXTS; i++) {
  1027. vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][0],
  1028. counts->single_ref[i][0]);
  1029. vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][1],
  1030. counts->single_ref[i][1]);
  1031. }
  1032. }
  1033. if (cm->reference_mode != SINGLE_REFERENCE)
  1034. for (i = 0; i < REF_CONTEXTS; i++)
  1035. vp9_cond_prob_diff_update(&header_bc, &fc->comp_ref_prob[i],
  1036. counts->comp_ref[i]);
  1037. for (i = 0; i < BLOCK_SIZE_GROUPS; ++i)
  1038. prob_diff_update(vp9_intra_mode_tree, cm->fc->y_mode_prob[i],
  1039. counts->y_mode[i], INTRA_MODES, &header_bc);
  1040. for (i = 0; i < PARTITION_CONTEXTS; ++i)
  1041. prob_diff_update(vp9_partition_tree, fc->partition_prob[i],
  1042. counts->partition[i], PARTITION_TYPES, &header_bc);
  1043. vp9_write_nmv_probs(cm, cm->allow_high_precision_mv, &header_bc,
  1044. &counts->mv);
  1045. }
  1046. vpx_stop_encode(&header_bc);
  1047. assert(header_bc.pos <= 0xffff);
  1048. return header_bc.pos;
  1049. }
  1050. void vp9_pack_bitstream(VP9_COMP *cpi, uint8_t *dest, size_t *size) {
  1051. uint8_t *data = dest;
  1052. size_t first_part_size, uncompressed_hdr_size;
  1053. struct vpx_write_bit_buffer wb = { data, 0 };
  1054. struct vpx_write_bit_buffer saved_wb;
  1055. write_uncompressed_header(cpi, &wb);
  1056. saved_wb = wb;
  1057. vpx_wb_write_literal(&wb, 0, 16); // don't know in advance first part. size
  1058. uncompressed_hdr_size = vpx_wb_bytes_written(&wb);
  1059. data += uncompressed_hdr_size;
  1060. vpx_clear_system_state();
  1061. first_part_size = write_compressed_header(cpi, data);
  1062. data += first_part_size;
  1063. // TODO(jbb): Figure out what to do if first_part_size > 16 bits.
  1064. vpx_wb_write_literal(&saved_wb, (int)first_part_size, 16);
  1065. data += encode_tiles(cpi, data);
  1066. *size = data - dest;
  1067. }