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.

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