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.

336 lines
9.1KB

  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 "dsputil.h"
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include "h263.h"
  30. #include "h261.h"
  31. #include "h261data.h"
  32. extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
  33. static void h261_encode_block(H261Context * h, DCTELEM * block,
  34. int n);
  35. int ff_h261_get_picture_format(int width, int height){
  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. H261Context * h = (H261Context *) s;
  48. int format, temp_ref;
  49. 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, 0); /* freeze picture release 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, 0); /* still image mode */
  62. put_bits(&s->pb, 1, 0); /* 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. H261Context * h = (H261Context *)s;
  75. if(ff_h261_get_picture_format(s->width, s->height) == 0){
  76. h->gob_number+=2; // QCIF
  77. }
  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. int index= s->mb_x + s->mb_y*s->mb_width;
  92. if(index % 33 == 0)
  93. h261_encode_gob_header(s,0);
  94. /* for CIF the GOB's are fragmented in the middle of a scanline
  95. that's why we need to adjust the x and y index of the macroblocks */
  96. if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF
  97. s->mb_x = index % 11 ; index /= 11;
  98. s->mb_y = index % 3 ; index /= 3;
  99. s->mb_x+= 11*(index % 2); index /= 2;
  100. s->mb_y+= 3*index;
  101. ff_init_block_index(s);
  102. ff_update_block_index(s);
  103. }
  104. }
  105. static void h261_encode_motion(H261Context * h, int val){
  106. MpegEncContext * const s = &h->s;
  107. int sign, code;
  108. if(val==0){
  109. code = 0;
  110. put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
  111. }
  112. else{
  113. if(val > 15)
  114. val -=32;
  115. if(val < -16)
  116. val+=32;
  117. sign = val < 0;
  118. code = sign ? -val : val;
  119. put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
  120. put_bits(&s->pb,1,sign);
  121. }
  122. }
  123. static inline int get_cbp(MpegEncContext * s,
  124. DCTELEM block[6][64])
  125. {
  126. int i, cbp;
  127. cbp= 0;
  128. for (i = 0; i < 6; i++) {
  129. if (s->block_last_index[i] >= 0)
  130. cbp |= 1 << (5 - i);
  131. }
  132. return cbp;
  133. }
  134. void ff_h261_encode_mb(MpegEncContext * s,
  135. DCTELEM block[6][64],
  136. int motion_x, int motion_y)
  137. {
  138. H261Context * h = (H261Context *)s;
  139. int mvd, mv_diff_x, mv_diff_y, i, cbp;
  140. cbp = 63; // avoid warning
  141. mvd = 0;
  142. h->current_mba++;
  143. h->mtype = 0;
  144. if (!s->mb_intra){
  145. /* compute cbp */
  146. cbp= get_cbp(s, block);
  147. /* mvd indicates if this block is motion compensated */
  148. mvd = motion_x | motion_y;
  149. if((cbp | mvd | s->dquant ) == 0) {
  150. /* skip macroblock */
  151. s->skip_count++;
  152. h->current_mv_x=0;
  153. h->current_mv_y=0;
  154. return;
  155. }
  156. }
  157. /* MB is not skipped, encode MBA */
  158. put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]);
  159. /* calculate MTYPE */
  160. if(!s->mb_intra){
  161. h->mtype++;
  162. if(mvd || s->loop_filter)
  163. h->mtype+=3;
  164. if(s->loop_filter)
  165. h->mtype+=3;
  166. if(cbp || s->dquant)
  167. h->mtype++;
  168. assert(h->mtype > 1);
  169. }
  170. if(s->dquant)
  171. h->mtype++;
  172. put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]);
  173. h->mtype = h261_mtype_map[h->mtype];
  174. if(IS_QUANT(h->mtype)){
  175. ff_set_qscale(s,s->qscale+s->dquant);
  176. put_bits(&s->pb, 5, s->qscale);
  177. }
  178. if(IS_16X16(h->mtype)){
  179. mv_diff_x = (motion_x >> 1) - h->current_mv_x;
  180. mv_diff_y = (motion_y >> 1) - h->current_mv_y;
  181. h->current_mv_x = (motion_x >> 1);
  182. h->current_mv_y = (motion_y >> 1);
  183. h261_encode_motion(h,mv_diff_x);
  184. h261_encode_motion(h,mv_diff_y);
  185. }
  186. h->previous_mba = h->current_mba;
  187. if(HAS_CBP(h->mtype)){
  188. assert(cbp>0);
  189. put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]);
  190. }
  191. for(i=0; i<6; i++) {
  192. /* encode each block */
  193. h261_encode_block(h, block[i], i);
  194. }
  195. if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
  196. h->current_mv_x=0;
  197. h->current_mv_y=0;
  198. }
  199. }
  200. void ff_h261_encode_init(MpegEncContext *s){
  201. static int done = 0;
  202. if (!done) {
  203. done = 1;
  204. init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
  205. }
  206. s->min_qcoeff= -127;
  207. s->max_qcoeff= 127;
  208. s->y_dc_scale_table=
  209. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  210. }
  211. /**
  212. * encodes a 8x8 block.
  213. * @param block the 8x8 block
  214. * @param n block index (0-3 are luma, 4-5 are chroma)
  215. */
  216. static void h261_encode_block(H261Context * h, DCTELEM * block, int n){
  217. MpegEncContext * const s = &h->s;
  218. int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
  219. RLTable *rl;
  220. rl = &h261_rl_tcoeff;
  221. if (s->mb_intra) {
  222. /* DC coef */
  223. level = block[0];
  224. /* 255 cannot be represented, so we clamp */
  225. if (level > 254) {
  226. level = 254;
  227. block[0] = 254;
  228. }
  229. /* 0 cannot be represented also */
  230. else if (level < 1) {
  231. level = 1;
  232. block[0] = 1;
  233. }
  234. if (level == 128)
  235. put_bits(&s->pb, 8, 0xff);
  236. else
  237. put_bits(&s->pb, 8, level);
  238. i = 1;
  239. } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
  240. //special case
  241. put_bits(&s->pb,2,block[0]>0 ? 2 : 3 );
  242. i = 1;
  243. } else {
  244. i = 0;
  245. }
  246. /* AC coefs */
  247. last_index = s->block_last_index[n];
  248. last_non_zero = i - 1;
  249. for (; i <= last_index; i++) {
  250. j = s->intra_scantable.permutated[i];
  251. level = block[j];
  252. if (level) {
  253. run = i - last_non_zero - 1;
  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 ff_h261_encoder = {
  280. "h261",
  281. AVMEDIA_TYPE_VIDEO,
  282. CODEC_ID_H261,
  283. sizeof(H261Context),
  284. MPV_encode_init,
  285. MPV_encode_picture,
  286. MPV_encode_end,
  287. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  288. .long_name= NULL_IF_CONFIG_SMALL("H.261"),
  289. };