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

343 lines
9.5KB

  1. /*
  2. * H261 encoder
  3. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  4. * Copyright (c) 2004 Maarten Daniels
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * H.261 encoder.
  25. */
  26. #include "avcodec.h"
  27. #include "mpegvideo.h"
  28. #include "h263.h"
  29. #include "h261.h"
  30. #include "h261data.h"
  31. static void h261_encode_block(H261Context *h, int16_t *block,
  32. int n);
  33. int ff_h261_get_picture_format(int width, int height)
  34. {
  35. // QCIF
  36. if (width == 176 && height == 144)
  37. return 0;
  38. // CIF
  39. else if (width == 352 && height == 288)
  40. return 1;
  41. // ERROR
  42. else
  43. return -1;
  44. }
  45. void ff_h261_encode_picture_header(MpegEncContext *s, int picture_number)
  46. {
  47. H261Context *h = (H261Context *)s;
  48. int format, temp_ref;
  49. avpriv_align_put_bits(&s->pb);
  50. /* Update the pointer to last GOB */
  51. s->ptr_lastgob = put_bits_ptr(&s->pb);
  52. put_bits(&s->pb, 20, 0x10); /* PSC */
  53. temp_ref = s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
  54. (1001 * (int64_t)s->avctx->time_base.den); // FIXME maybe this should use a timestamp
  55. put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */
  56. put_bits(&s->pb, 1, 0); /* split screen off */
  57. put_bits(&s->pb, 1, 0); /* camera off */
  58. put_bits(&s->pb, 1, 0); /* freeze picture release off */
  59. format = ff_h261_get_picture_format(s->width, s->height);
  60. put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
  61. put_bits(&s->pb, 1, 0); /* still image mode */
  62. put_bits(&s->pb, 1, 0); /* reserved */
  63. put_bits(&s->pb, 1, 0); /* no PEI */
  64. if (format == 0)
  65. h->gob_number = -1;
  66. else
  67. h->gob_number = 0;
  68. h->current_mba = 0;
  69. }
  70. /**
  71. * Encode a group of blocks header.
  72. */
  73. static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
  74. {
  75. H261Context *h = (H261Context *)s;
  76. if (ff_h261_get_picture_format(s->width, s->height) == 0) {
  77. h->gob_number += 2; // QCIF
  78. } else {
  79. h->gob_number++; // CIF
  80. }
  81. put_bits(&s->pb, 16, 1); /* GBSC */
  82. put_bits(&s->pb, 4, h->gob_number); /* GN */
  83. put_bits(&s->pb, 5, s->qscale); /* GQUANT */
  84. put_bits(&s->pb, 1, 0); /* no GEI */
  85. h->current_mba = 0;
  86. h->previous_mba = 0;
  87. h->current_mv_x = 0;
  88. h->current_mv_y = 0;
  89. }
  90. void ff_h261_reorder_mb_index(MpegEncContext *s)
  91. {
  92. int index = s->mb_x + s->mb_y * s->mb_width;
  93. if (index % 33 == 0)
  94. h261_encode_gob_header(s, 0);
  95. /* for CIF the GOB's are fragmented in the middle of a scanline
  96. * that's why we need to adjust the x and y index of the macroblocks */
  97. if (ff_h261_get_picture_format(s->width, s->height) == 1) { // CIF
  98. s->mb_x = index % 11;
  99. index /= 11;
  100. s->mb_y = index % 3;
  101. index /= 3;
  102. s->mb_x += 11 * (index % 2);
  103. index /= 2;
  104. s->mb_y += 3 * index;
  105. ff_init_block_index(s);
  106. ff_update_block_index(s);
  107. }
  108. }
  109. static void h261_encode_motion(H261Context *h, int val)
  110. {
  111. MpegEncContext *const s = &h->s;
  112. int sign, code;
  113. if (val == 0) {
  114. code = 0;
  115. put_bits(&s->pb, h261_mv_tab[code][1], h261_mv_tab[code][0]);
  116. } else {
  117. if (val > 15)
  118. val -= 32;
  119. if (val < -16)
  120. val += 32;
  121. sign = val < 0;
  122. code = sign ? -val : val;
  123. put_bits(&s->pb, h261_mv_tab[code][1], h261_mv_tab[code][0]);
  124. put_bits(&s->pb, 1, sign);
  125. }
  126. }
  127. static inline int get_cbp(MpegEncContext *s, int16_t block[6][64])
  128. {
  129. int i, cbp;
  130. cbp = 0;
  131. for (i = 0; i < 6; i++)
  132. if (s->block_last_index[i] >= 0)
  133. cbp |= 1 << (5 - i);
  134. return cbp;
  135. }
  136. void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
  137. int motion_x, int motion_y)
  138. {
  139. H261Context *h = (H261Context *)s;
  140. int mvd, mv_diff_x, mv_diff_y, i, cbp;
  141. cbp = 63; // avoid warning
  142. mvd = 0;
  143. h->current_mba++;
  144. h->mtype = 0;
  145. if (!s->mb_intra) {
  146. /* compute cbp */
  147. cbp = get_cbp(s, block);
  148. /* mvd indicates if this block is motion compensated */
  149. mvd = motion_x | motion_y;
  150. if ((cbp | mvd | s->dquant) == 0) {
  151. /* skip macroblock */
  152. s->skip_count++;
  153. h->current_mv_x = 0;
  154. h->current_mv_y = 0;
  155. return;
  156. }
  157. }
  158. /* MB is not skipped, encode MBA */
  159. put_bits(&s->pb, h261_mba_bits[(h->current_mba - h->previous_mba) - 1],
  160. h261_mba_code[(h->current_mba - h->previous_mba) - 1]);
  161. /* calculate MTYPE */
  162. if (!s->mb_intra) {
  163. h->mtype++;
  164. if (mvd || s->loop_filter)
  165. h->mtype += 3;
  166. if (s->loop_filter)
  167. h->mtype += 3;
  168. if (cbp || s->dquant)
  169. h->mtype++;
  170. assert(h->mtype > 1);
  171. }
  172. if (s->dquant)
  173. h->mtype++;
  174. put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]);
  175. h->mtype = h261_mtype_map[h->mtype];
  176. if (IS_QUANT(h->mtype)) {
  177. ff_set_qscale(s, s->qscale + s->dquant);
  178. put_bits(&s->pb, 5, s->qscale);
  179. }
  180. if (IS_16X16(h->mtype)) {
  181. mv_diff_x = (motion_x >> 1) - h->current_mv_x;
  182. mv_diff_y = (motion_y >> 1) - h->current_mv_y;
  183. h->current_mv_x = (motion_x >> 1);
  184. h->current_mv_y = (motion_y >> 1);
  185. h261_encode_motion(h, mv_diff_x);
  186. h261_encode_motion(h, mv_diff_y);
  187. }
  188. h->previous_mba = h->current_mba;
  189. if (HAS_CBP(h->mtype)) {
  190. assert(cbp > 0);
  191. put_bits(&s->pb, h261_cbp_tab[cbp - 1][1], h261_cbp_tab[cbp - 1][0]);
  192. }
  193. for (i = 0; i < 6; i++)
  194. /* encode each block */
  195. h261_encode_block(h, block[i], i);
  196. if ((h->current_mba == 11) || (h->current_mba == 22) ||
  197. (h->current_mba == 33) || (!IS_16X16(h->mtype))) {
  198. h->current_mv_x = 0;
  199. h->current_mv_y = 0;
  200. }
  201. }
  202. void ff_h261_encode_init(MpegEncContext *s)
  203. {
  204. static int done = 0;
  205. if (!done) {
  206. done = 1;
  207. ff_init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
  208. }
  209. s->min_qcoeff = -127;
  210. s->max_qcoeff = 127;
  211. s->y_dc_scale_table =
  212. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  213. }
  214. /**
  215. * Encode an 8x8 block.
  216. * @param block the 8x8 block
  217. * @param n block index (0-3 are luma, 4-5 are chroma)
  218. */
  219. static void h261_encode_block(H261Context *h, int16_t *block, int n)
  220. {
  221. MpegEncContext *const s = &h->s;
  222. int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
  223. RLTable *rl;
  224. rl = &h261_rl_tcoeff;
  225. if (s->mb_intra) {
  226. /* DC coef */
  227. level = block[0];
  228. /* 255 cannot be represented, so we clamp */
  229. if (level > 254) {
  230. level = 254;
  231. block[0] = 254;
  232. }
  233. /* 0 cannot be represented also */
  234. else if (level < 1) {
  235. level = 1;
  236. block[0] = 1;
  237. }
  238. if (level == 128)
  239. put_bits(&s->pb, 8, 0xff);
  240. else
  241. put_bits(&s->pb, 8, level);
  242. i = 1;
  243. } else if ((block[0] == 1 || block[0] == -1) &&
  244. (s->block_last_index[n] > -1)) {
  245. // special case
  246. put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3);
  247. i = 1;
  248. } else {
  249. i = 0;
  250. }
  251. /* AC coefs */
  252. last_index = s->block_last_index[n];
  253. last_non_zero = i - 1;
  254. for (; i <= last_index; i++) {
  255. j = s->intra_scantable.permutated[i];
  256. level = block[j];
  257. if (level) {
  258. run = i - last_non_zero - 1;
  259. sign = 0;
  260. slevel = level;
  261. if (level < 0) {
  262. sign = 1;
  263. level = -level;
  264. }
  265. code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/,
  266. run, level);
  267. if (run == 0 && level < 16)
  268. code += 1;
  269. put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
  270. if (code == rl->n) {
  271. put_bits(&s->pb, 6, run);
  272. assert(slevel != 0);
  273. assert(level <= 127);
  274. put_sbits(&s->pb, 8, slevel);
  275. } else {
  276. put_bits(&s->pb, 1, sign);
  277. }
  278. last_non_zero = i;
  279. }
  280. }
  281. if (last_index > -1)
  282. put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]); // EOB
  283. }
  284. FF_MPV_GENERIC_CLASS(h261)
  285. AVCodec ff_h261_encoder = {
  286. .name = "h261",
  287. .type = AVMEDIA_TYPE_VIDEO,
  288. .id = AV_CODEC_ID_H261,
  289. .priv_data_size = sizeof(H261Context),
  290. .init = ff_MPV_encode_init,
  291. .encode2 = ff_MPV_encode_picture,
  292. .close = ff_MPV_encode_end,
  293. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  294. AV_PIX_FMT_NONE },
  295. .long_name = NULL_IF_CONFIG_SMALL("H.261"),
  296. .priv_class = &h261_class,
  297. };