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.

339 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 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/avassert.h"
  27. #include "dsputil.h"
  28. #include "avcodec.h"
  29. #include "mpegvideo.h"
  30. #include "h263.h"
  31. #include "h261.h"
  32. #include "h261data.h"
  33. extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
  34. static void h261_encode_block(H261Context * h, DCTELEM * block,
  35. int n);
  36. int ff_h261_get_picture_format(int width, int height){
  37. // QCIF
  38. if (width == 176 && height == 144)
  39. return 0;
  40. // CIF
  41. else if (width == 352 && height == 288)
  42. return 1;
  43. // ERROR
  44. else
  45. return -1;
  46. }
  47. void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){
  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. H261Context * h = (H261Context *)s;
  76. if(ff_h261_get_picture_format(s->width, s->height) == 0){
  77. h->gob_number+=2; // QCIF
  78. }
  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. 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 ; index /= 11;
  99. s->mb_y = index % 3 ; index /= 3;
  100. s->mb_x+= 11*(index % 2); index /= 2;
  101. s->mb_y+= 3*index;
  102. ff_init_block_index(s);
  103. ff_update_block_index(s);
  104. }
  105. }
  106. static void h261_encode_motion(H261Context * h, int val){
  107. MpegEncContext * const s = &h->s;
  108. int sign, code;
  109. if(val==0){
  110. code = 0;
  111. put_bits(&s->pb,ff_h261_mv_tab[code][1],ff_h261_mv_tab[code][0]);
  112. }
  113. else{
  114. if(val > 15)
  115. val -=32;
  116. if(val < -16)
  117. val+=32;
  118. sign = val < 0;
  119. code = sign ? -val : val;
  120. put_bits(&s->pb,ff_h261_mv_tab[code][1],ff_h261_mv_tab[code][0]);
  121. put_bits(&s->pb,1,sign);
  122. }
  123. }
  124. static inline int get_cbp(MpegEncContext * s,
  125. DCTELEM 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. }
  133. return cbp;
  134. }
  135. void ff_h261_encode_mb(MpegEncContext * s,
  136. DCTELEM block[6][64],
  137. int motion_x, int motion_y)
  138. {
  139. H261Context * h = (H261Context *)s;
  140. int mvd, mv_diff_x, mv_diff_y, i, cbp;
  141. cbp = 63; // avoid warning
  142. mvd = 0;
  143. h->current_mba++;
  144. h->mtype = 0;
  145. if (!s->mb_intra){
  146. /* compute cbp */
  147. cbp= get_cbp(s, block);
  148. /* mvd indicates if this block is motion compensated */
  149. mvd = motion_x | motion_y;
  150. if((cbp | mvd | s->dquant ) == 0) {
  151. /* skip macroblock */
  152. s->skip_count++;
  153. h->current_mv_x=0;
  154. h->current_mv_y=0;
  155. return;
  156. }
  157. }
  158. /* MB is not skipped, encode MBA */
  159. put_bits(&s->pb, ff_h261_mba_bits[(h->current_mba-h->previous_mba)-1], ff_h261_mba_code[(h->current_mba-h->previous_mba)-1]);
  160. /* calculate MTYPE */
  161. if(!s->mb_intra){
  162. h->mtype++;
  163. if(mvd || s->loop_filter)
  164. h->mtype+=3;
  165. if(s->loop_filter)
  166. h->mtype+=3;
  167. if(cbp || s->dquant)
  168. h->mtype++;
  169. av_assert1(h->mtype > 1);
  170. }
  171. if(s->dquant)
  172. h->mtype++;
  173. put_bits(&s->pb, ff_h261_mtype_bits[h->mtype], ff_h261_mtype_code[h->mtype]);
  174. h->mtype = ff_h261_mtype_map[h->mtype];
  175. if(IS_QUANT(h->mtype)){
  176. ff_set_qscale(s,s->qscale+s->dquant);
  177. put_bits(&s->pb, 5, s->qscale);
  178. }
  179. if(IS_16X16(h->mtype)){
  180. mv_diff_x = (motion_x >> 1) - h->current_mv_x;
  181. mv_diff_y = (motion_y >> 1) - h->current_mv_y;
  182. h->current_mv_x = (motion_x >> 1);
  183. h->current_mv_y = (motion_y >> 1);
  184. h261_encode_motion(h,mv_diff_x);
  185. h261_encode_motion(h,mv_diff_y);
  186. }
  187. h->previous_mba = h->current_mba;
  188. if(HAS_CBP(h->mtype)){
  189. av_assert1(cbp>0);
  190. put_bits(&s->pb,ff_h261_cbp_tab[cbp-1][1],ff_h261_cbp_tab[cbp-1][0]);
  191. }
  192. for(i=0; i<6; i++) {
  193. /* encode each block */
  194. h261_encode_block(h, block[i], i);
  195. }
  196. if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
  197. h->current_mv_x=0;
  198. h->current_mv_y=0;
  199. }
  200. }
  201. void ff_h261_encode_init(MpegEncContext *s){
  202. static int done = 0;
  203. if (!done) {
  204. done = 1;
  205. ff_init_rl(&ff_h261_rl_tcoeff, ff_h261_rl_table_store);
  206. }
  207. s->min_qcoeff= -127;
  208. s->max_qcoeff= 127;
  209. s->y_dc_scale_table=
  210. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  211. }
  212. /**
  213. * Encode an 8x8 block.
  214. * @param block the 8x8 block
  215. * @param n block index (0-3 are luma, 4-5 are chroma)
  216. */
  217. static void h261_encode_block(H261Context * h, DCTELEM * block, int n){
  218. MpegEncContext * const s = &h->s;
  219. int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
  220. RLTable *rl;
  221. rl = &ff_h261_rl_tcoeff;
  222. if (s->mb_intra) {
  223. /* DC coef */
  224. level = block[0];
  225. /* 255 cannot be represented, so we clamp */
  226. if (level > 254) {
  227. level = 254;
  228. block[0] = 254;
  229. }
  230. /* 0 cannot be represented also */
  231. else if (level < 1) {
  232. level = 1;
  233. block[0] = 1;
  234. }
  235. if (level == 128)
  236. put_bits(&s->pb, 8, 0xff);
  237. else
  238. put_bits(&s->pb, 8, level);
  239. i = 1;
  240. } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
  241. //special case
  242. put_bits(&s->pb,2,block[0]>0 ? 2 : 3 );
  243. i = 1;
  244. } else {
  245. i = 0;
  246. }
  247. /* AC coefs */
  248. last_index = s->block_last_index[n];
  249. last_non_zero = i - 1;
  250. for (; i <= last_index; i++) {
  251. j = s->intra_scantable.permutated[i];
  252. level = block[j];
  253. if (level) {
  254. run = i - last_non_zero - 1;
  255. sign = 0;
  256. slevel = level;
  257. if (level < 0) {
  258. sign = 1;
  259. level = -level;
  260. }
  261. code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level);
  262. if(run==0 && level < 16)
  263. code+=1;
  264. put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
  265. if (code == rl->n) {
  266. put_bits(&s->pb, 6, run);
  267. av_assert1(slevel != 0);
  268. av_assert1(level <= 127);
  269. put_sbits(&s->pb, 8, slevel);
  270. } else {
  271. put_bits(&s->pb, 1, sign);
  272. }
  273. last_non_zero = i;
  274. }
  275. }
  276. if(last_index > -1){
  277. put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK
  278. }
  279. }
  280. FF_MPV_GENERIC_CLASS(h261)
  281. AVCodec ff_h261_encoder = {
  282. .name = "h261",
  283. .type = AVMEDIA_TYPE_VIDEO,
  284. .id = AV_CODEC_ID_H261,
  285. .priv_data_size = sizeof(H261Context),
  286. .init = ff_MPV_encode_init,
  287. .encode2 = ff_MPV_encode_picture,
  288. .close = ff_MPV_encode_end,
  289. .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
  290. .long_name = NULL_IF_CONFIG_SMALL("H.261"),
  291. .priv_class = &h261_class,
  292. };