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.

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