iterator_enc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // VP8Iterator: block iterator
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <string.h>
  14. #include "./vp8i_enc.h"
  15. //------------------------------------------------------------------------------
  16. // VP8Iterator
  17. //------------------------------------------------------------------------------
  18. static void InitLeft(VP8EncIterator* const it) {
  19. it->y_left_[-1] = it->u_left_[-1] = it->v_left_[-1] =
  20. (it->y_ > 0) ? 129 : 127;
  21. memset(it->y_left_, 129, 16);
  22. memset(it->u_left_, 129, 8);
  23. memset(it->v_left_, 129, 8);
  24. it->left_nz_[8] = 0;
  25. if (it->top_derr_ != NULL) {
  26. memset(&it->left_derr_, 0, sizeof(it->left_derr_));
  27. }
  28. }
  29. static void InitTop(VP8EncIterator* const it) {
  30. const VP8Encoder* const enc = it->enc_;
  31. const size_t top_size = enc->mb_w_ * 16;
  32. memset(enc->y_top_, 127, 2 * top_size);
  33. memset(enc->nz_, 0, enc->mb_w_ * sizeof(*enc->nz_));
  34. if (enc->top_derr_ != NULL) {
  35. memset(enc->top_derr_, 0, enc->mb_w_ * sizeof(*enc->top_derr_));
  36. }
  37. }
  38. void VP8IteratorSetRow(VP8EncIterator* const it, int y) {
  39. VP8Encoder* const enc = it->enc_;
  40. it->x_ = 0;
  41. it->y_ = y;
  42. it->bw_ = &enc->parts_[y & (enc->num_parts_ - 1)];
  43. it->preds_ = enc->preds_ + y * 4 * enc->preds_w_;
  44. it->nz_ = enc->nz_;
  45. it->mb_ = enc->mb_info_ + y * enc->mb_w_;
  46. it->y_top_ = enc->y_top_;
  47. it->uv_top_ = enc->uv_top_;
  48. InitLeft(it);
  49. }
  50. void VP8IteratorReset(VP8EncIterator* const it) {
  51. VP8Encoder* const enc = it->enc_;
  52. VP8IteratorSetRow(it, 0);
  53. VP8IteratorSetCountDown(it, enc->mb_w_ * enc->mb_h_); // default
  54. InitTop(it);
  55. memset(it->bit_count_, 0, sizeof(it->bit_count_));
  56. it->do_trellis_ = 0;
  57. }
  58. void VP8IteratorSetCountDown(VP8EncIterator* const it, int count_down) {
  59. it->count_down_ = it->count_down0_ = count_down;
  60. }
  61. int VP8IteratorIsDone(const VP8EncIterator* const it) {
  62. return (it->count_down_ <= 0);
  63. }
  64. void VP8IteratorInit(VP8Encoder* const enc, VP8EncIterator* const it) {
  65. it->enc_ = enc;
  66. it->yuv_in_ = (uint8_t*)WEBP_ALIGN(it->yuv_mem_);
  67. it->yuv_out_ = it->yuv_in_ + YUV_SIZE_ENC;
  68. it->yuv_out2_ = it->yuv_out_ + YUV_SIZE_ENC;
  69. it->yuv_p_ = it->yuv_out2_ + YUV_SIZE_ENC;
  70. it->lf_stats_ = enc->lf_stats_;
  71. it->percent0_ = enc->percent_;
  72. it->y_left_ = (uint8_t*)WEBP_ALIGN(it->yuv_left_mem_ + 1);
  73. it->u_left_ = it->y_left_ + 16 + 16;
  74. it->v_left_ = it->u_left_ + 16;
  75. it->top_derr_ = enc->top_derr_;
  76. VP8IteratorReset(it);
  77. }
  78. int VP8IteratorProgress(const VP8EncIterator* const it, int delta) {
  79. VP8Encoder* const enc = it->enc_;
  80. if (delta && enc->pic_->progress_hook != NULL) {
  81. const int done = it->count_down0_ - it->count_down_;
  82. const int percent = (it->count_down0_ <= 0)
  83. ? it->percent0_
  84. : it->percent0_ + delta * done / it->count_down0_;
  85. return WebPReportProgress(enc->pic_, percent, &enc->percent_);
  86. }
  87. return 1;
  88. }
  89. //------------------------------------------------------------------------------
  90. // Import the source samples into the cache. Takes care of replicating
  91. // boundary pixels if necessary.
  92. static WEBP_INLINE int MinSize(int a, int b) { return (a < b) ? a : b; }
  93. static void ImportBlock(const uint8_t* src, int src_stride,
  94. uint8_t* dst, int w, int h, int size) {
  95. int i;
  96. for (i = 0; i < h; ++i) {
  97. memcpy(dst, src, w);
  98. if (w < size) {
  99. memset(dst + w, dst[w - 1], size - w);
  100. }
  101. dst += BPS;
  102. src += src_stride;
  103. }
  104. for (i = h; i < size; ++i) {
  105. memcpy(dst, dst - BPS, size);
  106. dst += BPS;
  107. }
  108. }
  109. static void ImportLine(const uint8_t* src, int src_stride,
  110. uint8_t* dst, int len, int total_len) {
  111. int i;
  112. for (i = 0; i < len; ++i, src += src_stride) dst[i] = *src;
  113. for (; i < total_len; ++i) dst[i] = dst[len - 1];
  114. }
  115. void VP8IteratorImport(VP8EncIterator* const it, uint8_t* const tmp_32) {
  116. const VP8Encoder* const enc = it->enc_;
  117. const int x = it->x_, y = it->y_;
  118. const WebPPicture* const pic = enc->pic_;
  119. const uint8_t* const ysrc = pic->y + (y * pic->y_stride + x) * 16;
  120. const uint8_t* const usrc = pic->u + (y * pic->uv_stride + x) * 8;
  121. const uint8_t* const vsrc = pic->v + (y * pic->uv_stride + x) * 8;
  122. const int w = MinSize(pic->width - x * 16, 16);
  123. const int h = MinSize(pic->height - y * 16, 16);
  124. const int uv_w = (w + 1) >> 1;
  125. const int uv_h = (h + 1) >> 1;
  126. ImportBlock(ysrc, pic->y_stride, it->yuv_in_ + Y_OFF_ENC, w, h, 16);
  127. ImportBlock(usrc, pic->uv_stride, it->yuv_in_ + U_OFF_ENC, uv_w, uv_h, 8);
  128. ImportBlock(vsrc, pic->uv_stride, it->yuv_in_ + V_OFF_ENC, uv_w, uv_h, 8);
  129. if (tmp_32 == NULL) return;
  130. // Import source (uncompressed) samples into boundary.
  131. if (x == 0) {
  132. InitLeft(it);
  133. } else {
  134. if (y == 0) {
  135. it->y_left_[-1] = it->u_left_[-1] = it->v_left_[-1] = 127;
  136. } else {
  137. it->y_left_[-1] = ysrc[- 1 - pic->y_stride];
  138. it->u_left_[-1] = usrc[- 1 - pic->uv_stride];
  139. it->v_left_[-1] = vsrc[- 1 - pic->uv_stride];
  140. }
  141. ImportLine(ysrc - 1, pic->y_stride, it->y_left_, h, 16);
  142. ImportLine(usrc - 1, pic->uv_stride, it->u_left_, uv_h, 8);
  143. ImportLine(vsrc - 1, pic->uv_stride, it->v_left_, uv_h, 8);
  144. }
  145. it->y_top_ = tmp_32 + 0;
  146. it->uv_top_ = tmp_32 + 16;
  147. if (y == 0) {
  148. memset(tmp_32, 127, 32 * sizeof(*tmp_32));
  149. } else {
  150. ImportLine(ysrc - pic->y_stride, 1, tmp_32, w, 16);
  151. ImportLine(usrc - pic->uv_stride, 1, tmp_32 + 16, uv_w, 8);
  152. ImportLine(vsrc - pic->uv_stride, 1, tmp_32 + 16 + 8, uv_w, 8);
  153. }
  154. }
  155. //------------------------------------------------------------------------------
  156. // Copy back the compressed samples into user space if requested.
  157. static void ExportBlock(const uint8_t* src, uint8_t* dst, int dst_stride,
  158. int w, int h) {
  159. while (h-- > 0) {
  160. memcpy(dst, src, w);
  161. dst += dst_stride;
  162. src += BPS;
  163. }
  164. }
  165. void VP8IteratorExport(const VP8EncIterator* const it) {
  166. const VP8Encoder* const enc = it->enc_;
  167. if (enc->config_->show_compressed) {
  168. const int x = it->x_, y = it->y_;
  169. const uint8_t* const ysrc = it->yuv_out_ + Y_OFF_ENC;
  170. const uint8_t* const usrc = it->yuv_out_ + U_OFF_ENC;
  171. const uint8_t* const vsrc = it->yuv_out_ + V_OFF_ENC;
  172. const WebPPicture* const pic = enc->pic_;
  173. uint8_t* const ydst = pic->y + (y * pic->y_stride + x) * 16;
  174. uint8_t* const udst = pic->u + (y * pic->uv_stride + x) * 8;
  175. uint8_t* const vdst = pic->v + (y * pic->uv_stride + x) * 8;
  176. int w = (pic->width - x * 16);
  177. int h = (pic->height - y * 16);
  178. if (w > 16) w = 16;
  179. if (h > 16) h = 16;
  180. // Luma plane
  181. ExportBlock(ysrc, ydst, pic->y_stride, w, h);
  182. { // U/V planes
  183. const int uv_w = (w + 1) >> 1;
  184. const int uv_h = (h + 1) >> 1;
  185. ExportBlock(usrc, udst, pic->uv_stride, uv_w, uv_h);
  186. ExportBlock(vsrc, vdst, pic->uv_stride, uv_w, uv_h);
  187. }
  188. }
  189. }
  190. //------------------------------------------------------------------------------
  191. // Non-zero contexts setup/teardown
  192. // Nz bits:
  193. // 0 1 2 3 Y
  194. // 4 5 6 7
  195. // 8 9 10 11
  196. // 12 13 14 15
  197. // 16 17 U
  198. // 18 19
  199. // 20 21 V
  200. // 22 23
  201. // 24 DC-intra16
  202. // Convert packed context to byte array
  203. #define BIT(nz, n) (!!((nz) & (1 << (n))))
  204. void VP8IteratorNzToBytes(VP8EncIterator* const it) {
  205. const int tnz = it->nz_[0], lnz = it->nz_[-1];
  206. int* const top_nz = it->top_nz_;
  207. int* const left_nz = it->left_nz_;
  208. // Top-Y
  209. top_nz[0] = BIT(tnz, 12);
  210. top_nz[1] = BIT(tnz, 13);
  211. top_nz[2] = BIT(tnz, 14);
  212. top_nz[3] = BIT(tnz, 15);
  213. // Top-U
  214. top_nz[4] = BIT(tnz, 18);
  215. top_nz[5] = BIT(tnz, 19);
  216. // Top-V
  217. top_nz[6] = BIT(tnz, 22);
  218. top_nz[7] = BIT(tnz, 23);
  219. // DC
  220. top_nz[8] = BIT(tnz, 24);
  221. // left-Y
  222. left_nz[0] = BIT(lnz, 3);
  223. left_nz[1] = BIT(lnz, 7);
  224. left_nz[2] = BIT(lnz, 11);
  225. left_nz[3] = BIT(lnz, 15);
  226. // left-U
  227. left_nz[4] = BIT(lnz, 17);
  228. left_nz[5] = BIT(lnz, 19);
  229. // left-V
  230. left_nz[6] = BIT(lnz, 21);
  231. left_nz[7] = BIT(lnz, 23);
  232. // left-DC is special, iterated separately
  233. }
  234. void VP8IteratorBytesToNz(VP8EncIterator* const it) {
  235. uint32_t nz = 0;
  236. const int* const top_nz = it->top_nz_;
  237. const int* const left_nz = it->left_nz_;
  238. // top
  239. nz |= (top_nz[0] << 12) | (top_nz[1] << 13);
  240. nz |= (top_nz[2] << 14) | (top_nz[3] << 15);
  241. nz |= (top_nz[4] << 18) | (top_nz[5] << 19);
  242. nz |= (top_nz[6] << 22) | (top_nz[7] << 23);
  243. nz |= (top_nz[8] << 24); // we propagate the _top_ bit, esp. for intra4
  244. // left
  245. nz |= (left_nz[0] << 3) | (left_nz[1] << 7);
  246. nz |= (left_nz[2] << 11);
  247. nz |= (left_nz[4] << 17) | (left_nz[6] << 21);
  248. *it->nz_ = nz;
  249. }
  250. #undef BIT
  251. //------------------------------------------------------------------------------
  252. // Advance to the next position, doing the bookkeeping.
  253. void VP8IteratorSaveBoundary(VP8EncIterator* const it) {
  254. VP8Encoder* const enc = it->enc_;
  255. const int x = it->x_, y = it->y_;
  256. const uint8_t* const ysrc = it->yuv_out_ + Y_OFF_ENC;
  257. const uint8_t* const uvsrc = it->yuv_out_ + U_OFF_ENC;
  258. if (x < enc->mb_w_ - 1) { // left
  259. int i;
  260. for (i = 0; i < 16; ++i) {
  261. it->y_left_[i] = ysrc[15 + i * BPS];
  262. }
  263. for (i = 0; i < 8; ++i) {
  264. it->u_left_[i] = uvsrc[7 + i * BPS];
  265. it->v_left_[i] = uvsrc[15 + i * BPS];
  266. }
  267. // top-left (before 'top'!)
  268. it->y_left_[-1] = it->y_top_[15];
  269. it->u_left_[-1] = it->uv_top_[0 + 7];
  270. it->v_left_[-1] = it->uv_top_[8 + 7];
  271. }
  272. if (y < enc->mb_h_ - 1) { // top
  273. memcpy(it->y_top_, ysrc + 15 * BPS, 16);
  274. memcpy(it->uv_top_, uvsrc + 7 * BPS, 8 + 8);
  275. }
  276. }
  277. int VP8IteratorNext(VP8EncIterator* const it) {
  278. if (++it->x_ == it->enc_->mb_w_) {
  279. VP8IteratorSetRow(it, ++it->y_);
  280. } else {
  281. it->preds_ += 4;
  282. it->mb_ += 1;
  283. it->nz_ += 1;
  284. it->y_top_ += 16;
  285. it->uv_top_ += 16;
  286. }
  287. return (0 < --it->count_down_);
  288. }
  289. //------------------------------------------------------------------------------
  290. // Helper function to set mode properties
  291. void VP8SetIntra16Mode(const VP8EncIterator* const it, int mode) {
  292. uint8_t* preds = it->preds_;
  293. int y;
  294. for (y = 0; y < 4; ++y) {
  295. memset(preds, mode, 4);
  296. preds += it->enc_->preds_w_;
  297. }
  298. it->mb_->type_ = 1;
  299. }
  300. void VP8SetIntra4Mode(const VP8EncIterator* const it, const uint8_t* modes) {
  301. uint8_t* preds = it->preds_;
  302. int y;
  303. for (y = 4; y > 0; --y) {
  304. memcpy(preds, modes, 4 * sizeof(*modes));
  305. preds += it->enc_->preds_w_;
  306. modes += 4;
  307. }
  308. it->mb_->type_ = 0;
  309. }
  310. void VP8SetIntraUVMode(const VP8EncIterator* const it, int mode) {
  311. it->mb_->uv_mode_ = mode;
  312. }
  313. void VP8SetSkip(const VP8EncIterator* const it, int skip) {
  314. it->mb_->skip_ = skip;
  315. }
  316. void VP8SetSegment(const VP8EncIterator* const it, int segment) {
  317. it->mb_->segment_ = segment;
  318. }
  319. //------------------------------------------------------------------------------
  320. // Intra4x4 sub-blocks iteration
  321. //
  322. // We store and update the boundary samples into an array of 37 pixels. They
  323. // are updated as we iterate and reconstructs each intra4x4 blocks in turn.
  324. // The position of the samples has the following snake pattern:
  325. //
  326. // 16|17 18 19 20|21 22 23 24|25 26 27 28|29 30 31 32|33 34 35 36 <- Top-right
  327. // --+-----------+-----------+-----------+-----------+
  328. // 15| 19| 23| 27| 31|
  329. // 14| 18| 22| 26| 30|
  330. // 13| 17| 21| 25| 29|
  331. // 12|13 14 15 16|17 18 19 20|21 22 23 24|25 26 27 28|
  332. // --+-----------+-----------+-----------+-----------+
  333. // 11| 15| 19| 23| 27|
  334. // 10| 14| 18| 22| 26|
  335. // 9| 13| 17| 21| 25|
  336. // 8| 9 10 11 12|13 14 15 16|17 18 19 20|21 22 23 24|
  337. // --+-----------+-----------+-----------+-----------+
  338. // 7| 11| 15| 19| 23|
  339. // 6| 10| 14| 18| 22|
  340. // 5| 9| 13| 17| 21|
  341. // 4| 5 6 7 8| 9 10 11 12|13 14 15 16|17 18 19 20|
  342. // --+-----------+-----------+-----------+-----------+
  343. // 3| 7| 11| 15| 19|
  344. // 2| 6| 10| 14| 18|
  345. // 1| 5| 9| 13| 17|
  346. // 0| 1 2 3 4| 5 6 7 8| 9 10 11 12|13 14 15 16|
  347. // --+-----------+-----------+-----------+-----------+
  348. // Array to record the position of the top sample to pass to the prediction
  349. // functions in dsp.c.
  350. static const uint8_t VP8TopLeftI4[16] = {
  351. 17, 21, 25, 29,
  352. 13, 17, 21, 25,
  353. 9, 13, 17, 21,
  354. 5, 9, 13, 17
  355. };
  356. void VP8IteratorStartI4(VP8EncIterator* const it) {
  357. const VP8Encoder* const enc = it->enc_;
  358. int i;
  359. it->i4_ = 0; // first 4x4 sub-block
  360. it->i4_top_ = it->i4_boundary_ + VP8TopLeftI4[0];
  361. // Import the boundary samples
  362. for (i = 0; i < 17; ++i) { // left
  363. it->i4_boundary_[i] = it->y_left_[15 - i];
  364. }
  365. for (i = 0; i < 16; ++i) { // top
  366. it->i4_boundary_[17 + i] = it->y_top_[i];
  367. }
  368. // top-right samples have a special case on the far right of the picture
  369. if (it->x_ < enc->mb_w_ - 1) {
  370. for (i = 16; i < 16 + 4; ++i) {
  371. it->i4_boundary_[17 + i] = it->y_top_[i];
  372. }
  373. } else { // else, replicate the last valid pixel four times
  374. for (i = 16; i < 16 + 4; ++i) {
  375. it->i4_boundary_[17 + i] = it->i4_boundary_[17 + 15];
  376. }
  377. }
  378. VP8IteratorNzToBytes(it); // import the non-zero context
  379. }
  380. int VP8IteratorRotateI4(VP8EncIterator* const it,
  381. const uint8_t* const yuv_out) {
  382. const uint8_t* const blk = yuv_out + VP8Scan[it->i4_];
  383. uint8_t* const top = it->i4_top_;
  384. int i;
  385. // Update the cache with 7 fresh samples
  386. for (i = 0; i <= 3; ++i) {
  387. top[-4 + i] = blk[i + 3 * BPS]; // store future top samples
  388. }
  389. if ((it->i4_ & 3) != 3) { // if not on the right sub-blocks #3, #7, #11, #15
  390. for (i = 0; i <= 2; ++i) { // store future left samples
  391. top[i] = blk[3 + (2 - i) * BPS];
  392. }
  393. } else { // else replicate top-right samples, as says the specs.
  394. for (i = 0; i <= 3; ++i) {
  395. top[i] = top[i + 4];
  396. }
  397. }
  398. // move pointers to next sub-block
  399. ++it->i4_;
  400. if (it->i4_ == 16) { // we're done
  401. return 0;
  402. }
  403. it->i4_top_ = it->i4_boundary_ + VP8TopLeftI4[it->i4_];
  404. return 1;
  405. }
  406. //------------------------------------------------------------------------------