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.

339 lines
9.4KB

  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. int ff_h261_get_picture_format(int width, int height)
  31. {
  32. // QCIF
  33. if (width == 176 && height == 144)
  34. return 0;
  35. // CIF
  36. else if (width == 352 && height == 288)
  37. return 1;
  38. // ERROR
  39. else
  40. return -1;
  41. }
  42. void ff_h261_encode_picture_header(MpegEncContext *s, int picture_number)
  43. {
  44. H261Context *h = (H261Context *)s;
  45. int format, temp_ref;
  46. avpriv_align_put_bits(&s->pb);
  47. /* Update the pointer to last GOB */
  48. s->ptr_lastgob = put_bits_ptr(&s->pb);
  49. put_bits(&s->pb, 20, 0x10); /* PSC */
  50. temp_ref = s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
  51. (1001 * (int64_t)s->avctx->time_base.den); // FIXME maybe this should use a timestamp
  52. put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */
  53. put_bits(&s->pb, 1, 0); /* split screen off */
  54. put_bits(&s->pb, 1, 0); /* camera off */
  55. put_bits(&s->pb, 1, 0); /* freeze picture release off */
  56. format = ff_h261_get_picture_format(s->width, s->height);
  57. put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
  58. put_bits(&s->pb, 1, 0); /* still image mode */
  59. put_bits(&s->pb, 1, 0); /* reserved */
  60. put_bits(&s->pb, 1, 0); /* no PEI */
  61. if (format == 0)
  62. h->gob_number = -1;
  63. else
  64. h->gob_number = 0;
  65. h->current_mba = 0;
  66. }
  67. /**
  68. * Encode a group of blocks header.
  69. */
  70. static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
  71. {
  72. H261Context *h = (H261Context *)s;
  73. if (ff_h261_get_picture_format(s->width, s->height) == 0) {
  74. h->gob_number += 2; // QCIF
  75. } else {
  76. h->gob_number++; // CIF
  77. }
  78. put_bits(&s->pb, 16, 1); /* GBSC */
  79. put_bits(&s->pb, 4, h->gob_number); /* GN */
  80. put_bits(&s->pb, 5, s->qscale); /* GQUANT */
  81. put_bits(&s->pb, 1, 0); /* no GEI */
  82. h->current_mba = 0;
  83. h->previous_mba = 0;
  84. h->current_mv_x = 0;
  85. h->current_mv_y = 0;
  86. }
  87. void ff_h261_reorder_mb_index(MpegEncContext *s)
  88. {
  89. int index = s->mb_x + s->mb_y * s->mb_width;
  90. if (index % 33 == 0)
  91. h261_encode_gob_header(s, 0);
  92. /* for CIF the GOB's are fragmented in the middle of a scanline
  93. * that's why we need to adjust the x and y index of the macroblocks */
  94. if (ff_h261_get_picture_format(s->width, s->height) == 1) { // CIF
  95. s->mb_x = index % 11;
  96. index /= 11;
  97. s->mb_y = index % 3;
  98. index /= 3;
  99. s->mb_x += 11 * (index % 2);
  100. index /= 2;
  101. s->mb_y += 3 * index;
  102. ff_init_block_index(s);
  103. ff_update_block_index(s);
  104. }
  105. }
  106. static void h261_encode_motion(H261Context *h, int val)
  107. {
  108. MpegEncContext *const s = &h->s;
  109. int sign, code;
  110. if (val == 0) {
  111. code = 0;
  112. put_bits(&s->pb, ff_h261_mv_tab[code][1], ff_h261_mv_tab[code][0]);
  113. } else {
  114. if (val > 15)
  115. val -= 32;
  116. if (val < -16)
  117. val += 32;
  118. sign = val < 0;
  119. code = sign ? -val : val;
  120. put_bits(&s->pb, ff_h261_mv_tab[code][1], ff_h261_mv_tab[code][0]);
  121. put_bits(&s->pb, 1, sign);
  122. }
  123. }
  124. static inline int get_cbp(MpegEncContext *s, int16_t block[6][64])
  125. {
  126. int i, cbp;
  127. cbp = 0;
  128. for (i = 0; i < 6; i++)
  129. if (s->block_last_index[i] >= 0)
  130. cbp |= 1 << (5 - i);
  131. return cbp;
  132. }
  133. /**
  134. * Encode an 8x8 block.
  135. * @param block the 8x8 block
  136. * @param n block index (0-3 are luma, 4-5 are chroma)
  137. */
  138. static void h261_encode_block(H261Context *h, int16_t *block, int n)
  139. {
  140. MpegEncContext *const s = &h->s;
  141. int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
  142. RLTable *rl;
  143. rl = &ff_h261_rl_tcoeff;
  144. if (s->mb_intra) {
  145. /* DC coef */
  146. level = block[0];
  147. /* 255 cannot be represented, so we clamp */
  148. if (level > 254) {
  149. level = 254;
  150. block[0] = 254;
  151. }
  152. /* 0 cannot be represented also */
  153. else if (level < 1) {
  154. level = 1;
  155. block[0] = 1;
  156. }
  157. if (level == 128)
  158. put_bits(&s->pb, 8, 0xff);
  159. else
  160. put_bits(&s->pb, 8, level);
  161. i = 1;
  162. } else if ((block[0] == 1 || block[0] == -1) &&
  163. (s->block_last_index[n] > -1)) {
  164. // special case
  165. put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3);
  166. i = 1;
  167. } else {
  168. i = 0;
  169. }
  170. /* AC coefs */
  171. last_index = s->block_last_index[n];
  172. last_non_zero = i - 1;
  173. for (; i <= last_index; i++) {
  174. j = s->intra_scantable.permutated[i];
  175. level = block[j];
  176. if (level) {
  177. run = i - last_non_zero - 1;
  178. sign = 0;
  179. slevel = level;
  180. if (level < 0) {
  181. sign = 1;
  182. level = -level;
  183. }
  184. code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/,
  185. run, level);
  186. if (run == 0 && level < 16)
  187. code += 1;
  188. put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
  189. if (code == rl->n) {
  190. put_bits(&s->pb, 6, run);
  191. assert(slevel != 0);
  192. assert(level <= 127);
  193. put_sbits(&s->pb, 8, slevel);
  194. } else {
  195. put_bits(&s->pb, 1, sign);
  196. }
  197. last_non_zero = i;
  198. }
  199. }
  200. if (last_index > -1)
  201. put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]); // EOB
  202. }
  203. void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
  204. int motion_x, int motion_y)
  205. {
  206. H261Context *h = (H261Context *)s;
  207. int mvd, mv_diff_x, mv_diff_y, i, cbp;
  208. cbp = 63; // avoid warning
  209. mvd = 0;
  210. h->current_mba++;
  211. h->mtype = 0;
  212. if (!s->mb_intra) {
  213. /* compute cbp */
  214. cbp = get_cbp(s, block);
  215. /* mvd indicates if this block is motion compensated */
  216. mvd = motion_x | motion_y;
  217. if ((cbp | mvd | s->dquant) == 0) {
  218. /* skip macroblock */
  219. s->skip_count++;
  220. h->current_mv_x = 0;
  221. h->current_mv_y = 0;
  222. return;
  223. }
  224. }
  225. /* MB is not skipped, encode MBA */
  226. put_bits(&s->pb,
  227. ff_h261_mba_bits[(h->current_mba - h->previous_mba) - 1],
  228. ff_h261_mba_code[(h->current_mba - h->previous_mba) - 1]);
  229. /* calculate MTYPE */
  230. if (!s->mb_intra) {
  231. h->mtype++;
  232. if (mvd || s->loop_filter)
  233. h->mtype += 3;
  234. if (s->loop_filter)
  235. h->mtype += 3;
  236. if (cbp || s->dquant)
  237. h->mtype++;
  238. assert(h->mtype > 1);
  239. }
  240. if (s->dquant)
  241. h->mtype++;
  242. put_bits(&s->pb,
  243. ff_h261_mtype_bits[h->mtype],
  244. ff_h261_mtype_code[h->mtype]);
  245. h->mtype = ff_h261_mtype_map[h->mtype];
  246. if (IS_QUANT(h->mtype)) {
  247. ff_set_qscale(s, s->qscale + s->dquant);
  248. put_bits(&s->pb, 5, s->qscale);
  249. }
  250. if (IS_16X16(h->mtype)) {
  251. mv_diff_x = (motion_x >> 1) - h->current_mv_x;
  252. mv_diff_y = (motion_y >> 1) - h->current_mv_y;
  253. h->current_mv_x = (motion_x >> 1);
  254. h->current_mv_y = (motion_y >> 1);
  255. h261_encode_motion(h, mv_diff_x);
  256. h261_encode_motion(h, mv_diff_y);
  257. }
  258. h->previous_mba = h->current_mba;
  259. if (HAS_CBP(h->mtype)) {
  260. assert(cbp > 0);
  261. put_bits(&s->pb,
  262. ff_h261_cbp_tab[cbp - 1][1],
  263. ff_h261_cbp_tab[cbp - 1][0]);
  264. }
  265. for (i = 0; i < 6; i++)
  266. /* encode each block */
  267. h261_encode_block(h, block[i], i);
  268. if ((h->current_mba == 11) || (h->current_mba == 22) ||
  269. (h->current_mba == 33) || (!IS_16X16(h->mtype))) {
  270. h->current_mv_x = 0;
  271. h->current_mv_y = 0;
  272. }
  273. }
  274. void ff_h261_encode_init(MpegEncContext *s)
  275. {
  276. ff_h261_common_init();
  277. s->min_qcoeff = -127;
  278. s->max_qcoeff = 127;
  279. s->y_dc_scale_table =
  280. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  281. }
  282. FF_MPV_GENERIC_CLASS(h261)
  283. AVCodec ff_h261_encoder = {
  284. .name = "h261",
  285. .type = AVMEDIA_TYPE_VIDEO,
  286. .id = AV_CODEC_ID_H261,
  287. .priv_data_size = sizeof(H261Context),
  288. .init = ff_MPV_encode_init,
  289. .encode2 = ff_MPV_encode_picture,
  290. .close = ff_MPV_encode_end,
  291. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  292. AV_PIX_FMT_NONE },
  293. .long_name = NULL_IF_CONFIG_SMALL("H.261"),
  294. .priv_class = &h261_class,
  295. };