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.

401 lines
11KB

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