h261enc.c 9.1 KB

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