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.

347 lines
9.7KB

  1. /*
  2. * H.261 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. #include "mpegvideodata.h"
  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, s->pict_type == AV_PICTURE_TYPE_I); /* freeze picture release on/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, 1); /* still image mode */
  62. put_bits(&s->pb, 1, 1); /* 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, ff_h261_mv_tab[code][1], ff_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, ff_h261_mv_tab[code][1], ff_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. /**
  137. * Encode an 8x8 block.
  138. * @param block the 8x8 block
  139. * @param n block index (0-3 are luma, 4-5 are chroma)
  140. */
  141. static void h261_encode_block(H261Context *h, int16_t *block, int n)
  142. {
  143. MpegEncContext *const s = &h->s;
  144. int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
  145. RLTable *rl;
  146. rl = &ff_h261_rl_tcoeff;
  147. if (s->mb_intra) {
  148. /* DC coef */
  149. level = block[0];
  150. /* 255 cannot be represented, so we clamp */
  151. if (level > 254) {
  152. level = 254;
  153. block[0] = 254;
  154. }
  155. /* 0 cannot be represented also */
  156. else if (level < 1) {
  157. level = 1;
  158. block[0] = 1;
  159. }
  160. if (level == 128)
  161. put_bits(&s->pb, 8, 0xff);
  162. else
  163. put_bits(&s->pb, 8, level);
  164. i = 1;
  165. } else if ((block[0] == 1 || block[0] == -1) &&
  166. (s->block_last_index[n] > -1)) {
  167. // special case
  168. put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3);
  169. i = 1;
  170. } else {
  171. i = 0;
  172. }
  173. /* AC coefs */
  174. last_index = s->block_last_index[n];
  175. last_non_zero = i - 1;
  176. for (; i <= last_index; i++) {
  177. j = s->intra_scantable.permutated[i];
  178. level = block[j];
  179. if (level) {
  180. run = i - last_non_zero - 1;
  181. sign = 0;
  182. slevel = level;
  183. if (level < 0) {
  184. sign = 1;
  185. level = -level;
  186. }
  187. code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/,
  188. run, level);
  189. if (run == 0 && level < 16)
  190. code += 1;
  191. put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
  192. if (code == rl->n) {
  193. put_bits(&s->pb, 6, run);
  194. assert(slevel != 0);
  195. assert(level <= 127);
  196. put_sbits(&s->pb, 8, slevel);
  197. } else {
  198. put_bits(&s->pb, 1, sign);
  199. }
  200. last_non_zero = i;
  201. }
  202. }
  203. if (last_index > -1)
  204. put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]); // EOB
  205. }
  206. void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
  207. int motion_x, int motion_y)
  208. {
  209. H261Context *h = (H261Context *)s;
  210. int mvd, mv_diff_x, mv_diff_y, i, cbp;
  211. cbp = 63; // avoid warning
  212. mvd = 0;
  213. h->current_mba++;
  214. h->mtype = 0;
  215. if (!s->mb_intra) {
  216. /* compute cbp */
  217. cbp = get_cbp(s, block);
  218. /* mvd indicates if this block is motion compensated */
  219. mvd = motion_x | motion_y;
  220. if ((cbp | mvd | s->dquant) == 0) {
  221. /* skip macroblock */
  222. s->skip_count++;
  223. h->current_mv_x = 0;
  224. h->current_mv_y = 0;
  225. return;
  226. }
  227. }
  228. /* MB is not skipped, encode MBA */
  229. put_bits(&s->pb,
  230. ff_h261_mba_bits[(h->current_mba - h->previous_mba) - 1],
  231. ff_h261_mba_code[(h->current_mba - h->previous_mba) - 1]);
  232. /* calculate MTYPE */
  233. if (!s->mb_intra) {
  234. h->mtype++;
  235. if (mvd || s->loop_filter)
  236. h->mtype += 3;
  237. if (s->loop_filter)
  238. h->mtype += 3;
  239. if (cbp || s->dquant)
  240. h->mtype++;
  241. assert(h->mtype > 1);
  242. }
  243. if (s->dquant)
  244. h->mtype++;
  245. put_bits(&s->pb,
  246. ff_h261_mtype_bits[h->mtype],
  247. ff_h261_mtype_code[h->mtype]);
  248. h->mtype = ff_h261_mtype_map[h->mtype];
  249. if (IS_QUANT(h->mtype)) {
  250. ff_set_qscale(s, s->qscale + s->dquant);
  251. put_bits(&s->pb, 5, s->qscale);
  252. }
  253. if (IS_16X16(h->mtype)) {
  254. mv_diff_x = (motion_x >> 1) - h->current_mv_x;
  255. mv_diff_y = (motion_y >> 1) - h->current_mv_y;
  256. h->current_mv_x = (motion_x >> 1);
  257. h->current_mv_y = (motion_y >> 1);
  258. h261_encode_motion(h, mv_diff_x);
  259. h261_encode_motion(h, mv_diff_y);
  260. }
  261. h->previous_mba = h->current_mba;
  262. if (HAS_CBP(h->mtype)) {
  263. assert(cbp > 0);
  264. put_bits(&s->pb,
  265. ff_h261_cbp_tab[cbp - 1][1],
  266. ff_h261_cbp_tab[cbp - 1][0]);
  267. }
  268. for (i = 0; i < 6; i++)
  269. /* encode each block */
  270. h261_encode_block(h, block[i], i);
  271. if ((h->current_mba == 11) || (h->current_mba == 22) ||
  272. (h->current_mba == 33) || (!IS_16X16(h->mtype))) {
  273. h->current_mv_x = 0;
  274. h->current_mv_y = 0;
  275. }
  276. }
  277. av_cold void ff_h261_encode_init(MpegEncContext *s)
  278. {
  279. ff_h261_common_init();
  280. s->min_qcoeff = -127;
  281. s->max_qcoeff = 127;
  282. s->y_dc_scale_table =
  283. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  284. }
  285. static const AVClass h261_class = {
  286. .class_name = "h261 encoder",
  287. .item_name = av_default_item_name,
  288. .option = ff_mpv_generic_options,
  289. .version = LIBAVUTIL_VERSION_INT,
  290. };
  291. AVCodec ff_h261_encoder = {
  292. .name = "h261",
  293. .long_name = NULL_IF_CONFIG_SMALL("H.261"),
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .id = AV_CODEC_ID_H261,
  296. .priv_data_size = sizeof(H261Context),
  297. .init = ff_mpv_encode_init,
  298. .encode2 = ff_mpv_encode_picture,
  299. .close = ff_mpv_encode_end,
  300. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  301. AV_PIX_FMT_NONE },
  302. .priv_class = &h261_class,
  303. };