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.

391 lines
11KB

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