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.

4164 lines
142KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  20. */
  21. #include <ctype.h>
  22. #include <limits.h>
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. #include "simple_idct.h"
  27. #ifdef USE_FASTMEMCPY
  28. #include "fastmemcpy.h"
  29. #endif
  30. //#undef NDEBUG
  31. //#include <assert.h>
  32. #ifdef CONFIG_ENCODERS
  33. static void encode_picture(MpegEncContext *s, int picture_number);
  34. #endif //CONFIG_ENCODERS
  35. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  36. DCTELEM *block, int n, int qscale);
  37. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  38. DCTELEM *block, int n, int qscale);
  39. static void dct_unquantize_h263_c(MpegEncContext *s,
  40. DCTELEM *block, int n, int qscale);
  41. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
  42. #ifdef CONFIG_ENCODERS
  43. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  44. static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  45. #endif //CONFIG_ENCODERS
  46. void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
  47. /* enable all paranoid tests for rounding, overflows, etc... */
  48. //#define PARANOID
  49. //#define DEBUG
  50. /* for jpeg fast DCT */
  51. #define CONST_BITS 14
  52. static const uint16_t aanscales[64] = {
  53. /* precomputed values scaled up by 14 bits */
  54. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  55. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  56. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  57. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  58. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  59. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  60. 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  61. 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
  62. };
  63. /* Input permutation for the simple_idct_mmx */
  64. static const uint8_t simple_mmx_permutation[64]={
  65. 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
  66. 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
  67. 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
  68. 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
  69. 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
  70. 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
  71. 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
  72. 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
  73. };
  74. static const uint8_t h263_chroma_roundtab[16] = {
  75. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  76. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  77. };
  78. #ifdef CONFIG_ENCODERS
  79. static uint16_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
  80. static uint8_t default_fcode_tab[MAX_MV*2+1];
  81. enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
  82. static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
  83. const uint16_t *quant_matrix, int bias, int qmin, int qmax)
  84. {
  85. int qscale;
  86. for(qscale=qmin; qscale<=qmax; qscale++){
  87. int i;
  88. if (s->fdct == ff_jpeg_fdct_islow) {
  89. for(i=0;i<64;i++) {
  90. const int j= s->idct_permutation[i];
  91. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  92. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  93. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  94. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  95. qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
  96. (qscale * quant_matrix[j]));
  97. }
  98. } else if (s->fdct == fdct_ifast) {
  99. for(i=0;i<64;i++) {
  100. const int j= s->idct_permutation[i];
  101. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  102. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  103. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  104. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  105. qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
  106. (aanscales[i] * qscale * quant_matrix[j]));
  107. }
  108. } else {
  109. for(i=0;i<64;i++) {
  110. const int j= s->idct_permutation[i];
  111. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  112. So 16 <= qscale * quant_matrix[i] <= 7905
  113. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  114. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  115. */
  116. qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
  117. // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  118. qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
  119. if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
  120. qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
  121. }
  122. }
  123. }
  124. }
  125. #endif //CONFIG_ENCODERS
  126. // move into common.c perhaps
  127. #define CHECKED_ALLOCZ(p, size)\
  128. {\
  129. p= av_mallocz(size);\
  130. if(p==NULL){\
  131. perror("malloc");\
  132. goto fail;\
  133. }\
  134. }
  135. void ff_init_scantable(MpegEncContext *s, ScanTable *st, const uint8_t *src_scantable){
  136. int i;
  137. int end;
  138. st->scantable= src_scantable;
  139. for(i=0; i<64; i++){
  140. int j;
  141. j = src_scantable[i];
  142. st->permutated[i] = s->idct_permutation[j];
  143. #ifdef ARCH_POWERPC
  144. st->inverse[j] = i;
  145. #endif
  146. }
  147. end=-1;
  148. for(i=0; i<64; i++){
  149. int j;
  150. j = st->permutated[i];
  151. if(j>end) end=j;
  152. st->raster_end[i]= end;
  153. }
  154. }
  155. /* XXX: those functions should be suppressed ASAP when all IDCTs are
  156. converted */
  157. // *FIXME* this is ugly hack using local static
  158. static void (*ff_put_pixels_clamped)(const DCTELEM *block, uint8_t *pixels, int line_size);
  159. static void (*ff_add_pixels_clamped)(const DCTELEM *block, uint8_t *pixels, int line_size);
  160. static void ff_jref_idct_put(uint8_t *dest, int line_size, DCTELEM *block)
  161. {
  162. j_rev_dct (block);
  163. ff_put_pixels_clamped(block, dest, line_size);
  164. }
  165. static void ff_jref_idct_add(uint8_t *dest, int line_size, DCTELEM *block)
  166. {
  167. j_rev_dct (block);
  168. ff_add_pixels_clamped(block, dest, line_size);
  169. }
  170. /* init common dct for both encoder and decoder */
  171. int DCT_common_init(MpegEncContext *s)
  172. {
  173. int i;
  174. ff_put_pixels_clamped = s->dsp.put_pixels_clamped;
  175. ff_add_pixels_clamped = s->dsp.add_pixels_clamped;
  176. s->dct_unquantize_h263 = dct_unquantize_h263_c;
  177. s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
  178. s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
  179. #ifdef CONFIG_ENCODERS
  180. s->dct_quantize= dct_quantize_c;
  181. if(s->avctx->dct_algo==FF_DCT_FASTINT)
  182. s->fdct = fdct_ifast;
  183. else
  184. s->fdct = ff_jpeg_fdct_islow; //slow/accurate/default
  185. #endif //CONFIG_ENCODERS
  186. if(s->avctx->idct_algo==FF_IDCT_INT){
  187. s->idct_put= ff_jref_idct_put;
  188. s->idct_add= ff_jref_idct_add;
  189. s->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM;
  190. }else{ //accurate/default
  191. s->idct_put= simple_idct_put;
  192. s->idct_add= simple_idct_add;
  193. s->idct_permutation_type= FF_NO_IDCT_PERM;
  194. }
  195. #ifdef HAVE_MMX
  196. MPV_common_init_mmx(s);
  197. #endif
  198. #ifdef ARCH_ALPHA
  199. MPV_common_init_axp(s);
  200. #endif
  201. #ifdef HAVE_MLIB
  202. MPV_common_init_mlib(s);
  203. #endif
  204. #ifdef HAVE_MMI
  205. MPV_common_init_mmi(s);
  206. #endif
  207. #ifdef ARCH_ARMV4L
  208. MPV_common_init_armv4l(s);
  209. #endif
  210. #ifdef ARCH_POWERPC
  211. MPV_common_init_ppc(s);
  212. #endif
  213. #ifdef CONFIG_ENCODERS
  214. s->fast_dct_quantize= s->dct_quantize;
  215. if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
  216. s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
  217. }
  218. #endif //CONFIG_ENCODERS
  219. switch(s->idct_permutation_type){
  220. case FF_NO_IDCT_PERM:
  221. for(i=0; i<64; i++)
  222. s->idct_permutation[i]= i;
  223. break;
  224. case FF_LIBMPEG2_IDCT_PERM:
  225. for(i=0; i<64; i++)
  226. s->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
  227. break;
  228. case FF_SIMPLE_IDCT_PERM:
  229. for(i=0; i<64; i++)
  230. s->idct_permutation[i]= simple_mmx_permutation[i];
  231. break;
  232. case FF_TRANSPOSE_IDCT_PERM:
  233. for(i=0; i<64; i++)
  234. s->idct_permutation[i]= ((i&7)<<3) | (i>>3);
  235. break;
  236. default:
  237. fprintf(stderr, "Internal error, IDCT permutation not set\n");
  238. return -1;
  239. }
  240. /* load & permutate scantables
  241. note: only wmv uses differnt ones
  242. */
  243. ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct);
  244. ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct);
  245. ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  246. ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
  247. return 0;
  248. }
  249. /**
  250. * allocates a Picture
  251. * The pixels are allocated/set by calling get_buffer() if shared=0
  252. */
  253. static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  254. if(shared){
  255. assert(pic->data[0]);
  256. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  257. pic->type= FF_BUFFER_TYPE_SHARED;
  258. }else{
  259. int r;
  260. assert(!pic->data[0]);
  261. r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
  262. if(r<0 || !pic->age || !pic->type || !pic->data[0]){
  263. fprintf(stderr, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  264. return -1;
  265. }
  266. if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
  267. fprintf(stderr, "get_buffer() failed (stride changed)\n");
  268. return -1;
  269. }
  270. if(pic->linesize[1] != pic->linesize[2]){
  271. fprintf(stderr, "get_buffer() failed (uv stride missmatch)\n");
  272. return -1;
  273. }
  274. s->linesize = pic->linesize[0];
  275. s->uvlinesize= pic->linesize[1];
  276. }
  277. if(pic->qscale_table==NULL){
  278. if (s->encoding) {
  279. CHECKED_ALLOCZ(pic->mb_var , s->mb_num * sizeof(int16_t))
  280. CHECKED_ALLOCZ(pic->mc_mb_var, s->mb_num * sizeof(int16_t))
  281. CHECKED_ALLOCZ(pic->mb_mean , s->mb_num * sizeof(int8_t))
  282. CHECKED_ALLOCZ(pic->mb_cmp_score, s->mb_num * sizeof(int32_t))
  283. }
  284. CHECKED_ALLOCZ(pic->mbskip_table , s->mb_num * sizeof(uint8_t)+1) //the +1 is for the slice end check
  285. CHECKED_ALLOCZ(pic->qscale_table , s->mb_num * sizeof(uint8_t))
  286. pic->qstride= s->mb_width;
  287. }
  288. //it might be nicer if the application would keep track of these but it would require a API change
  289. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  290. s->prev_pict_types[0]= s->pict_type;
  291. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
  292. pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
  293. return 0;
  294. fail: //for the CHECKED_ALLOCZ macro
  295. return -1;
  296. }
  297. /**
  298. * deallocates a picture
  299. */
  300. static void free_picture(MpegEncContext *s, Picture *pic){
  301. int i;
  302. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  303. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  304. }
  305. av_freep(&pic->mb_var);
  306. av_freep(&pic->mc_mb_var);
  307. av_freep(&pic->mb_mean);
  308. av_freep(&pic->mb_cmp_score);
  309. av_freep(&pic->mbskip_table);
  310. av_freep(&pic->qscale_table);
  311. if(pic->type == FF_BUFFER_TYPE_INTERNAL){
  312. for(i=0; i<4; i++){
  313. av_freep(&pic->base[i]);
  314. pic->data[i]= NULL;
  315. }
  316. av_freep(&pic->opaque);
  317. pic->type= 0;
  318. }else if(pic->type == FF_BUFFER_TYPE_SHARED){
  319. for(i=0; i<4; i++){
  320. pic->base[i]=
  321. pic->data[i]= NULL;
  322. }
  323. pic->type= 0;
  324. }
  325. }
  326. /* init common structure for both encoder and decoder */
  327. int MPV_common_init(MpegEncContext *s)
  328. {
  329. int y_size, c_size, yc_size, i;
  330. dsputil_init(&s->dsp, s->avctx->dsp_mask);
  331. DCT_common_init(s);
  332. s->flags= s->avctx->flags;
  333. s->mb_width = (s->width + 15) / 16;
  334. s->mb_height = (s->height + 15) / 16;
  335. /* set default edge pos, will be overriden in decode_header if needed */
  336. s->h_edge_pos= s->mb_width*16;
  337. s->v_edge_pos= s->mb_height*16;
  338. s->mb_num = s->mb_width * s->mb_height;
  339. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  340. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  341. yc_size = y_size + 2 * c_size;
  342. /* convert fourcc to upper case */
  343. s->avctx->fourcc= toupper( s->avctx->fourcc &0xFF)
  344. + (toupper((s->avctx->fourcc>>8 )&0xFF)<<8 )
  345. + (toupper((s->avctx->fourcc>>16)&0xFF)<<16)
  346. + (toupper((s->avctx->fourcc>>24)&0xFF)<<24);
  347. CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
  348. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
  349. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  350. if (s->encoding) {
  351. int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
  352. /* Allocate MV tables */
  353. CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(int16_t))
  354. CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(int16_t))
  355. CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(int16_t))
  356. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(int16_t))
  357. CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(int16_t))
  358. CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(int16_t))
  359. //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
  360. CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t))
  361. CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
  362. CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
  363. if(s->codec_id==CODEC_ID_MPEG4){
  364. CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
  365. CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
  366. }
  367. if(s->msmpeg4_version){
  368. CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
  369. }
  370. CHECKED_ALLOCZ(s->avctx->stats_out, 256);
  371. }
  372. CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(uint8_t))
  373. if (s->out_format == FMT_H263 || s->encoding) {
  374. int size;
  375. /* Allocate MB type table */
  376. CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(uint8_t))
  377. /* MV prediction */
  378. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  379. CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
  380. }
  381. if(s->codec_id==CODEC_ID_MPEG4){
  382. /* interlaced direct mode decoding tables */
  383. CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(int16_t))
  384. CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(int8_t))
  385. }
  386. /* 4mv b frame decoding table */
  387. //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
  388. CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(uint8_t))
  389. if (s->out_format == FMT_H263) {
  390. /* ac values */
  391. CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
  392. s->ac_val[1] = s->ac_val[0] + y_size;
  393. s->ac_val[2] = s->ac_val[1] + c_size;
  394. /* cbp values */
  395. CHECKED_ALLOCZ(s->coded_block, y_size);
  396. /* divx501 bitstream reorder buffer */
  397. CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
  398. /* cbp, ac_pred, pred_dir */
  399. CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(uint8_t))
  400. CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(uint8_t))
  401. }
  402. if (s->h263_pred || s->h263_plus || !s->encoding) {
  403. /* dc values */
  404. //MN: we need these for error resilience of intra-frames
  405. CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
  406. s->dc_val[1] = s->dc_val[0] + y_size;
  407. s->dc_val[2] = s->dc_val[1] + c_size;
  408. for(i=0;i<yc_size;i++)
  409. s->dc_val[0][i] = 1024;
  410. }
  411. /* which mb is a intra block */
  412. CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
  413. memset(s->mbintra_table, 1, s->mb_num);
  414. /* default structure is frame */
  415. s->picture_structure = PICT_FRAME;
  416. /* init macroblock skip table */
  417. CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
  418. //Note the +1 is for a quicker mpeg4 slice_end detection
  419. CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  420. s->block= s->blocks[0];
  421. s->parse_context.state= -1;
  422. s->context_initialized = 1;
  423. return 0;
  424. fail:
  425. MPV_common_end(s);
  426. return -1;
  427. }
  428. //extern int sads;
  429. /* init common structure for both encoder and decoder */
  430. void MPV_common_end(MpegEncContext *s)
  431. {
  432. int i;
  433. av_freep(&s->mb_type);
  434. av_freep(&s->p_mv_table);
  435. av_freep(&s->b_forw_mv_table);
  436. av_freep(&s->b_back_mv_table);
  437. av_freep(&s->b_bidir_forw_mv_table);
  438. av_freep(&s->b_bidir_back_mv_table);
  439. av_freep(&s->b_direct_mv_table);
  440. av_freep(&s->motion_val);
  441. av_freep(&s->dc_val[0]);
  442. av_freep(&s->ac_val[0]);
  443. av_freep(&s->coded_block);
  444. av_freep(&s->mbintra_table);
  445. av_freep(&s->cbp_table);
  446. av_freep(&s->pred_dir_table);
  447. av_freep(&s->me.scratchpad);
  448. av_freep(&s->me.map);
  449. av_freep(&s->me.score_map);
  450. av_freep(&s->mbskip_table);
  451. av_freep(&s->prev_pict_types);
  452. av_freep(&s->bitstream_buffer);
  453. av_freep(&s->tex_pb_buffer);
  454. av_freep(&s->pb2_buffer);
  455. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  456. av_freep(&s->co_located_type_table);
  457. av_freep(&s->field_mv_table);
  458. av_freep(&s->field_select_table);
  459. av_freep(&s->avctx->stats_out);
  460. av_freep(&s->ac_stats);
  461. av_freep(&s->error_status_table);
  462. for(i=0; i<MAX_PICTURE_COUNT; i++){
  463. free_picture(s, &s->picture[i]);
  464. }
  465. s->context_initialized = 0;
  466. }
  467. #ifdef CONFIG_ENCODERS
  468. /* init video encoder */
  469. int MPV_encode_init(AVCodecContext *avctx)
  470. {
  471. MpegEncContext *s = avctx->priv_data;
  472. int i;
  473. avctx->pix_fmt = PIX_FMT_YUV420P;
  474. s->bit_rate = avctx->bit_rate;
  475. s->bit_rate_tolerance = avctx->bit_rate_tolerance;
  476. s->frame_rate = avctx->frame_rate;
  477. s->width = avctx->width;
  478. s->height = avctx->height;
  479. if(avctx->gop_size > 600){
  480. fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
  481. avctx->gop_size=600;
  482. }
  483. s->gop_size = avctx->gop_size;
  484. s->rtp_mode = avctx->rtp_mode;
  485. s->rtp_payload_size = avctx->rtp_payload_size;
  486. if (avctx->rtp_callback)
  487. s->rtp_callback = avctx->rtp_callback;
  488. s->qmin= avctx->qmin;
  489. s->qmax= avctx->qmax;
  490. s->max_qdiff= avctx->max_qdiff;
  491. s->qcompress= avctx->qcompress;
  492. s->qblur= avctx->qblur;
  493. s->avctx = avctx;
  494. s->flags= avctx->flags;
  495. s->max_b_frames= avctx->max_b_frames;
  496. s->b_frame_strategy= avctx->b_frame_strategy;
  497. s->codec_id= avctx->codec->id;
  498. s->luma_elim_threshold = avctx->luma_elim_threshold;
  499. s->chroma_elim_threshold= avctx->chroma_elim_threshold;
  500. s->strict_std_compliance= avctx->strict_std_compliance;
  501. s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
  502. s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
  503. s->mpeg_quant= avctx->mpeg_quant;
  504. if (s->gop_size <= 1) {
  505. s->intra_only = 1;
  506. s->gop_size = 12;
  507. } else {
  508. s->intra_only = 0;
  509. }
  510. s->me_method = avctx->me_method;
  511. /* Fixed QSCALE */
  512. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  513. s->adaptive_quant= ( s->avctx->lumi_masking
  514. || s->avctx->dark_masking
  515. || s->avctx->temporal_cplx_masking
  516. || s->avctx->spatial_cplx_masking
  517. || s->avctx->p_masking)
  518. && !s->fixed_qscale;
  519. s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  520. switch(avctx->codec->id) {
  521. case CODEC_ID_MPEG1VIDEO:
  522. s->out_format = FMT_MPEG1;
  523. avctx->delay=0; //FIXME not sure, should check the spec
  524. break;
  525. case CODEC_ID_MJPEG:
  526. s->out_format = FMT_MJPEG;
  527. s->intra_only = 1; /* force intra only for jpeg */
  528. s->mjpeg_write_tables = 1; /* write all tables */
  529. s->mjpeg_data_only_frames = 0; /* write all the needed headers */
  530. s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
  531. s->mjpeg_vsample[1] = 1; /* the only currently supported values */
  532. s->mjpeg_vsample[2] = 1;
  533. s->mjpeg_hsample[0] = 2;
  534. s->mjpeg_hsample[1] = 1;
  535. s->mjpeg_hsample[2] = 1;
  536. if (mjpeg_init(s) < 0)
  537. return -1;
  538. avctx->delay=0;
  539. s->low_delay=1;
  540. break;
  541. #ifdef CONFIG_RISKY
  542. case CODEC_ID_H263:
  543. if (h263_get_picture_format(s->width, s->height) == 7) {
  544. printf("Input picture size isn't suitable for h263 codec! try h263+\n");
  545. return -1;
  546. }
  547. s->out_format = FMT_H263;
  548. avctx->delay=0;
  549. s->low_delay=1;
  550. break;
  551. case CODEC_ID_H263P:
  552. s->out_format = FMT_H263;
  553. s->h263_plus = 1;
  554. s->unrestricted_mv = 1;
  555. s->h263_aic = 1;
  556. /* These are just to be sure */
  557. s->umvplus = 0;
  558. s->umvplus_dec = 0;
  559. avctx->delay=0;
  560. s->low_delay=1;
  561. break;
  562. case CODEC_ID_RV10:
  563. s->out_format = FMT_H263;
  564. s->h263_rv10 = 1;
  565. avctx->delay=0;
  566. s->low_delay=1;
  567. break;
  568. case CODEC_ID_MPEG4:
  569. s->out_format = FMT_H263;
  570. s->h263_pred = 1;
  571. s->unrestricted_mv = 1;
  572. s->low_delay= s->max_b_frames ? 0 : 1;
  573. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  574. break;
  575. case CODEC_ID_MSMPEG4V1:
  576. s->out_format = FMT_H263;
  577. s->h263_msmpeg4 = 1;
  578. s->h263_pred = 1;
  579. s->unrestricted_mv = 1;
  580. s->msmpeg4_version= 1;
  581. avctx->delay=0;
  582. s->low_delay=1;
  583. break;
  584. case CODEC_ID_MSMPEG4V2:
  585. s->out_format = FMT_H263;
  586. s->h263_msmpeg4 = 1;
  587. s->h263_pred = 1;
  588. s->unrestricted_mv = 1;
  589. s->msmpeg4_version= 2;
  590. avctx->delay=0;
  591. s->low_delay=1;
  592. break;
  593. case CODEC_ID_MSMPEG4V3:
  594. s->out_format = FMT_H263;
  595. s->h263_msmpeg4 = 1;
  596. s->h263_pred = 1;
  597. s->unrestricted_mv = 1;
  598. s->msmpeg4_version= 3;
  599. avctx->delay=0;
  600. s->low_delay=1;
  601. break;
  602. case CODEC_ID_WMV1:
  603. s->out_format = FMT_H263;
  604. s->h263_msmpeg4 = 1;
  605. s->h263_pred = 1;
  606. s->unrestricted_mv = 1;
  607. s->msmpeg4_version= 4;
  608. avctx->delay=0;
  609. s->low_delay=1;
  610. break;
  611. case CODEC_ID_WMV2:
  612. s->out_format = FMT_H263;
  613. s->h263_msmpeg4 = 1;
  614. s->h263_pred = 1;
  615. s->unrestricted_mv = 1;
  616. s->msmpeg4_version= 5;
  617. avctx->delay=0;
  618. s->low_delay=1;
  619. break;
  620. #endif
  621. default:
  622. return -1;
  623. }
  624. { /* set up some save defaults, some codecs might override them later */
  625. static int done=0;
  626. if(!done){
  627. int i;
  628. done=1;
  629. default_mv_penalty= av_mallocz( sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
  630. memset(default_mv_penalty, 0, sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
  631. memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
  632. for(i=-16; i<16; i++){
  633. default_fcode_tab[i + MAX_MV]= 1;
  634. }
  635. }
  636. }
  637. s->me.mv_penalty= default_mv_penalty;
  638. s->fcode_tab= default_fcode_tab;
  639. s->y_dc_scale_table=
  640. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  641. /* dont use mv_penalty table for crap MV as it would be confused */
  642. //FIXME remove after fixing / removing old ME
  643. if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
  644. s->encoding = 1;
  645. /* init */
  646. if (MPV_common_init(s) < 0)
  647. return -1;
  648. ff_init_me(s);
  649. #ifdef CONFIG_ENCODERS
  650. #ifdef CONFIG_RISKY
  651. if (s->out_format == FMT_H263)
  652. h263_encode_init(s);
  653. if(s->msmpeg4_version)
  654. ff_msmpeg4_encode_init(s);
  655. #endif
  656. if (s->out_format == FMT_MPEG1)
  657. ff_mpeg1_encode_init(s);
  658. #endif
  659. /* init default q matrix */
  660. for(i=0;i<64;i++) {
  661. int j= s->idct_permutation[i];
  662. #ifdef CONFIG_RISKY
  663. if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
  664. s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
  665. s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
  666. }else if(s->out_format == FMT_H263){
  667. s->intra_matrix[j] =
  668. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  669. }else
  670. #endif
  671. { /* mpeg1 */
  672. s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
  673. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  674. }
  675. }
  676. /* precompute matrix */
  677. /* for mjpeg, we do include qscale in the matrix */
  678. if (s->out_format != FMT_MJPEG) {
  679. convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
  680. s->intra_matrix, s->intra_quant_bias, 1, 31);
  681. convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
  682. s->inter_matrix, s->inter_quant_bias, 1, 31);
  683. }
  684. if(ff_rate_control_init(s) < 0)
  685. return -1;
  686. s->picture_number = 0;
  687. s->picture_in_gop_number = 0;
  688. s->fake_picture_number = 0;
  689. /* motion detector init */
  690. s->f_code = 1;
  691. s->b_code = 1;
  692. return 0;
  693. }
  694. int MPV_encode_end(AVCodecContext *avctx)
  695. {
  696. MpegEncContext *s = avctx->priv_data;
  697. #ifdef STATS
  698. print_stats();
  699. #endif
  700. ff_rate_control_uninit(s);
  701. MPV_common_end(s);
  702. if (s->out_format == FMT_MJPEG)
  703. mjpeg_close(s);
  704. return 0;
  705. }
  706. #endif //CONFIG_ENCODERS
  707. void init_rl(RLTable *rl)
  708. {
  709. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  710. uint8_t index_run[MAX_RUN+1];
  711. int last, run, level, start, end, i;
  712. /* compute max_level[], max_run[] and index_run[] */
  713. for(last=0;last<2;last++) {
  714. if (last == 0) {
  715. start = 0;
  716. end = rl->last;
  717. } else {
  718. start = rl->last;
  719. end = rl->n;
  720. }
  721. memset(max_level, 0, MAX_RUN + 1);
  722. memset(max_run, 0, MAX_LEVEL + 1);
  723. memset(index_run, rl->n, MAX_RUN + 1);
  724. for(i=start;i<end;i++) {
  725. run = rl->table_run[i];
  726. level = rl->table_level[i];
  727. if (index_run[run] == rl->n)
  728. index_run[run] = i;
  729. if (level > max_level[run])
  730. max_level[run] = level;
  731. if (run > max_run[level])
  732. max_run[level] = run;
  733. }
  734. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  735. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  736. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  737. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  738. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  739. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  740. }
  741. }
  742. /* draw the edges of width 'w' of an image of size width, height */
  743. //FIXME check that this is ok for mpeg4 interlaced
  744. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
  745. {
  746. uint8_t *ptr, *last_line;
  747. int i;
  748. last_line = buf + (height - 1) * wrap;
  749. for(i=0;i<w;i++) {
  750. /* top and bottom */
  751. memcpy(buf - (i + 1) * wrap, buf, width);
  752. memcpy(last_line + (i + 1) * wrap, last_line, width);
  753. }
  754. /* left and right */
  755. ptr = buf;
  756. for(i=0;i<height;i++) {
  757. memset(ptr - w, ptr[0], w);
  758. memset(ptr + width, ptr[width-1], w);
  759. ptr += wrap;
  760. }
  761. /* corners */
  762. for(i=0;i<w;i++) {
  763. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  764. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  765. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  766. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  767. }
  768. }
  769. static int find_unused_picture(MpegEncContext *s, int shared){
  770. int i;
  771. if(shared){
  772. for(i=0; i<MAX_PICTURE_COUNT; i++){
  773. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
  774. }
  775. }else{
  776. for(i=0; i<MAX_PICTURE_COUNT; i++){
  777. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break;
  778. }
  779. for(i=0; i<MAX_PICTURE_COUNT; i++){
  780. if(s->picture[i].data[0]==NULL) break;
  781. }
  782. }
  783. assert(i<MAX_PICTURE_COUNT);
  784. return i;
  785. }
  786. /* generic function for encode/decode called before a frame is coded/decoded */
  787. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  788. {
  789. int i;
  790. AVFrame *pic;
  791. s->mb_skiped = 0;
  792. /* mark&release old frames */
  793. if (s->pict_type != B_TYPE && s->last_picture.data[0]) {
  794. for(i=0; i<MAX_PICTURE_COUNT; i++){
  795. //printf("%8X %d %d %X %X\n", s->picture[i].data[0], s->picture[i].type, i, s->next_picture.data[0], s->last_picture.data[0]);
  796. if(s->picture[i].data[0] == s->last_picture.data[0]){
  797. // s->picture[i].reference=0;
  798. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  799. break;
  800. }
  801. }
  802. assert(i<MAX_PICTURE_COUNT);
  803. /* release forgotten pictures */
  804. /* if(mpeg124/h263) */
  805. if(!s->encoding){
  806. for(i=0; i<MAX_PICTURE_COUNT; i++){
  807. if(s->picture[i].data[0] && s->picture[i].data[0] != s->next_picture.data[0] && s->picture[i].reference){
  808. fprintf(stderr, "releasing zombie picture\n");
  809. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  810. }
  811. }
  812. }
  813. }
  814. alloc:
  815. if(!s->encoding){
  816. i= find_unused_picture(s, 0);
  817. pic= (AVFrame*)&s->picture[i];
  818. pic->reference= s->pict_type != B_TYPE;
  819. pic->coded_picture_number= s->current_picture.coded_picture_number+1;
  820. alloc_picture(s, (Picture*)pic, 0);
  821. s->current_picture= s->picture[i];
  822. }
  823. if (s->pict_type != B_TYPE) {
  824. s->last_picture= s->next_picture;
  825. s->next_picture= s->current_picture;
  826. }
  827. if(s->pict_type != I_TYPE && s->last_picture.data[0]==NULL){
  828. fprintf(stderr, "warning: first frame is no keyframe\n");
  829. assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
  830. goto alloc;
  831. }
  832. s->hurry_up= s->avctx->hurry_up;
  833. s->error_resilience= avctx->error_resilience;
  834. /* set dequantizer, we cant do it during init as it might change for mpeg4
  835. and we cant do it in the header decode as init isnt called for mpeg4 there yet */
  836. if(s->out_format == FMT_H263){
  837. if(s->mpeg_quant)
  838. s->dct_unquantize = s->dct_unquantize_mpeg2;
  839. else
  840. s->dct_unquantize = s->dct_unquantize_h263;
  841. }else
  842. s->dct_unquantize = s->dct_unquantize_mpeg1;
  843. return 0;
  844. }
  845. /* generic function for encode/decode called after a frame has been coded/decoded */
  846. void MPV_frame_end(MpegEncContext *s)
  847. {
  848. int i;
  849. /* draw edge for correct motion prediction if outside */
  850. if(s->codec_id!=CODEC_ID_SVQ1){
  851. if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  852. draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  853. draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  854. draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  855. }
  856. }
  857. emms_c();
  858. s->last_pict_type = s->pict_type;
  859. if(s->pict_type!=B_TYPE){
  860. s->last_non_b_pict_type= s->pict_type;
  861. }
  862. s->current_picture.quality= s->qscale; //FIXME get average of qscale_table
  863. s->current_picture.pict_type= s->pict_type;
  864. s->current_picture.key_frame= s->pict_type == I_TYPE;
  865. /* copy back current_picture variables */
  866. for(i=0; i<MAX_PICTURE_COUNT; i++){
  867. if(s->picture[i].data[0] == s->current_picture.data[0]){
  868. s->picture[i]= s->current_picture;
  869. break;
  870. }
  871. }
  872. assert(i<MAX_PICTURE_COUNT);
  873. /* release non refernce frames */
  874. for(i=0; i<MAX_PICTURE_COUNT; i++){
  875. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/)
  876. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  877. }
  878. if(s->avctx->debug&FF_DEBUG_SKIP){
  879. int x,y;
  880. for(y=0; y<s->mb_height; y++){
  881. for(x=0; x<s->mb_width; x++){
  882. int count= s->mbskip_table[x + y*s->mb_width];
  883. if(count>9) count=9;
  884. printf(" %1d", count);
  885. }
  886. printf("\n");
  887. }
  888. printf("pict type: %d\n", s->pict_type);
  889. }
  890. }
  891. #ifdef CONFIG_ENCODERS
  892. static int get_sae(uint8_t *src, int ref, int stride){
  893. int x,y;
  894. int acc=0;
  895. for(y=0; y<16; y++){
  896. for(x=0; x<16; x++){
  897. acc+= ABS(src[x+y*stride] - ref);
  898. }
  899. }
  900. return acc;
  901. }
  902. static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
  903. int x, y, w, h;
  904. int acc=0;
  905. w= s->width &~15;
  906. h= s->height&~15;
  907. for(y=0; y<h; y+=16){
  908. for(x=0; x<w; x+=16){
  909. int offset= x + y*stride;
  910. int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
  911. int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
  912. int sae = get_sae(src + offset, mean, stride);
  913. acc+= sae + 500 < sad;
  914. }
  915. }
  916. return acc;
  917. }
  918. static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
  919. AVFrame *pic;
  920. int i;
  921. const int encoding_delay= s->max_b_frames;
  922. int direct=1;
  923. if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
  924. if(pic_arg->linesize[0] != s->linesize) direct=0;
  925. if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
  926. if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
  927. // printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
  928. if(direct){
  929. i= find_unused_picture(s, 1);
  930. pic= (AVFrame*)&s->picture[i];
  931. pic->reference= 1;
  932. for(i=0; i<4; i++){
  933. pic->data[i]= pic_arg->data[i];
  934. pic->linesize[i]= pic_arg->linesize[i];
  935. }
  936. alloc_picture(s, (Picture*)pic, 1);
  937. }else{
  938. i= find_unused_picture(s, 0);
  939. pic= (AVFrame*)&s->picture[i];
  940. pic->reference= 1;
  941. alloc_picture(s, (Picture*)pic, 0);
  942. if( pic->data[0] == pic_arg->data[0]
  943. && pic->data[1] == pic_arg->data[1]
  944. && pic->data[2] == pic_arg->data[2]){
  945. // empty
  946. }else{
  947. int h_chroma_shift, v_chroma_shift;
  948. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  949. for(i=0; i<3; i++){
  950. int src_stride= pic_arg->linesize[i];
  951. int dst_stride= i ? s->uvlinesize : s->linesize;
  952. int h_shift= i ? h_chroma_shift : 0;
  953. int v_shift= i ? v_chroma_shift : 0;
  954. int w= s->width >>h_shift;
  955. int h= s->height>>v_shift;
  956. uint8_t *src= pic_arg->data[i];
  957. uint8_t *dst= pic->data[i];
  958. if(src_stride==dst_stride)
  959. memcpy(dst, src, src_stride*h);
  960. else{
  961. while(h--){
  962. memcpy(dst, src, w);
  963. dst += dst_stride;
  964. src += src_stride;
  965. }
  966. }
  967. }
  968. }
  969. }
  970. pic->quality= pic_arg->quality;
  971. pic->pict_type= pic_arg->pict_type;
  972. pic->pts = pic_arg->pts;
  973. if(s->input_picture[encoding_delay])
  974. pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
  975. /* shift buffer entries */
  976. for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
  977. s->input_picture[i-1]= s->input_picture[i];
  978. s->input_picture[encoding_delay]= (Picture*)pic;
  979. return 0;
  980. }
  981. static void select_input_picture(MpegEncContext *s){
  982. int i;
  983. const int encoding_delay= s->max_b_frames;
  984. int coded_pic_num=0;
  985. if(s->reordered_input_picture[0])
  986. coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
  987. for(i=1; i<MAX_PICTURE_COUNT; i++)
  988. s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
  989. s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
  990. /* set next picture types & ordering */
  991. if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
  992. if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){
  993. s->reordered_input_picture[0]= s->input_picture[0];
  994. s->reordered_input_picture[0]->pict_type= I_TYPE;
  995. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  996. }else{
  997. int b_frames;
  998. if(s->flags&CODEC_FLAG_PASS2){
  999. for(i=0; i<s->max_b_frames+1; i++){
  1000. int pict_num= s->input_picture[0]->display_picture_number + i;
  1001. int pict_type= s->rc_context.entry[pict_num].new_pict_type;
  1002. s->input_picture[i]->pict_type= pict_type;
  1003. if(i + 1 >= s->rc_context.num_entries) break;
  1004. }
  1005. }
  1006. if(s->input_picture[0]->pict_type){
  1007. /* user selected pict_type */
  1008. for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
  1009. if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
  1010. }
  1011. if(b_frames > s->max_b_frames){
  1012. fprintf(stderr, "warning, too many bframes in a row\n");
  1013. b_frames = s->max_b_frames;
  1014. }
  1015. }else if(s->b_frame_strategy==0){
  1016. b_frames= s->max_b_frames;
  1017. }else if(s->b_frame_strategy==1){
  1018. for(i=1; i<s->max_b_frames+1; i++){
  1019. if(s->input_picture[i]->b_frame_score==0){
  1020. s->input_picture[i]->b_frame_score=
  1021. get_intra_count(s, s->input_picture[i ]->data[0],
  1022. s->input_picture[i-1]->data[0], s->linesize) + 1;
  1023. }
  1024. }
  1025. for(i=0; i<s->max_b_frames; i++){
  1026. if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
  1027. }
  1028. b_frames= FFMAX(0, i-1);
  1029. /* reset scores */
  1030. for(i=0; i<b_frames+1; i++){
  1031. s->input_picture[i]->b_frame_score=0;
  1032. }
  1033. }else{
  1034. fprintf(stderr, "illegal b frame strategy\n");
  1035. b_frames=0;
  1036. }
  1037. emms_c();
  1038. //static int b_count=0;
  1039. //b_count+= b_frames;
  1040. //printf("b_frames: %d\n", b_count);
  1041. s->reordered_input_picture[0]= s->input_picture[b_frames];
  1042. if( s->picture_in_gop_number + b_frames >= s->gop_size
  1043. || s->reordered_input_picture[0]->pict_type== I_TYPE)
  1044. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1045. else
  1046. s->reordered_input_picture[0]->pict_type= P_TYPE;
  1047. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  1048. for(i=0; i<b_frames; i++){
  1049. coded_pic_num++;
  1050. s->reordered_input_picture[i+1]= s->input_picture[i];
  1051. s->reordered_input_picture[i+1]->pict_type= B_TYPE;
  1052. s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
  1053. }
  1054. }
  1055. }
  1056. if(s->reordered_input_picture[0]){
  1057. s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE;
  1058. if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
  1059. int i= find_unused_picture(s, 0);
  1060. Picture *pic= &s->picture[i];
  1061. s->new_picture= *s->reordered_input_picture[0];
  1062. /* mark us unused / free shared pic */
  1063. for(i=0; i<4; i++)
  1064. s->reordered_input_picture[0]->data[i]= NULL;
  1065. s->reordered_input_picture[0]->type= 0;
  1066. pic->pict_type = s->reordered_input_picture[0]->pict_type;
  1067. pic->quality = s->reordered_input_picture[0]->quality;
  1068. pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
  1069. pic->reference = s->reordered_input_picture[0]->reference;
  1070. alloc_picture(s, pic, 0);
  1071. s->current_picture= *pic;
  1072. }else{
  1073. assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
  1074. || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  1075. s->new_picture= *s->reordered_input_picture[0];
  1076. for(i=0; i<4; i++){
  1077. s->reordered_input_picture[0]->data[i]-=16; //FIXME dirty
  1078. }
  1079. s->current_picture= *s->reordered_input_picture[0];
  1080. }
  1081. s->picture_number= s->new_picture.display_picture_number;
  1082. //printf("dpn:%d\n", s->picture_number);
  1083. }else{
  1084. memset(&s->new_picture, 0, sizeof(Picture));
  1085. }
  1086. }
  1087. int MPV_encode_picture(AVCodecContext *avctx,
  1088. unsigned char *buf, int buf_size, void *data)
  1089. {
  1090. MpegEncContext *s = avctx->priv_data;
  1091. AVFrame *pic_arg = data;
  1092. int i;
  1093. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  1094. s->picture_in_gop_number++;
  1095. load_input_picture(s, pic_arg);
  1096. select_input_picture(s);
  1097. /* output? */
  1098. if(s->new_picture.data[0]){
  1099. s->pict_type= s->new_picture.pict_type;
  1100. if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
  1101. s->qscale= (int)(s->new_picture.quality+0.5);
  1102. assert(s->qscale);
  1103. }
  1104. //emms_c();
  1105. //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
  1106. MPV_frame_start(s, avctx);
  1107. encode_picture(s, s->picture_number);
  1108. avctx->real_pict_num = s->picture_number;
  1109. avctx->header_bits = s->header_bits;
  1110. avctx->mv_bits = s->mv_bits;
  1111. avctx->misc_bits = s->misc_bits;
  1112. avctx->i_tex_bits = s->i_tex_bits;
  1113. avctx->p_tex_bits = s->p_tex_bits;
  1114. avctx->i_count = s->i_count;
  1115. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  1116. avctx->skip_count = s->skip_count;
  1117. MPV_frame_end(s);
  1118. if (s->out_format == FMT_MJPEG)
  1119. mjpeg_picture_trailer(s);
  1120. if(s->flags&CODEC_FLAG_PASS1)
  1121. ff_write_pass1_stats(s);
  1122. }
  1123. s->input_picture_number++;
  1124. flush_put_bits(&s->pb);
  1125. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  1126. s->total_bits += s->frame_bits;
  1127. avctx->frame_bits = s->frame_bits;
  1128. for(i=0; i<4; i++){
  1129. avctx->error[i] += s->current_picture.error[i];
  1130. }
  1131. return pbBufPtr(&s->pb) - s->pb.buf;
  1132. }
  1133. #endif //CONFIG_ENCODERS
  1134. static inline void gmc1_motion(MpegEncContext *s,
  1135. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1136. int dest_offset,
  1137. uint8_t **ref_picture, int src_offset)
  1138. {
  1139. uint8_t *ptr;
  1140. int offset, src_x, src_y, linesize, uvlinesize;
  1141. int motion_x, motion_y;
  1142. int emu=0;
  1143. motion_x= s->sprite_offset[0][0];
  1144. motion_y= s->sprite_offset[0][1];
  1145. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  1146. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  1147. motion_x<<=(3-s->sprite_warping_accuracy);
  1148. motion_y<<=(3-s->sprite_warping_accuracy);
  1149. src_x = clip(src_x, -16, s->width);
  1150. if (src_x == s->width)
  1151. motion_x =0;
  1152. src_y = clip(src_y, -16, s->height);
  1153. if (src_y == s->height)
  1154. motion_y =0;
  1155. linesize = s->linesize;
  1156. uvlinesize = s->uvlinesize;
  1157. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1158. dest_y+=dest_offset;
  1159. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1160. if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
  1161. || src_y + 17 >= s->v_edge_pos){
  1162. ff_emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1163. ptr= s->edge_emu_buffer;
  1164. }
  1165. }
  1166. if((motion_x|motion_y)&7){
  1167. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1168. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1169. }else{
  1170. int dxy;
  1171. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  1172. if (s->no_rounding){
  1173. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  1174. }else{
  1175. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  1176. }
  1177. }
  1178. if(s->flags&CODEC_FLAG_GRAY) return;
  1179. motion_x= s->sprite_offset[1][0];
  1180. motion_y= s->sprite_offset[1][1];
  1181. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  1182. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  1183. motion_x<<=(3-s->sprite_warping_accuracy);
  1184. motion_y<<=(3-s->sprite_warping_accuracy);
  1185. src_x = clip(src_x, -8, s->width>>1);
  1186. if (src_x == s->width>>1)
  1187. motion_x =0;
  1188. src_y = clip(src_y, -8, s->height>>1);
  1189. if (src_y == s->height>>1)
  1190. motion_y =0;
  1191. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  1192. ptr = ref_picture[1] + offset;
  1193. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1194. if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
  1195. || src_y + 9 >= s->v_edge_pos>>1){
  1196. ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1197. ptr= s->edge_emu_buffer;
  1198. emu=1;
  1199. }
  1200. }
  1201. s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1202. ptr = ref_picture[2] + offset;
  1203. if(emu){
  1204. ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1205. ptr= s->edge_emu_buffer;
  1206. }
  1207. s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1208. return;
  1209. }
  1210. static inline void gmc_motion(MpegEncContext *s,
  1211. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1212. int dest_offset,
  1213. uint8_t **ref_picture, int src_offset)
  1214. {
  1215. uint8_t *ptr;
  1216. int linesize, uvlinesize;
  1217. const int a= s->sprite_warping_accuracy;
  1218. int ox, oy;
  1219. linesize = s->linesize;
  1220. uvlinesize = s->uvlinesize;
  1221. ptr = ref_picture[0] + src_offset;
  1222. dest_y+=dest_offset;
  1223. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  1224. oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
  1225. s->dsp.gmc(dest_y, ptr, linesize, 16,
  1226. ox,
  1227. oy,
  1228. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1229. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1230. a+1, (1<<(2*a+1)) - s->no_rounding,
  1231. s->h_edge_pos, s->v_edge_pos);
  1232. s->dsp.gmc(dest_y+8, ptr, linesize, 16,
  1233. ox + s->sprite_delta[0][0]*8,
  1234. oy + s->sprite_delta[1][0]*8,
  1235. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1236. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1237. a+1, (1<<(2*a+1)) - s->no_rounding,
  1238. s->h_edge_pos, s->v_edge_pos);
  1239. if(s->flags&CODEC_FLAG_GRAY) return;
  1240. dest_cb+=dest_offset>>1;
  1241. dest_cr+=dest_offset>>1;
  1242. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  1243. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  1244. ptr = ref_picture[1] + (src_offset>>1);
  1245. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  1246. ox,
  1247. oy,
  1248. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1249. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1250. a+1, (1<<(2*a+1)) - s->no_rounding,
  1251. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1252. ptr = ref_picture[2] + (src_offset>>1);
  1253. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  1254. ox,
  1255. oy,
  1256. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1257. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1258. a+1, (1<<(2*a+1)) - s->no_rounding,
  1259. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1260. }
  1261. void ff_emulated_edge_mc(MpegEncContext *s, uint8_t *src, int linesize, int block_w, int block_h,
  1262. int src_x, int src_y, int w, int h){
  1263. int x, y;
  1264. int start_y, start_x, end_y, end_x;
  1265. uint8_t *buf= s->edge_emu_buffer;
  1266. if(src_y>= h){
  1267. src+= (h-1-src_y)*linesize;
  1268. src_y=h-1;
  1269. }else if(src_y<=-block_h){
  1270. src+= (1-block_h-src_y)*linesize;
  1271. src_y=1-block_h;
  1272. }
  1273. if(src_x>= w){
  1274. src+= (w-1-src_x);
  1275. src_x=w-1;
  1276. }else if(src_x<=-block_w){
  1277. src+= (1-block_w-src_x);
  1278. src_x=1-block_w;
  1279. }
  1280. start_y= FFMAX(0, -src_y);
  1281. start_x= FFMAX(0, -src_x);
  1282. end_y= FFMIN(block_h, h-src_y);
  1283. end_x= FFMIN(block_w, w-src_x);
  1284. // copy existing part
  1285. for(y=start_y; y<end_y; y++){
  1286. for(x=start_x; x<end_x; x++){
  1287. buf[x + y*linesize]= src[x + y*linesize];
  1288. }
  1289. }
  1290. //top
  1291. for(y=0; y<start_y; y++){
  1292. for(x=start_x; x<end_x; x++){
  1293. buf[x + y*linesize]= buf[x + start_y*linesize];
  1294. }
  1295. }
  1296. //bottom
  1297. for(y=end_y; y<block_h; y++){
  1298. for(x=start_x; x<end_x; x++){
  1299. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  1300. }
  1301. }
  1302. for(y=0; y<block_h; y++){
  1303. //left
  1304. for(x=0; x<start_x; x++){
  1305. buf[x + y*linesize]= buf[start_x + y*linesize];
  1306. }
  1307. //right
  1308. for(x=end_x; x<block_w; x++){
  1309. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  1310. }
  1311. }
  1312. }
  1313. /* apply one mpeg motion vector to the three components */
  1314. static inline void mpeg_motion(MpegEncContext *s,
  1315. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1316. int dest_offset,
  1317. uint8_t **ref_picture, int src_offset,
  1318. int field_based, op_pixels_func (*pix_op)[4],
  1319. int motion_x, int motion_y, int h)
  1320. {
  1321. uint8_t *ptr;
  1322. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1323. int emu=0;
  1324. #if 0
  1325. if(s->quarter_sample)
  1326. {
  1327. motion_x>>=1;
  1328. motion_y>>=1;
  1329. }
  1330. #endif
  1331. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1332. src_x = s->mb_x * 16 + (motion_x >> 1);
  1333. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  1334. /* WARNING: do no forget half pels */
  1335. height = s->height >> field_based;
  1336. v_edge_pos = s->v_edge_pos >> field_based;
  1337. src_x = clip(src_x, -16, s->width);
  1338. if (src_x == s->width)
  1339. dxy &= ~1;
  1340. src_y = clip(src_y, -16, height);
  1341. if (src_y == height)
  1342. dxy &= ~2;
  1343. linesize = s->linesize << field_based;
  1344. uvlinesize = s->uvlinesize << field_based;
  1345. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  1346. dest_y += dest_offset;
  1347. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1348. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
  1349. || src_y + (motion_y&1) + h > v_edge_pos){
  1350. ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
  1351. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1352. ptr= s->edge_emu_buffer + src_offset;
  1353. emu=1;
  1354. }
  1355. }
  1356. pix_op[0][dxy](dest_y, ptr, linesize, h);
  1357. if(s->flags&CODEC_FLAG_GRAY) return;
  1358. if (s->out_format == FMT_H263) {
  1359. dxy = 0;
  1360. if ((motion_x & 3) != 0)
  1361. dxy |= 1;
  1362. if ((motion_y & 3) != 0)
  1363. dxy |= 2;
  1364. mx = motion_x >> 2;
  1365. my = motion_y >> 2;
  1366. } else {
  1367. mx = motion_x / 2;
  1368. my = motion_y / 2;
  1369. dxy = ((my & 1) << 1) | (mx & 1);
  1370. mx >>= 1;
  1371. my >>= 1;
  1372. }
  1373. src_x = s->mb_x * 8 + mx;
  1374. src_y = s->mb_y * (8 >> field_based) + my;
  1375. src_x = clip(src_x, -8, s->width >> 1);
  1376. if (src_x == (s->width >> 1))
  1377. dxy &= ~1;
  1378. src_y = clip(src_y, -8, height >> 1);
  1379. if (src_y == (height >> 1))
  1380. dxy &= ~2;
  1381. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1382. ptr = ref_picture[1] + offset;
  1383. if(emu){
  1384. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1385. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1386. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1387. }
  1388. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1389. ptr = ref_picture[2] + offset;
  1390. if(emu){
  1391. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1392. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1393. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1394. }
  1395. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1396. }
  1397. static inline void qpel_motion(MpegEncContext *s,
  1398. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1399. int dest_offset,
  1400. uint8_t **ref_picture, int src_offset,
  1401. int field_based, op_pixels_func (*pix_op)[4],
  1402. qpel_mc_func (*qpix_op)[16],
  1403. int motion_x, int motion_y, int h)
  1404. {
  1405. uint8_t *ptr;
  1406. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1407. int emu=0;
  1408. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1409. src_x = s->mb_x * 16 + (motion_x >> 2);
  1410. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  1411. height = s->height >> field_based;
  1412. v_edge_pos = s->v_edge_pos >> field_based;
  1413. src_x = clip(src_x, -16, s->width);
  1414. if (src_x == s->width)
  1415. dxy &= ~3;
  1416. src_y = clip(src_y, -16, height);
  1417. if (src_y == height)
  1418. dxy &= ~12;
  1419. linesize = s->linesize << field_based;
  1420. uvlinesize = s->uvlinesize << field_based;
  1421. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1422. dest_y += dest_offset;
  1423. //printf("%d %d %d\n", src_x, src_y, dxy);
  1424. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1425. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
  1426. || src_y + (motion_y&3) + h > v_edge_pos){
  1427. ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
  1428. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1429. ptr= s->edge_emu_buffer + src_offset;
  1430. emu=1;
  1431. }
  1432. }
  1433. if(!field_based)
  1434. qpix_op[0][dxy](dest_y, ptr, linesize);
  1435. else{
  1436. //damn interlaced mode
  1437. //FIXME boundary mirroring is not exactly correct here
  1438. qpix_op[1][dxy](dest_y , ptr , linesize);
  1439. qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
  1440. }
  1441. if(s->flags&CODEC_FLAG_GRAY) return;
  1442. if(field_based){
  1443. mx= motion_x/2;
  1444. my= motion_y>>1;
  1445. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  1446. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  1447. mx= (motion_x>>1) + rtab[motion_x&7];
  1448. my= (motion_y>>1) + rtab[motion_y&7];
  1449. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  1450. mx= (motion_x>>1)|(motion_x&1);
  1451. my= (motion_y>>1)|(motion_y&1);
  1452. }else{
  1453. mx= motion_x/2;
  1454. my= motion_y/2;
  1455. }
  1456. mx= (mx>>1)|(mx&1);
  1457. my= (my>>1)|(my&1);
  1458. dxy= (mx&1) | ((my&1)<<1);
  1459. mx>>=1;
  1460. my>>=1;
  1461. src_x = s->mb_x * 8 + mx;
  1462. src_y = s->mb_y * (8 >> field_based) + my;
  1463. src_x = clip(src_x, -8, s->width >> 1);
  1464. if (src_x == (s->width >> 1))
  1465. dxy &= ~1;
  1466. src_y = clip(src_y, -8, height >> 1);
  1467. if (src_y == (height >> 1))
  1468. dxy &= ~2;
  1469. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1470. ptr = ref_picture[1] + offset;
  1471. if(emu){
  1472. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1473. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1474. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1475. }
  1476. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1477. ptr = ref_picture[2] + offset;
  1478. if(emu){
  1479. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1480. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1481. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1482. }
  1483. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1484. }
  1485. inline int ff_h263_round_chroma(int x){
  1486. if (x >= 0)
  1487. return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  1488. else {
  1489. x = -x;
  1490. return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  1491. }
  1492. }
  1493. static inline void MPV_motion(MpegEncContext *s,
  1494. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1495. int dir, uint8_t **ref_picture,
  1496. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  1497. {
  1498. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  1499. int mb_x, mb_y, i;
  1500. uint8_t *ptr, *dest;
  1501. int emu=0;
  1502. mb_x = s->mb_x;
  1503. mb_y = s->mb_y;
  1504. switch(s->mv_type) {
  1505. case MV_TYPE_16X16:
  1506. #ifdef CONFIG_RISKY
  1507. if(s->mcsel){
  1508. if(s->real_sprite_warping_points==1){
  1509. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  1510. ref_picture, 0);
  1511. }else{
  1512. gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
  1513. ref_picture, 0);
  1514. }
  1515. }else if(s->quarter_sample){
  1516. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1517. ref_picture, 0,
  1518. 0, pix_op, qpix_op,
  1519. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1520. }else if(s->mspel){
  1521. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  1522. ref_picture, pix_op,
  1523. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1524. }else
  1525. #endif
  1526. {
  1527. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1528. ref_picture, 0,
  1529. 0, pix_op,
  1530. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1531. }
  1532. break;
  1533. case MV_TYPE_8X8:
  1534. mx = 0;
  1535. my = 0;
  1536. if(s->quarter_sample){
  1537. for(i=0;i<4;i++) {
  1538. motion_x = s->mv[dir][i][0];
  1539. motion_y = s->mv[dir][i][1];
  1540. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1541. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  1542. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  1543. /* WARNING: do no forget half pels */
  1544. src_x = clip(src_x, -16, s->width);
  1545. if (src_x == s->width)
  1546. dxy &= ~3;
  1547. src_y = clip(src_y, -16, s->height);
  1548. if (src_y == s->height)
  1549. dxy &= ~12;
  1550. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1551. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1552. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
  1553. || src_y + (motion_y&3) + 8 > s->v_edge_pos){
  1554. ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1555. ptr= s->edge_emu_buffer;
  1556. }
  1557. }
  1558. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1559. qpix_op[1][dxy](dest, ptr, s->linesize);
  1560. mx += s->mv[dir][i][0]/2;
  1561. my += s->mv[dir][i][1]/2;
  1562. }
  1563. }else{
  1564. for(i=0;i<4;i++) {
  1565. motion_x = s->mv[dir][i][0];
  1566. motion_y = s->mv[dir][i][1];
  1567. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1568. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  1569. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  1570. /* WARNING: do no forget half pels */
  1571. src_x = clip(src_x, -16, s->width);
  1572. if (src_x == s->width)
  1573. dxy &= ~1;
  1574. src_y = clip(src_y, -16, s->height);
  1575. if (src_y == s->height)
  1576. dxy &= ~2;
  1577. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1578. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1579. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
  1580. || src_y + (motion_y&1) + 8 > s->v_edge_pos){
  1581. ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1582. ptr= s->edge_emu_buffer;
  1583. }
  1584. }
  1585. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1586. pix_op[1][dxy](dest, ptr, s->linesize, 8);
  1587. mx += s->mv[dir][i][0];
  1588. my += s->mv[dir][i][1];
  1589. }
  1590. }
  1591. if(s->flags&CODEC_FLAG_GRAY) break;
  1592. /* In case of 8X8, we construct a single chroma motion vector
  1593. with a special rounding */
  1594. mx= ff_h263_round_chroma(mx);
  1595. my= ff_h263_round_chroma(my);
  1596. dxy = ((my & 1) << 1) | (mx & 1);
  1597. mx >>= 1;
  1598. my >>= 1;
  1599. src_x = mb_x * 8 + mx;
  1600. src_y = mb_y * 8 + my;
  1601. src_x = clip(src_x, -8, s->width/2);
  1602. if (src_x == s->width/2)
  1603. dxy &= ~1;
  1604. src_y = clip(src_y, -8, s->height/2);
  1605. if (src_y == s->height/2)
  1606. dxy &= ~2;
  1607. offset = (src_y * (s->uvlinesize)) + src_x;
  1608. ptr = ref_picture[1] + offset;
  1609. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1610. if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
  1611. || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
  1612. ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1613. ptr= s->edge_emu_buffer;
  1614. emu=1;
  1615. }
  1616. }
  1617. pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
  1618. ptr = ref_picture[2] + offset;
  1619. if(emu){
  1620. ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1621. ptr= s->edge_emu_buffer;
  1622. }
  1623. pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
  1624. break;
  1625. case MV_TYPE_FIELD:
  1626. if (s->picture_structure == PICT_FRAME) {
  1627. if(s->quarter_sample){
  1628. /* top field */
  1629. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1630. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1631. 1, pix_op, qpix_op,
  1632. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1633. /* bottom field */
  1634. qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1635. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1636. 1, pix_op, qpix_op,
  1637. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1638. }else{
  1639. /* top field */
  1640. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1641. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1642. 1, pix_op,
  1643. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1644. /* bottom field */
  1645. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1646. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1647. 1, pix_op,
  1648. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1649. }
  1650. } else {
  1651. }
  1652. break;
  1653. }
  1654. }
  1655. /* put block[] to dest[] */
  1656. static inline void put_dct(MpegEncContext *s,
  1657. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1658. {
  1659. s->dct_unquantize(s, block, i, s->qscale);
  1660. s->idct_put (dest, line_size, block);
  1661. }
  1662. /* add block[] to dest[] */
  1663. static inline void add_dct(MpegEncContext *s,
  1664. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1665. {
  1666. if (s->block_last_index[i] >= 0) {
  1667. s->idct_add (dest, line_size, block);
  1668. }
  1669. }
  1670. static inline void add_dequant_dct(MpegEncContext *s,
  1671. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1672. {
  1673. if (s->block_last_index[i] >= 0) {
  1674. s->dct_unquantize(s, block, i, s->qscale);
  1675. s->idct_add (dest, line_size, block);
  1676. }
  1677. }
  1678. /**
  1679. * cleans dc, ac, coded_block for the current non intra MB
  1680. */
  1681. void ff_clean_intra_table_entries(MpegEncContext *s)
  1682. {
  1683. int wrap = s->block_wrap[0];
  1684. int xy = s->block_index[0];
  1685. s->dc_val[0][xy ] =
  1686. s->dc_val[0][xy + 1 ] =
  1687. s->dc_val[0][xy + wrap] =
  1688. s->dc_val[0][xy + 1 + wrap] = 1024;
  1689. /* ac pred */
  1690. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1691. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1692. if (s->msmpeg4_version>=3) {
  1693. s->coded_block[xy ] =
  1694. s->coded_block[xy + 1 ] =
  1695. s->coded_block[xy + wrap] =
  1696. s->coded_block[xy + 1 + wrap] = 0;
  1697. }
  1698. /* chroma */
  1699. wrap = s->block_wrap[4];
  1700. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  1701. s->dc_val[1][xy] =
  1702. s->dc_val[2][xy] = 1024;
  1703. /* ac pred */
  1704. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1705. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1706. s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
  1707. }
  1708. /* generic function called after a macroblock has been parsed by the
  1709. decoder or after it has been encoded by the encoder.
  1710. Important variables used:
  1711. s->mb_intra : true if intra macroblock
  1712. s->mv_dir : motion vector direction
  1713. s->mv_type : motion vector type
  1714. s->mv : motion vector
  1715. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1716. */
  1717. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  1718. {
  1719. int mb_x, mb_y;
  1720. const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
  1721. mb_x = s->mb_x;
  1722. mb_y = s->mb_y;
  1723. s->current_picture.qscale_table[mb_xy]= s->qscale;
  1724. /* update DC predictors for P macroblocks */
  1725. if (!s->mb_intra) {
  1726. if (s->h263_pred || s->h263_aic) {
  1727. if(s->mbintra_table[mb_xy])
  1728. ff_clean_intra_table_entries(s);
  1729. } else {
  1730. s->last_dc[0] =
  1731. s->last_dc[1] =
  1732. s->last_dc[2] = 128 << s->intra_dc_precision;
  1733. }
  1734. }
  1735. else if (s->h263_pred || s->h263_aic)
  1736. s->mbintra_table[mb_xy]=1;
  1737. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1738. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1739. //FIXME a lot of thet is only needed for !low_delay
  1740. const int wrap = s->block_wrap[0];
  1741. const int xy = s->block_index[0];
  1742. const int mb_index= s->mb_x + s->mb_y*s->mb_width;
  1743. if(s->mv_type == MV_TYPE_8X8){
  1744. s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
  1745. } else {
  1746. int motion_x, motion_y;
  1747. if (s->mb_intra) {
  1748. motion_x = 0;
  1749. motion_y = 0;
  1750. if(s->co_located_type_table)
  1751. s->co_located_type_table[mb_index]= 0;
  1752. } else if (s->mv_type == MV_TYPE_16X16) {
  1753. motion_x = s->mv[0][0][0];
  1754. motion_y = s->mv[0][0][1];
  1755. if(s->co_located_type_table)
  1756. s->co_located_type_table[mb_index]= 0;
  1757. } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
  1758. int i;
  1759. motion_x = s->mv[0][0][0] + s->mv[0][1][0];
  1760. motion_y = s->mv[0][0][1] + s->mv[0][1][1];
  1761. motion_x = (motion_x>>1) | (motion_x&1);
  1762. for(i=0; i<2; i++){
  1763. s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
  1764. s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
  1765. s->field_select_table[mb_index][i]= s->field_select[0][i];
  1766. }
  1767. s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
  1768. }
  1769. /* no update if 8X8 because it has been done during parsing */
  1770. s->motion_val[xy][0] = motion_x;
  1771. s->motion_val[xy][1] = motion_y;
  1772. s->motion_val[xy + 1][0] = motion_x;
  1773. s->motion_val[xy + 1][1] = motion_y;
  1774. s->motion_val[xy + wrap][0] = motion_x;
  1775. s->motion_val[xy + wrap][1] = motion_y;
  1776. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1777. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1778. }
  1779. }
  1780. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
  1781. uint8_t *dest_y, *dest_cb, *dest_cr;
  1782. int dct_linesize, dct_offset;
  1783. op_pixels_func (*op_pix)[4];
  1784. qpel_mc_func (*op_qpix)[16];
  1785. /* avoid copy if macroblock skipped in last frame too */
  1786. if (s->pict_type != B_TYPE) {
  1787. s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
  1788. }
  1789. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1790. if(!s->encoding){
  1791. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1792. const int age= s->current_picture.age;
  1793. assert(age);
  1794. if (s->mb_skiped) {
  1795. s->mb_skiped= 0;
  1796. assert(s->pict_type!=I_TYPE);
  1797. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  1798. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1799. /* if previous was skipped too, then nothing to do ! */
  1800. if (*mbskip_ptr >= age && s->current_picture.reference){
  1801. return;
  1802. }
  1803. } else if(!s->current_picture.reference){
  1804. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1805. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1806. } else{
  1807. *mbskip_ptr = 0; /* not skipped */
  1808. }
  1809. }else
  1810. s->mb_skiped= 0;
  1811. if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band){
  1812. dest_y = s->current_picture.data[0] + mb_x * 16;
  1813. dest_cb = s->current_picture.data[1] + mb_x * 8;
  1814. dest_cr = s->current_picture.data[2] + mb_x * 8;
  1815. }else{
  1816. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  1817. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1818. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1819. }
  1820. if (s->interlaced_dct) {
  1821. dct_linesize = s->linesize * 2;
  1822. dct_offset = s->linesize;
  1823. } else {
  1824. dct_linesize = s->linesize;
  1825. dct_offset = s->linesize * 8;
  1826. }
  1827. if (!s->mb_intra) {
  1828. /* motion handling */
  1829. /* decoding or more than one mb_type (MC was allready done otherwise) */
  1830. if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
  1831. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1832. op_pix = s->dsp.put_pixels_tab;
  1833. op_qpix= s->dsp.put_qpel_pixels_tab;
  1834. }else{
  1835. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1836. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  1837. }
  1838. if (s->mv_dir & MV_DIR_FORWARD) {
  1839. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1840. op_pix = s->dsp.avg_pixels_tab;
  1841. op_qpix= s->dsp.avg_qpel_pixels_tab;
  1842. }
  1843. if (s->mv_dir & MV_DIR_BACKWARD) {
  1844. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1845. }
  1846. }
  1847. /* skip dequant / idct if we are really late ;) */
  1848. if(s->hurry_up>1) return;
  1849. /* add dct residue */
  1850. if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO
  1851. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1852. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
  1853. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1854. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1855. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1856. if(!(s->flags&CODEC_FLAG_GRAY)){
  1857. add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1858. add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1859. }
  1860. } else if(s->codec_id != CODEC_ID_WMV2){
  1861. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1862. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1863. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1864. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1865. if(!(s->flags&CODEC_FLAG_GRAY)){
  1866. add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1867. add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1868. }
  1869. }
  1870. #ifdef CONFIG_RISKY
  1871. else{
  1872. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1873. }
  1874. #endif
  1875. } else {
  1876. /* dct only in intra block */
  1877. if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
  1878. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1879. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1880. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1881. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1882. if(!(s->flags&CODEC_FLAG_GRAY)){
  1883. put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1884. put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1885. }
  1886. }else{
  1887. s->idct_put(dest_y , dct_linesize, block[0]);
  1888. s->idct_put(dest_y + 8, dct_linesize, block[1]);
  1889. s->idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1890. s->idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
  1891. if(!(s->flags&CODEC_FLAG_GRAY)){
  1892. s->idct_put(dest_cb, s->uvlinesize, block[4]);
  1893. s->idct_put(dest_cr, s->uvlinesize, block[5]);
  1894. }
  1895. }
  1896. }
  1897. }
  1898. }
  1899. #ifdef CONFIG_ENCODERS
  1900. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  1901. {
  1902. static const char tab[64]=
  1903. {3,2,2,1,1,1,1,1,
  1904. 1,1,1,1,1,1,1,1,
  1905. 1,1,1,1,1,1,1,1,
  1906. 0,0,0,0,0,0,0,0,
  1907. 0,0,0,0,0,0,0,0,
  1908. 0,0,0,0,0,0,0,0,
  1909. 0,0,0,0,0,0,0,0,
  1910. 0,0,0,0,0,0,0,0};
  1911. int score=0;
  1912. int run=0;
  1913. int i;
  1914. DCTELEM *block= s->block[n];
  1915. const int last_index= s->block_last_index[n];
  1916. int skip_dc;
  1917. if(threshold<0){
  1918. skip_dc=0;
  1919. threshold= -threshold;
  1920. }else
  1921. skip_dc=1;
  1922. /* are all which we could set to zero are allready zero? */
  1923. if(last_index<=skip_dc - 1) return;
  1924. for(i=0; i<=last_index; i++){
  1925. const int j = s->intra_scantable.permutated[i];
  1926. const int level = ABS(block[j]);
  1927. if(level==1){
  1928. if(skip_dc && i==0) continue;
  1929. score+= tab[run];
  1930. run=0;
  1931. }else if(level>1){
  1932. return;
  1933. }else{
  1934. run++;
  1935. }
  1936. }
  1937. if(score >= threshold) return;
  1938. for(i=skip_dc; i<=last_index; i++){
  1939. const int j = s->intra_scantable.permutated[i];
  1940. block[j]=0;
  1941. }
  1942. if(block[0]) s->block_last_index[n]= 0;
  1943. else s->block_last_index[n]= -1;
  1944. }
  1945. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1946. {
  1947. int i;
  1948. const int maxlevel= s->max_qcoeff;
  1949. const int minlevel= s->min_qcoeff;
  1950. if(s->mb_intra){
  1951. i=1; //skip clipping of intra dc
  1952. }else
  1953. i=0;
  1954. for(;i<=last_index; i++){
  1955. const int j= s->intra_scantable.permutated[i];
  1956. int level = block[j];
  1957. if (level>maxlevel) level=maxlevel;
  1958. else if(level<minlevel) level=minlevel;
  1959. block[j]= level;
  1960. }
  1961. }
  1962. static inline void requantize_coeffs(MpegEncContext *s, DCTELEM block[64], int oldq, int newq, int n)
  1963. {
  1964. int i;
  1965. if(s->mb_intra){
  1966. i=1; //skip clipping of intra dc
  1967. //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
  1968. }else
  1969. i=0;
  1970. for(;i<=s->block_last_index[n]; i++){
  1971. const int j = s->intra_scantable.permutated[i];
  1972. int level = block[j];
  1973. block[j]= ROUNDED_DIV(level*oldq, newq);
  1974. }
  1975. for(i=s->block_last_index[n]; i>=0; i--){
  1976. const int j = s->intra_scantable.permutated[i];
  1977. if(block[j]) break;
  1978. }
  1979. s->block_last_index[n]= i;
  1980. }
  1981. static inline void auto_requantize_coeffs(MpegEncContext *s, DCTELEM block[6][64])
  1982. {
  1983. int i,n, newq;
  1984. const int maxlevel= s->max_qcoeff;
  1985. const int minlevel= s->min_qcoeff;
  1986. int largest=0, smallest=0;
  1987. assert(s->adaptive_quant);
  1988. for(n=0; n<6; n++){
  1989. if(s->mb_intra){
  1990. i=1; //skip clipping of intra dc
  1991. //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
  1992. }else
  1993. i=0;
  1994. for(;i<=s->block_last_index[n]; i++){
  1995. const int j = s->intra_scantable.permutated[i];
  1996. int level = block[n][j];
  1997. if(largest < level) largest = level;
  1998. if(smallest > level) smallest= level;
  1999. }
  2000. }
  2001. for(newq=s->qscale+1; newq<32; newq++){
  2002. if( ROUNDED_DIV(smallest*s->qscale, newq) >= minlevel
  2003. && ROUNDED_DIV(largest *s->qscale, newq) <= maxlevel)
  2004. break;
  2005. }
  2006. if(s->out_format==FMT_H263){
  2007. /* h263 like formats cannot change qscale by more than 2 easiely */
  2008. if(s->avctx->qmin + 2 < newq)
  2009. newq= s->avctx->qmin + 2;
  2010. }
  2011. for(n=0; n<6; n++){
  2012. requantize_coeffs(s, block[n], s->qscale, newq, n);
  2013. clip_coeffs(s, block[n], s->block_last_index[n]);
  2014. }
  2015. s->dquant+= newq - s->qscale;
  2016. s->qscale= newq;
  2017. }
  2018. #if 0
  2019. static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
  2020. int score=0;
  2021. int x,y;
  2022. for(y=0; y<7; y++){
  2023. for(x=0; x<16; x+=4){
  2024. score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
  2025. +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
  2026. }
  2027. s+= stride;
  2028. }
  2029. return score;
  2030. }
  2031. static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
  2032. int score=0;
  2033. int x,y;
  2034. for(y=0; y<7; y++){
  2035. for(x=0; x<16; x++){
  2036. score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2037. }
  2038. s1+= stride;
  2039. s2+= stride;
  2040. }
  2041. return score;
  2042. }
  2043. #else
  2044. #define SQ(a) ((a)*(a))
  2045. static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
  2046. int score=0;
  2047. int x,y;
  2048. for(y=0; y<7; y++){
  2049. for(x=0; x<16; x+=4){
  2050. score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
  2051. +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
  2052. }
  2053. s+= stride;
  2054. }
  2055. return score;
  2056. }
  2057. static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
  2058. int score=0;
  2059. int x,y;
  2060. for(y=0; y<7; y++){
  2061. for(x=0; x<16; x++){
  2062. score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2063. }
  2064. s1+= stride;
  2065. s2+= stride;
  2066. }
  2067. return score;
  2068. }
  2069. #endif
  2070. #endif //CONFIG_ENCODERS
  2071. void ff_draw_horiz_band(MpegEncContext *s){
  2072. if ( s->avctx->draw_horiz_band
  2073. && (s->last_picture.data[0] || s->low_delay) ) {
  2074. uint8_t *src_ptr[3];
  2075. int y, h, offset;
  2076. y = s->mb_y * 16;
  2077. h = s->height - y;
  2078. if (h > 16)
  2079. h = 16;
  2080. if(s->pict_type==B_TYPE)
  2081. offset = 0;
  2082. else
  2083. offset = y * s->linesize;
  2084. if(s->pict_type==B_TYPE || s->low_delay){
  2085. src_ptr[0] = s->current_picture.data[0] + offset;
  2086. src_ptr[1] = s->current_picture.data[1] + (offset >> 2);
  2087. src_ptr[2] = s->current_picture.data[2] + (offset >> 2);
  2088. } else {
  2089. src_ptr[0] = s->last_picture.data[0] + offset;
  2090. src_ptr[1] = s->last_picture.data[1] + (offset >> 2);
  2091. src_ptr[2] = s->last_picture.data[2] + (offset >> 2);
  2092. }
  2093. emms_c();
  2094. s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
  2095. y, s->width, h);
  2096. }
  2097. }
  2098. #ifdef CONFIG_ENCODERS
  2099. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  2100. {
  2101. const int mb_x= s->mb_x;
  2102. const int mb_y= s->mb_y;
  2103. int i;
  2104. int skip_dct[6];
  2105. int dct_offset = s->linesize*8; //default for progressive frames
  2106. for(i=0; i<6; i++) skip_dct[i]=0;
  2107. if(s->adaptive_quant){
  2108. s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
  2109. if(s->out_format==FMT_H263){
  2110. if (s->dquant> 2) s->dquant= 2;
  2111. else if(s->dquant<-2) s->dquant=-2;
  2112. }
  2113. if(s->codec_id==CODEC_ID_MPEG4){
  2114. if(!s->mb_intra){
  2115. assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
  2116. if(s->mv_dir&MV_DIRECT)
  2117. s->dquant=0;
  2118. }
  2119. }
  2120. s->qscale+= s->dquant;
  2121. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  2122. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  2123. }
  2124. if (s->mb_intra) {
  2125. uint8_t *ptr;
  2126. int wrap_y;
  2127. int emu=0;
  2128. wrap_y = s->linesize;
  2129. ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2130. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2131. ff_emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2132. ptr= s->edge_emu_buffer;
  2133. emu=1;
  2134. }
  2135. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2136. int progressive_score, interlaced_score;
  2137. progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
  2138. interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
  2139. if(progressive_score > interlaced_score + 100){
  2140. s->interlaced_dct=1;
  2141. dct_offset= wrap_y;
  2142. wrap_y<<=1;
  2143. }else
  2144. s->interlaced_dct=0;
  2145. }
  2146. s->dsp.get_pixels(s->block[0], ptr , wrap_y);
  2147. s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
  2148. s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
  2149. s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
  2150. if(s->flags&CODEC_FLAG_GRAY){
  2151. skip_dct[4]= 1;
  2152. skip_dct[5]= 1;
  2153. }else{
  2154. int wrap_c = s->uvlinesize;
  2155. ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2156. if(emu){
  2157. ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2158. ptr= s->edge_emu_buffer;
  2159. }
  2160. s->dsp.get_pixels(s->block[4], ptr, wrap_c);
  2161. ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2162. if(emu){
  2163. ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2164. ptr= s->edge_emu_buffer;
  2165. }
  2166. s->dsp.get_pixels(s->block[5], ptr, wrap_c);
  2167. }
  2168. }else{
  2169. op_pixels_func (*op_pix)[4];
  2170. qpel_mc_func (*op_qpix)[16];
  2171. uint8_t *dest_y, *dest_cb, *dest_cr;
  2172. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2173. int wrap_y, wrap_c;
  2174. int emu=0;
  2175. dest_y = s->current_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  2176. dest_cb = s->current_picture.data[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2177. dest_cr = s->current_picture.data[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2178. wrap_y = s->linesize;
  2179. wrap_c = s->uvlinesize;
  2180. ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2181. ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2182. ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2183. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2184. op_pix = s->dsp.put_pixels_tab;
  2185. op_qpix= s->dsp.put_qpel_pixels_tab;
  2186. }else{
  2187. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2188. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2189. }
  2190. if (s->mv_dir & MV_DIR_FORWARD) {
  2191. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2192. op_pix = s->dsp.avg_pixels_tab;
  2193. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2194. }
  2195. if (s->mv_dir & MV_DIR_BACKWARD) {
  2196. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2197. }
  2198. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2199. ff_emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2200. ptr_y= s->edge_emu_buffer;
  2201. emu=1;
  2202. }
  2203. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2204. int progressive_score, interlaced_score;
  2205. progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
  2206. + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
  2207. interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
  2208. + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
  2209. if(progressive_score > interlaced_score + 600){
  2210. s->interlaced_dct=1;
  2211. dct_offset= wrap_y;
  2212. wrap_y<<=1;
  2213. }else
  2214. s->interlaced_dct=0;
  2215. }
  2216. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  2217. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  2218. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  2219. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  2220. if(s->flags&CODEC_FLAG_GRAY){
  2221. skip_dct[4]= 1;
  2222. skip_dct[5]= 1;
  2223. }else{
  2224. if(emu){
  2225. ff_emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2226. ptr_cb= s->edge_emu_buffer;
  2227. }
  2228. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  2229. if(emu){
  2230. ff_emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2231. ptr_cr= s->edge_emu_buffer;
  2232. }
  2233. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  2234. }
  2235. /* pre quantization */
  2236. if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
  2237. //FIXME optimize
  2238. if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  2239. if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  2240. if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  2241. if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  2242. if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
  2243. if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
  2244. #if 0
  2245. {
  2246. static int stat[7];
  2247. int num=0;
  2248. for(i=0; i<6; i++)
  2249. if(skip_dct[i]) num++;
  2250. stat[num]++;
  2251. if(s->mb_x==0 && s->mb_y==0){
  2252. for(i=0; i<7; i++){
  2253. printf("%6d %1d\n", stat[i], i);
  2254. }
  2255. }
  2256. }
  2257. #endif
  2258. }
  2259. }
  2260. #if 0
  2261. {
  2262. float adap_parm;
  2263. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  2264. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  2265. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  2266. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  2267. s->qscale, adap_parm, s->qscale*adap_parm,
  2268. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  2269. }
  2270. #endif
  2271. /* DCT & quantize */
  2272. if(s->out_format==FMT_MJPEG){
  2273. for(i=0;i<6;i++) {
  2274. int overflow;
  2275. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
  2276. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2277. }
  2278. }else{
  2279. for(i=0;i<6;i++) {
  2280. if(!skip_dct[i]){
  2281. int overflow;
  2282. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  2283. // FIXME we could decide to change to quantizer instead of clipping
  2284. // JS: I don't think that would be a good idea it could lower quality instead
  2285. // of improve it. Just INTRADC clipping deserves changes in quantizer
  2286. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2287. }else
  2288. s->block_last_index[i]= -1;
  2289. }
  2290. if(s->luma_elim_threshold && !s->mb_intra)
  2291. for(i=0; i<4; i++)
  2292. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  2293. if(s->chroma_elim_threshold && !s->mb_intra)
  2294. for(i=4; i<6; i++)
  2295. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  2296. }
  2297. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  2298. s->block_last_index[4]=
  2299. s->block_last_index[5]= 0;
  2300. s->block[4][0]=
  2301. s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  2302. }
  2303. /* huffman encode */
  2304. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  2305. case CODEC_ID_MPEG1VIDEO:
  2306. mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
  2307. #ifdef CONFIG_RISKY
  2308. case CODEC_ID_MPEG4:
  2309. mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2310. case CODEC_ID_MSMPEG4V2:
  2311. case CODEC_ID_MSMPEG4V3:
  2312. case CODEC_ID_WMV1:
  2313. msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2314. case CODEC_ID_WMV2:
  2315. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
  2316. case CODEC_ID_H263:
  2317. case CODEC_ID_H263P:
  2318. case CODEC_ID_RV10:
  2319. h263_encode_mb(s, s->block, motion_x, motion_y); break;
  2320. #endif
  2321. case CODEC_ID_MJPEG:
  2322. mjpeg_encode_mb(s, s->block); break;
  2323. default:
  2324. assert(0);
  2325. }
  2326. }
  2327. #endif //CONFIG_ENCODERS
  2328. /**
  2329. * combines the (truncated) bitstream to a complete frame
  2330. * @returns -1 if no complete frame could be created
  2331. */
  2332. int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
  2333. ParseContext *pc= &s->parse_context;
  2334. pc->last_index= pc->index;
  2335. if(next==-1){
  2336. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  2337. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  2338. pc->index += *buf_size;
  2339. return -1;
  2340. }
  2341. if(pc->index){
  2342. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  2343. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  2344. pc->index = 0;
  2345. *buf= pc->buffer;
  2346. *buf_size= pc->last_index + next;
  2347. }
  2348. return 0;
  2349. }
  2350. #ifdef CONFIG_ENCODERS
  2351. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
  2352. {
  2353. int bytes= length>>4;
  2354. int bits= length&15;
  2355. int i;
  2356. if(length==0) return;
  2357. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  2358. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  2359. }
  2360. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2361. int i;
  2362. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2363. /* mpeg1 */
  2364. d->mb_incr= s->mb_incr;
  2365. for(i=0; i<3; i++)
  2366. d->last_dc[i]= s->last_dc[i];
  2367. /* statistics */
  2368. d->mv_bits= s->mv_bits;
  2369. d->i_tex_bits= s->i_tex_bits;
  2370. d->p_tex_bits= s->p_tex_bits;
  2371. d->i_count= s->i_count;
  2372. d->f_count= s->f_count;
  2373. d->b_count= s->b_count;
  2374. d->skip_count= s->skip_count;
  2375. d->misc_bits= s->misc_bits;
  2376. d->last_bits= 0;
  2377. d->mb_skiped= s->mb_skiped;
  2378. d->qscale= s->qscale;
  2379. }
  2380. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2381. int i;
  2382. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  2383. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2384. /* mpeg1 */
  2385. d->mb_incr= s->mb_incr;
  2386. for(i=0; i<3; i++)
  2387. d->last_dc[i]= s->last_dc[i];
  2388. /* statistics */
  2389. d->mv_bits= s->mv_bits;
  2390. d->i_tex_bits= s->i_tex_bits;
  2391. d->p_tex_bits= s->p_tex_bits;
  2392. d->i_count= s->i_count;
  2393. d->f_count= s->f_count;
  2394. d->b_count= s->b_count;
  2395. d->skip_count= s->skip_count;
  2396. d->misc_bits= s->misc_bits;
  2397. d->mb_intra= s->mb_intra;
  2398. d->mb_skiped= s->mb_skiped;
  2399. d->mv_type= s->mv_type;
  2400. d->mv_dir= s->mv_dir;
  2401. d->pb= s->pb;
  2402. if(s->data_partitioning){
  2403. d->pb2= s->pb2;
  2404. d->tex_pb= s->tex_pb;
  2405. }
  2406. d->block= s->block;
  2407. for(i=0; i<6; i++)
  2408. d->block_last_index[i]= s->block_last_index[i];
  2409. d->interlaced_dct= s->interlaced_dct;
  2410. d->qscale= s->qscale;
  2411. }
  2412. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  2413. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  2414. int *dmin, int *next_block, int motion_x, int motion_y)
  2415. {
  2416. int bits_count;
  2417. copy_context_before_encode(s, backup, type);
  2418. s->block= s->blocks[*next_block];
  2419. s->pb= pb[*next_block];
  2420. if(s->data_partitioning){
  2421. s->pb2 = pb2 [*next_block];
  2422. s->tex_pb= tex_pb[*next_block];
  2423. }
  2424. encode_mb(s, motion_x, motion_y);
  2425. bits_count= get_bit_count(&s->pb);
  2426. if(s->data_partitioning){
  2427. bits_count+= get_bit_count(&s->pb2);
  2428. bits_count+= get_bit_count(&s->tex_pb);
  2429. }
  2430. if(bits_count<*dmin){
  2431. *dmin= bits_count;
  2432. *next_block^=1;
  2433. copy_context_after_encode(best, s, type);
  2434. }
  2435. }
  2436. static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  2437. uint32_t *sq = squareTbl + 256;
  2438. int acc=0;
  2439. int x,y;
  2440. if(w==16 && h==16)
  2441. return s->dsp.sse[0](NULL, src1, src2, stride);
  2442. else if(w==8 && h==8)
  2443. return s->dsp.sse[1](NULL, src1, src2, stride);
  2444. for(y=0; y<h; y++){
  2445. for(x=0; x<w; x++){
  2446. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  2447. }
  2448. }
  2449. assert(acc>=0);
  2450. return acc;
  2451. }
  2452. static void encode_picture(MpegEncContext *s, int picture_number)
  2453. {
  2454. int mb_x, mb_y, pdif = 0;
  2455. int i;
  2456. int bits;
  2457. MpegEncContext best_s, backup_s;
  2458. uint8_t bit_buf[2][3000];
  2459. uint8_t bit_buf2[2][3000];
  2460. uint8_t bit_buf_tex[2][3000];
  2461. PutBitContext pb[2], pb2[2], tex_pb[2];
  2462. for(i=0; i<2; i++){
  2463. init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
  2464. init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
  2465. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
  2466. }
  2467. s->picture_number = picture_number;
  2468. s->block_wrap[0]=
  2469. s->block_wrap[1]=
  2470. s->block_wrap[2]=
  2471. s->block_wrap[3]= s->mb_width*2 + 2;
  2472. s->block_wrap[4]=
  2473. s->block_wrap[5]= s->mb_width + 2;
  2474. /* Reset the average MB variance */
  2475. s->current_picture.mb_var_sum = 0;
  2476. s->current_picture.mc_mb_var_sum = 0;
  2477. #ifdef CONFIG_RISKY
  2478. /* we need to initialize some time vars before we can encode b-frames */
  2479. // RAL: Condition added for MPEG1VIDEO
  2480. if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
  2481. ff_set_mpeg4_time(s, s->picture_number);
  2482. #endif
  2483. s->scene_change_score=0;
  2484. s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
  2485. if(s->msmpeg4_version){
  2486. if(s->pict_type==I_TYPE)
  2487. s->no_rounding=1;
  2488. else if(s->flipflop_rounding)
  2489. s->no_rounding ^= 1;
  2490. }else if(s->out_format == FMT_H263){
  2491. if(s->pict_type==I_TYPE)
  2492. s->no_rounding=0;
  2493. else if(s->pict_type!=B_TYPE)
  2494. s->no_rounding ^= 1;
  2495. }
  2496. /* Estimate motion for every MB */
  2497. s->mb_intra=0; //for the rate distoration & bit compare functions
  2498. if(s->pict_type != I_TYPE){
  2499. if(s->pict_type != B_TYPE){
  2500. if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
  2501. s->me.pre_pass=1;
  2502. s->me.dia_size= s->avctx->pre_dia_size;
  2503. for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
  2504. for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
  2505. s->mb_x = mb_x;
  2506. s->mb_y = mb_y;
  2507. ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
  2508. }
  2509. }
  2510. s->me.pre_pass=0;
  2511. }
  2512. }
  2513. s->me.dia_size= s->avctx->dia_size;
  2514. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2515. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  2516. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  2517. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  2518. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  2519. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2520. s->mb_x = mb_x;
  2521. s->mb_y = mb_y;
  2522. s->block_index[0]+=2;
  2523. s->block_index[1]+=2;
  2524. s->block_index[2]+=2;
  2525. s->block_index[3]+=2;
  2526. /* compute motion vector & mb_type and store in context */
  2527. if(s->pict_type==B_TYPE)
  2528. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  2529. else
  2530. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  2531. }
  2532. }
  2533. }else /* if(s->pict_type == I_TYPE) */{
  2534. /* I-Frame */
  2535. //FIXME do we need to zero them?
  2536. memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  2537. memset(s->p_mv_table , 0, sizeof(int16_t)*(s->mb_width+2)*(s->mb_height+2)*2);
  2538. memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
  2539. if(!s->fixed_qscale){
  2540. /* finding spatial complexity for I-frame rate control */
  2541. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2542. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2543. int xx = mb_x * 16;
  2544. int yy = mb_y * 16;
  2545. uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  2546. int varc;
  2547. int sum = s->dsp.pix_sum(pix, s->linesize);
  2548. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  2549. s->current_picture.mb_var [s->mb_width * mb_y + mb_x] = varc;
  2550. s->current_picture.mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8;
  2551. s->current_picture.mb_var_sum += varc;
  2552. }
  2553. }
  2554. }
  2555. }
  2556. emms_c();
  2557. if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
  2558. s->pict_type= I_TYPE;
  2559. memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
  2560. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  2561. }
  2562. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
  2563. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  2564. // RAL: Next call moved into that bloc
  2565. ff_fix_long_p_mvs(s);
  2566. }
  2567. // RAL: All this bloc changed
  2568. if(s->pict_type==B_TYPE){
  2569. int a, b;
  2570. a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  2571. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
  2572. s->f_code = FFMAX(a, b);
  2573. a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  2574. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
  2575. s->b_code = FFMAX(a, b);
  2576. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  2577. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  2578. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  2579. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  2580. }
  2581. if (s->fixed_qscale)
  2582. s->frame_qscale = s->current_picture.quality;
  2583. else
  2584. s->frame_qscale = ff_rate_estimate_qscale(s);
  2585. if(s->adaptive_quant){
  2586. #ifdef CONFIG_RISKY
  2587. switch(s->codec_id){
  2588. case CODEC_ID_MPEG4:
  2589. ff_clean_mpeg4_qscales(s);
  2590. break;
  2591. case CODEC_ID_H263:
  2592. case CODEC_ID_H263P:
  2593. ff_clean_h263_qscales(s);
  2594. break;
  2595. }
  2596. #endif
  2597. s->qscale= s->current_picture.qscale_table[0];
  2598. }else
  2599. s->qscale= (int)(s->frame_qscale + 0.5);
  2600. if (s->out_format == FMT_MJPEG) {
  2601. /* for mjpeg, we do include qscale in the matrix */
  2602. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  2603. for(i=1;i<64;i++){
  2604. int j= s->idct_permutation[i];
  2605. s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  2606. }
  2607. convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  2608. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
  2609. }
  2610. //FIXME var duplication
  2611. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2612. s->current_picture.pict_type= s->pict_type;
  2613. if(s->current_picture.key_frame)
  2614. s->picture_in_gop_number=0;
  2615. s->last_bits= get_bit_count(&s->pb);
  2616. switch(s->out_format) {
  2617. case FMT_MJPEG:
  2618. mjpeg_picture_header(s);
  2619. break;
  2620. #ifdef CONFIG_RISKY
  2621. case FMT_H263:
  2622. if (s->codec_id == CODEC_ID_WMV2)
  2623. ff_wmv2_encode_picture_header(s, picture_number);
  2624. else if (s->h263_msmpeg4)
  2625. msmpeg4_encode_picture_header(s, picture_number);
  2626. else if (s->h263_pred)
  2627. mpeg4_encode_picture_header(s, picture_number);
  2628. else if (s->h263_rv10)
  2629. rv10_encode_picture_header(s, picture_number);
  2630. else
  2631. h263_encode_picture_header(s, picture_number);
  2632. break;
  2633. #endif
  2634. case FMT_MPEG1:
  2635. mpeg1_encode_picture_header(s, picture_number);
  2636. break;
  2637. }
  2638. bits= get_bit_count(&s->pb);
  2639. s->header_bits= bits - s->last_bits;
  2640. s->last_bits= bits;
  2641. s->mv_bits=0;
  2642. s->misc_bits=0;
  2643. s->i_tex_bits=0;
  2644. s->p_tex_bits=0;
  2645. s->i_count=0;
  2646. s->f_count=0;
  2647. s->b_count=0;
  2648. s->skip_count=0;
  2649. for(i=0; i<3; i++){
  2650. /* init last dc values */
  2651. /* note: quant matrix value (8) is implied here */
  2652. s->last_dc[i] = 128;
  2653. s->current_picture.error[i] = 0;
  2654. }
  2655. s->mb_incr = 1;
  2656. s->last_mv[0][0][0] = 0;
  2657. s->last_mv[0][0][1] = 0;
  2658. s->last_mv[1][0][0] = 0;
  2659. s->last_mv[1][0][1] = 0;
  2660. s->last_mv_dir = 0;
  2661. #ifdef CONFIG_RISKY
  2662. if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
  2663. s->gob_index = ff_h263_get_gob_height(s);
  2664. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  2665. ff_mpeg4_init_partitions(s);
  2666. #endif
  2667. s->resync_mb_x=0;
  2668. s->resync_mb_y=0;
  2669. s->first_slice_line = 1;
  2670. s->ptr_lastgob = s->pb.buf;
  2671. s->ptr_last_mb_line = s->pb.buf;
  2672. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2673. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  2674. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  2675. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  2676. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  2677. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  2678. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  2679. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  2680. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  2681. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2682. int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  2683. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  2684. // int d;
  2685. int dmin=10000000;
  2686. s->mb_x = mb_x;
  2687. s->mb_y = mb_y;
  2688. s->block_index[0]+=2;
  2689. s->block_index[1]+=2;
  2690. s->block_index[2]+=2;
  2691. s->block_index[3]+=2;
  2692. s->block_index[4]++;
  2693. s->block_index[5]++;
  2694. /* write gob / video packet header */
  2695. #ifdef CONFIG_RISKY
  2696. if(s->rtp_mode){
  2697. int current_packet_size, is_gob_start;
  2698. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  2699. is_gob_start=0;
  2700. if(s->codec_id==CODEC_ID_MPEG4){
  2701. if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size
  2702. && s->mb_y + s->mb_x>0){
  2703. if(s->partitioned_frame){
  2704. ff_mpeg4_merge_partitions(s);
  2705. ff_mpeg4_init_partitions(s);
  2706. }
  2707. ff_mpeg4_encode_video_packet_header(s);
  2708. if(s->flags&CODEC_FLAG_PASS1){
  2709. int bits= get_bit_count(&s->pb);
  2710. s->misc_bits+= bits - s->last_bits;
  2711. s->last_bits= bits;
  2712. }
  2713. ff_mpeg4_clean_buffers(s);
  2714. is_gob_start=1;
  2715. }
  2716. }else{
  2717. if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size
  2718. && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
  2719. h263_encode_gob_header(s, mb_y);
  2720. is_gob_start=1;
  2721. }
  2722. }
  2723. if(is_gob_start){
  2724. s->ptr_lastgob = pbBufPtr(&s->pb);
  2725. s->first_slice_line=1;
  2726. s->resync_mb_x=mb_x;
  2727. s->resync_mb_y=mb_y;
  2728. }
  2729. }
  2730. #endif
  2731. if( (s->resync_mb_x == s->mb_x)
  2732. && s->resync_mb_y+1 == s->mb_y){
  2733. s->first_slice_line=0;
  2734. }
  2735. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  2736. int next_block=0;
  2737. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  2738. copy_context_before_encode(&backup_s, s, -1);
  2739. backup_s.pb= s->pb;
  2740. best_s.data_partitioning= s->data_partitioning;
  2741. best_s.partitioned_frame= s->partitioned_frame;
  2742. if(s->data_partitioning){
  2743. backup_s.pb2= s->pb2;
  2744. backup_s.tex_pb= s->tex_pb;
  2745. }
  2746. if(mb_type&MB_TYPE_INTER){
  2747. s->mv_dir = MV_DIR_FORWARD;
  2748. s->mv_type = MV_TYPE_16X16;
  2749. s->mb_intra= 0;
  2750. s->mv[0][0][0] = s->p_mv_table[xy][0];
  2751. s->mv[0][0][1] = s->p_mv_table[xy][1];
  2752. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  2753. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2754. }
  2755. if(mb_type&MB_TYPE_INTER4V){
  2756. s->mv_dir = MV_DIR_FORWARD;
  2757. s->mv_type = MV_TYPE_8X8;
  2758. s->mb_intra= 0;
  2759. for(i=0; i<4; i++){
  2760. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  2761. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  2762. }
  2763. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  2764. &dmin, &next_block, 0, 0);
  2765. }
  2766. if(mb_type&MB_TYPE_FORWARD){
  2767. s->mv_dir = MV_DIR_FORWARD;
  2768. s->mv_type = MV_TYPE_16X16;
  2769. s->mb_intra= 0;
  2770. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2771. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2772. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  2773. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2774. }
  2775. if(mb_type&MB_TYPE_BACKWARD){
  2776. s->mv_dir = MV_DIR_BACKWARD;
  2777. s->mv_type = MV_TYPE_16X16;
  2778. s->mb_intra= 0;
  2779. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2780. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2781. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  2782. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  2783. }
  2784. if(mb_type&MB_TYPE_BIDIR){
  2785. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2786. s->mv_type = MV_TYPE_16X16;
  2787. s->mb_intra= 0;
  2788. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2789. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2790. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2791. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2792. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  2793. &dmin, &next_block, 0, 0);
  2794. }
  2795. if(mb_type&MB_TYPE_DIRECT){
  2796. int mx= s->b_direct_mv_table[xy][0];
  2797. int my= s->b_direct_mv_table[xy][1];
  2798. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2799. s->mb_intra= 0;
  2800. #ifdef CONFIG_RISKY
  2801. ff_mpeg4_set_direct_mv(s, mx, my);
  2802. #endif
  2803. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2804. &dmin, &next_block, mx, my);
  2805. }
  2806. if(mb_type&MB_TYPE_INTRA){
  2807. s->mv_dir = 0;
  2808. s->mv_type = MV_TYPE_16X16;
  2809. s->mb_intra= 1;
  2810. s->mv[0][0][0] = 0;
  2811. s->mv[0][0][1] = 0;
  2812. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  2813. &dmin, &next_block, 0, 0);
  2814. /* force cleaning of ac/dc pred stuff if needed ... */
  2815. if(s->h263_pred || s->h263_aic)
  2816. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  2817. }
  2818. copy_context_after_encode(s, &best_s, -1);
  2819. pb_bits_count= get_bit_count(&s->pb);
  2820. flush_put_bits(&s->pb);
  2821. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  2822. s->pb= backup_s.pb;
  2823. if(s->data_partitioning){
  2824. pb2_bits_count= get_bit_count(&s->pb2);
  2825. flush_put_bits(&s->pb2);
  2826. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  2827. s->pb2= backup_s.pb2;
  2828. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  2829. flush_put_bits(&s->tex_pb);
  2830. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  2831. s->tex_pb= backup_s.tex_pb;
  2832. }
  2833. s->last_bits= get_bit_count(&s->pb);
  2834. } else {
  2835. int motion_x, motion_y;
  2836. int intra_score;
  2837. int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_width];
  2838. if(!(s->flags&CODEC_FLAG_HQ) && s->pict_type==P_TYPE){
  2839. /* get luma score */
  2840. if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  2841. intra_score= (s->current_picture.mb_var[mb_x + mb_y*s->mb_width]<<8) - 500; //FIXME dont scale it down so we dont have to fix it
  2842. }else{
  2843. uint8_t *dest_y;
  2844. int mean= s->current_picture.mb_mean[mb_x + mb_y*s->mb_width]; //FIXME
  2845. mean*= 0x01010101;
  2846. dest_y = s->new_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  2847. for(i=0; i<16; i++){
  2848. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
  2849. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
  2850. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
  2851. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
  2852. }
  2853. s->mb_intra=1;
  2854. intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, dest_y, s->linesize);
  2855. /* printf("intra:%7d inter:%7d var:%7d mc_var.%7d\n", intra_score>>8, inter_score>>8,
  2856. s->current_picture.mb_var[mb_x + mb_y*s->mb_width],
  2857. s->current_picture.mc_mb_var[mb_x + mb_y*s->mb_width]);*/
  2858. }
  2859. /* get chroma score */
  2860. if(s->avctx->mb_cmp&FF_CMP_CHROMA){
  2861. int i;
  2862. s->mb_intra=1;
  2863. for(i=1; i<3; i++){
  2864. uint8_t *dest_c;
  2865. int mean;
  2866. if(s->out_format == FMT_H263){
  2867. mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
  2868. }else{
  2869. mean= (s->last_dc[i] + 4)>>3;
  2870. }
  2871. dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2872. mean*= 0x01010101;
  2873. for(i=0; i<8; i++){
  2874. *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
  2875. *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
  2876. }
  2877. intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
  2878. }
  2879. }
  2880. /* bias */
  2881. switch(s->avctx->mb_cmp&0xFF){
  2882. default:
  2883. case FF_CMP_SAD:
  2884. intra_score+= 32*s->qscale;
  2885. break;
  2886. case FF_CMP_SSE:
  2887. intra_score+= 24*s->qscale*s->qscale;
  2888. break;
  2889. case FF_CMP_SATD:
  2890. intra_score+= 96*s->qscale;
  2891. break;
  2892. case FF_CMP_DCT:
  2893. intra_score+= 48*s->qscale;
  2894. break;
  2895. case FF_CMP_BIT:
  2896. intra_score+= 16;
  2897. break;
  2898. case FF_CMP_PSNR:
  2899. case FF_CMP_RD:
  2900. intra_score+= (s->qscale*s->qscale*109*8 + 64)>>7;
  2901. break;
  2902. }
  2903. if(intra_score < inter_score)
  2904. mb_type= MB_TYPE_INTRA;
  2905. }
  2906. s->mv_type=MV_TYPE_16X16;
  2907. // only one MB-Type possible
  2908. switch(mb_type){
  2909. case MB_TYPE_INTRA:
  2910. s->mv_dir = 0;
  2911. s->mb_intra= 1;
  2912. motion_x= s->mv[0][0][0] = 0;
  2913. motion_y= s->mv[0][0][1] = 0;
  2914. break;
  2915. case MB_TYPE_INTER:
  2916. s->mv_dir = MV_DIR_FORWARD;
  2917. s->mb_intra= 0;
  2918. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  2919. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  2920. break;
  2921. case MB_TYPE_INTER4V:
  2922. s->mv_dir = MV_DIR_FORWARD;
  2923. s->mv_type = MV_TYPE_8X8;
  2924. s->mb_intra= 0;
  2925. for(i=0; i<4; i++){
  2926. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  2927. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  2928. }
  2929. motion_x= motion_y= 0;
  2930. break;
  2931. case MB_TYPE_DIRECT:
  2932. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2933. s->mb_intra= 0;
  2934. motion_x=s->b_direct_mv_table[xy][0];
  2935. motion_y=s->b_direct_mv_table[xy][1];
  2936. #ifdef CONFIG_RISKY
  2937. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  2938. #endif
  2939. break;
  2940. case MB_TYPE_BIDIR:
  2941. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2942. s->mb_intra= 0;
  2943. motion_x=0;
  2944. motion_y=0;
  2945. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2946. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2947. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2948. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2949. break;
  2950. case MB_TYPE_BACKWARD:
  2951. s->mv_dir = MV_DIR_BACKWARD;
  2952. s->mb_intra= 0;
  2953. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2954. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2955. break;
  2956. case MB_TYPE_FORWARD:
  2957. s->mv_dir = MV_DIR_FORWARD;
  2958. s->mb_intra= 0;
  2959. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2960. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2961. // printf(" %d %d ", motion_x, motion_y);
  2962. break;
  2963. default:
  2964. motion_x=motion_y=0; //gcc warning fix
  2965. printf("illegal MB type\n");
  2966. }
  2967. encode_mb(s, motion_x, motion_y);
  2968. // RAL: Update last macrobloc type
  2969. s->last_mv_dir = s->mv_dir;
  2970. }
  2971. /* clean the MV table in IPS frames for direct mode in B frames */
  2972. if(s->mb_intra /* && I,P,S_TYPE */){
  2973. s->p_mv_table[xy][0]=0;
  2974. s->p_mv_table[xy][1]=0;
  2975. }
  2976. MPV_decode_mb(s, s->block);
  2977. if(s->flags&CODEC_FLAG_PSNR){
  2978. int w= 16;
  2979. int h= 16;
  2980. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  2981. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  2982. s->current_picture.error[0] += sse(
  2983. s,
  2984. s->new_picture .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  2985. s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  2986. w, h, s->linesize);
  2987. s->current_picture.error[1] += sse(
  2988. s,
  2989. s->new_picture .data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2990. s->current_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2991. w>>1, h>>1, s->uvlinesize);
  2992. s->current_picture.error[2] += sse(
  2993. s,
  2994. s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2995. s->current_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2996. w>>1, h>>1, s->uvlinesize);
  2997. }
  2998. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
  2999. }
  3000. /* Obtain average mb_row size for RTP */
  3001. if (s->rtp_mode) {
  3002. if (mb_y==0)
  3003. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  3004. else {
  3005. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  3006. }
  3007. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  3008. }
  3009. }
  3010. emms_c();
  3011. #ifdef CONFIG_RISKY
  3012. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  3013. ff_mpeg4_merge_partitions(s);
  3014. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  3015. msmpeg4_encode_ext_header(s);
  3016. if(s->codec_id==CODEC_ID_MPEG4)
  3017. ff_mpeg4_stuffing(&s->pb);
  3018. #endif
  3019. //if (s->gob_number)
  3020. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  3021. /* Send the last GOB if RTP */
  3022. if (s->rtp_mode) {
  3023. flush_put_bits(&s->pb);
  3024. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  3025. /* Call the RTP callback to send the last GOB */
  3026. if (s->rtp_callback)
  3027. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  3028. s->ptr_lastgob = pbBufPtr(&s->pb);
  3029. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  3030. }
  3031. }
  3032. static int dct_quantize_trellis_c(MpegEncContext *s,
  3033. DCTELEM *block, int n,
  3034. int qscale, int *overflow){
  3035. const int *qmat;
  3036. const uint8_t *scantable= s->intra_scantable.scantable;
  3037. int max=0;
  3038. unsigned int threshold1, threshold2;
  3039. int bias=0;
  3040. int run_tab[65];
  3041. int level_tab[65];
  3042. int score_tab[65];
  3043. int last_run=0;
  3044. int last_level=0;
  3045. int last_score= 0;
  3046. int last_i= 0;
  3047. int coeff[3][64];
  3048. int coeff_count[64];
  3049. int lambda, qmul, qadd, start_i, last_non_zero, i;
  3050. const int esc_length= s->ac_esc_length;
  3051. uint8_t * length;
  3052. uint8_t * last_length;
  3053. int score_limit=0;
  3054. int left_limit= 0;
  3055. s->fdct (block);
  3056. qmul= qscale*16;
  3057. qadd= ((qscale-1)|1)*8;
  3058. if (s->mb_intra) {
  3059. int q;
  3060. if (!s->h263_aic) {
  3061. if (n < 4)
  3062. q = s->y_dc_scale;
  3063. else
  3064. q = s->c_dc_scale;
  3065. q = q << 3;
  3066. } else{
  3067. /* For AIC we skip quant/dequant of INTRADC */
  3068. q = 1 << 3;
  3069. qadd=0;
  3070. }
  3071. /* note: block[0] is assumed to be positive */
  3072. block[0] = (block[0] + (q >> 1)) / q;
  3073. start_i = 1;
  3074. last_non_zero = 0;
  3075. qmat = s->q_intra_matrix[qscale];
  3076. if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO)
  3077. bias= 1<<(QMAT_SHIFT-1);
  3078. length = s->intra_ac_vlc_length;
  3079. last_length= s->intra_ac_vlc_last_length;
  3080. } else {
  3081. start_i = 0;
  3082. last_non_zero = -1;
  3083. qmat = s->q_inter_matrix[qscale];
  3084. length = s->inter_ac_vlc_length;
  3085. last_length= s->inter_ac_vlc_last_length;
  3086. }
  3087. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3088. threshold2= (threshold1<<1);
  3089. for(i=start_i; i<64; i++) {
  3090. const int j = scantable[i];
  3091. const int k= i-start_i;
  3092. int level = block[j];
  3093. level = level * qmat[j];
  3094. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3095. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3096. if(((unsigned)(level+threshold1))>threshold2){
  3097. if(level>0){
  3098. level= (bias + level)>>QMAT_SHIFT;
  3099. coeff[0][k]= level;
  3100. coeff[1][k]= level-1;
  3101. // coeff[2][k]= level-2;
  3102. }else{
  3103. level= (bias - level)>>QMAT_SHIFT;
  3104. coeff[0][k]= -level;
  3105. coeff[1][k]= -level+1;
  3106. // coeff[2][k]= -level+2;
  3107. }
  3108. coeff_count[k]= FFMIN(level, 2);
  3109. max |=level;
  3110. last_non_zero = i;
  3111. }else{
  3112. coeff[0][k]= (level>>31)|1;
  3113. coeff_count[k]= 1;
  3114. }
  3115. }
  3116. *overflow= s->max_qcoeff < max; //overflow might have happend
  3117. if(last_non_zero < start_i){
  3118. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3119. return last_non_zero;
  3120. }
  3121. lambda= (qscale*qscale*64*105 + 64)>>7; //FIXME finetune
  3122. score_tab[0]= 0;
  3123. for(i=0; i<=last_non_zero - start_i; i++){
  3124. int level_index, run, j;
  3125. const int dct_coeff= block[ scantable[i + start_i] ];
  3126. const int zero_distoration= dct_coeff*dct_coeff;
  3127. int best_score=256*256*256*120;
  3128. last_score += zero_distoration;
  3129. for(level_index=0; level_index < coeff_count[i]; level_index++){
  3130. int distoration;
  3131. int level= coeff[level_index][i];
  3132. int unquant_coeff;
  3133. assert(level);
  3134. if(s->out_format == FMT_H263){
  3135. if(level>0){
  3136. unquant_coeff= level*qmul + qadd;
  3137. }else{
  3138. unquant_coeff= level*qmul - qadd;
  3139. }
  3140. }else{ //MPEG1
  3141. j= s->idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
  3142. if(s->mb_intra){
  3143. if (level < 0) {
  3144. unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
  3145. unquant_coeff = -((unquant_coeff - 1) | 1);
  3146. } else {
  3147. unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3;
  3148. unquant_coeff = (unquant_coeff - 1) | 1;
  3149. }
  3150. }else{
  3151. if (level < 0) {
  3152. unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3153. unquant_coeff = -((unquant_coeff - 1) | 1);
  3154. } else {
  3155. unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3156. unquant_coeff = (unquant_coeff - 1) | 1;
  3157. }
  3158. }
  3159. unquant_coeff<<= 3;
  3160. }
  3161. distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
  3162. level+=64;
  3163. if((level&(~127)) == 0){
  3164. for(run=0; run<=i - left_limit; run++){
  3165. int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3166. score += score_tab[i-run];
  3167. if(score < best_score){
  3168. best_score=
  3169. score_tab[i+1]= score;
  3170. run_tab[i+1]= run;
  3171. level_tab[i+1]= level-64;
  3172. }
  3173. }
  3174. if(s->out_format == FMT_H263){
  3175. for(run=0; run<=i - left_limit; run++){
  3176. int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3177. score += score_tab[i-run];
  3178. if(score < last_score){
  3179. last_score= score;
  3180. last_run= run;
  3181. last_level= level-64;
  3182. last_i= i+1;
  3183. }
  3184. }
  3185. }
  3186. }else{
  3187. distoration += esc_length*lambda;
  3188. for(run=0; run<=i - left_limit; run++){
  3189. int score= distoration + score_tab[i-run];
  3190. if(score < best_score){
  3191. best_score=
  3192. score_tab[i+1]= score;
  3193. run_tab[i+1]= run;
  3194. level_tab[i+1]= level-64;
  3195. }
  3196. }
  3197. if(s->out_format == FMT_H263){
  3198. for(run=0; run<=i - left_limit; run++){
  3199. int score= distoration + score_tab[i-run];
  3200. if(score < last_score){
  3201. last_score= score;
  3202. last_run= run;
  3203. last_level= level-64;
  3204. last_i= i+1;
  3205. }
  3206. }
  3207. }
  3208. }
  3209. }
  3210. for(j=left_limit; j<=i; j++){
  3211. score_tab[j] += zero_distoration;
  3212. }
  3213. score_limit+= zero_distoration;
  3214. if(score_tab[i+1] < score_limit)
  3215. score_limit= score_tab[i+1];
  3216. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  3217. while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
  3218. }
  3219. //FIXME add some cbp penalty
  3220. if(s->out_format != FMT_H263){
  3221. last_score= 256*256*256*120;
  3222. for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
  3223. int score= score_tab[i];
  3224. if(i) score += lambda*2; //FIXME exacter?
  3225. if(score < last_score){
  3226. last_score= score;
  3227. last_i= i;
  3228. last_level= level_tab[i];
  3229. last_run= run_tab[i];
  3230. }
  3231. }
  3232. }
  3233. last_non_zero= last_i - 1 + start_i;
  3234. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3235. if(last_non_zero < start_i)
  3236. return last_non_zero;
  3237. i= last_i;
  3238. assert(last_level);
  3239. //FIXME use permutated scantable
  3240. block[ s->idct_permutation[ scantable[last_non_zero] ] ]= last_level;
  3241. i -= last_run + 1;
  3242. for(;i>0 ; i -= run_tab[i] + 1){
  3243. const int j= s->idct_permutation[ scantable[i - 1 + start_i] ];
  3244. block[j]= level_tab[i];
  3245. assert(block[j]);
  3246. }
  3247. return last_non_zero;
  3248. }
  3249. static int dct_quantize_c(MpegEncContext *s,
  3250. DCTELEM *block, int n,
  3251. int qscale, int *overflow)
  3252. {
  3253. int i, j, level, last_non_zero, q;
  3254. const int *qmat;
  3255. const uint8_t *scantable= s->intra_scantable.scantable;
  3256. int bias;
  3257. int max=0;
  3258. unsigned int threshold1, threshold2;
  3259. s->fdct (block);
  3260. if (s->mb_intra) {
  3261. if (!s->h263_aic) {
  3262. if (n < 4)
  3263. q = s->y_dc_scale;
  3264. else
  3265. q = s->c_dc_scale;
  3266. q = q << 3;
  3267. } else
  3268. /* For AIC we skip quant/dequant of INTRADC */
  3269. q = 1 << 3;
  3270. /* note: block[0] is assumed to be positive */
  3271. block[0] = (block[0] + (q >> 1)) / q;
  3272. i = 1;
  3273. last_non_zero = 0;
  3274. qmat = s->q_intra_matrix[qscale];
  3275. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3276. } else {
  3277. i = 0;
  3278. last_non_zero = -1;
  3279. qmat = s->q_inter_matrix[qscale];
  3280. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3281. }
  3282. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3283. threshold2= (threshold1<<1);
  3284. for(;i<64;i++) {
  3285. j = scantable[i];
  3286. level = block[j];
  3287. level = level * qmat[j];
  3288. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3289. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3290. if(((unsigned)(level+threshold1))>threshold2){
  3291. if(level>0){
  3292. level= (bias + level)>>QMAT_SHIFT;
  3293. block[j]= level;
  3294. }else{
  3295. level= (bias - level)>>QMAT_SHIFT;
  3296. block[j]= -level;
  3297. }
  3298. max |=level;
  3299. last_non_zero = i;
  3300. }else{
  3301. block[j]=0;
  3302. }
  3303. }
  3304. *overflow= s->max_qcoeff < max; //overflow might have happend
  3305. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  3306. if (s->idct_permutation_type != FF_NO_IDCT_PERM)
  3307. ff_block_permute(block, s->idct_permutation, scantable, last_non_zero);
  3308. return last_non_zero;
  3309. }
  3310. #endif //CONFIG_ENCODERS
  3311. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  3312. DCTELEM *block, int n, int qscale)
  3313. {
  3314. int i, level, nCoeffs;
  3315. const uint16_t *quant_matrix;
  3316. nCoeffs= s->block_last_index[n];
  3317. if (s->mb_intra) {
  3318. if (n < 4)
  3319. block[0] = block[0] * s->y_dc_scale;
  3320. else
  3321. block[0] = block[0] * s->c_dc_scale;
  3322. /* XXX: only mpeg1 */
  3323. quant_matrix = s->intra_matrix;
  3324. for(i=1;i<=nCoeffs;i++) {
  3325. int j= s->intra_scantable.permutated[i];
  3326. level = block[j];
  3327. if (level) {
  3328. if (level < 0) {
  3329. level = -level;
  3330. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3331. level = (level - 1) | 1;
  3332. level = -level;
  3333. } else {
  3334. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3335. level = (level - 1) | 1;
  3336. }
  3337. #ifdef PARANOID
  3338. if (level < -2048 || level > 2047)
  3339. fprintf(stderr, "unquant error %d %d\n", i, level);
  3340. #endif
  3341. block[j] = level;
  3342. }
  3343. }
  3344. } else {
  3345. i = 0;
  3346. quant_matrix = s->inter_matrix;
  3347. for(;i<=nCoeffs;i++) {
  3348. int j= s->intra_scantable.permutated[i];
  3349. level = block[j];
  3350. if (level) {
  3351. if (level < 0) {
  3352. level = -level;
  3353. level = (((level << 1) + 1) * qscale *
  3354. ((int) (quant_matrix[j]))) >> 4;
  3355. level = (level - 1) | 1;
  3356. level = -level;
  3357. } else {
  3358. level = (((level << 1) + 1) * qscale *
  3359. ((int) (quant_matrix[j]))) >> 4;
  3360. level = (level - 1) | 1;
  3361. }
  3362. #ifdef PARANOID
  3363. if (level < -2048 || level > 2047)
  3364. fprintf(stderr, "unquant error %d %d\n", i, level);
  3365. #endif
  3366. block[j] = level;
  3367. }
  3368. }
  3369. }
  3370. }
  3371. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  3372. DCTELEM *block, int n, int qscale)
  3373. {
  3374. int i, level, nCoeffs;
  3375. const uint16_t *quant_matrix;
  3376. if(s->alternate_scan) nCoeffs= 63;
  3377. else nCoeffs= s->block_last_index[n];
  3378. if (s->mb_intra) {
  3379. if (n < 4)
  3380. block[0] = block[0] * s->y_dc_scale;
  3381. else
  3382. block[0] = block[0] * s->c_dc_scale;
  3383. quant_matrix = s->intra_matrix;
  3384. for(i=1;i<=nCoeffs;i++) {
  3385. int j= s->intra_scantable.permutated[i];
  3386. level = block[j];
  3387. if (level) {
  3388. if (level < 0) {
  3389. level = -level;
  3390. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3391. level = -level;
  3392. } else {
  3393. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3394. }
  3395. #ifdef PARANOID
  3396. if (level < -2048 || level > 2047)
  3397. fprintf(stderr, "unquant error %d %d\n", i, level);
  3398. #endif
  3399. block[j] = level;
  3400. }
  3401. }
  3402. } else {
  3403. int sum=-1;
  3404. i = 0;
  3405. quant_matrix = s->inter_matrix;
  3406. for(;i<=nCoeffs;i++) {
  3407. int j= s->intra_scantable.permutated[i];
  3408. level = block[j];
  3409. if (level) {
  3410. if (level < 0) {
  3411. level = -level;
  3412. level = (((level << 1) + 1) * qscale *
  3413. ((int) (quant_matrix[j]))) >> 4;
  3414. level = -level;
  3415. } else {
  3416. level = (((level << 1) + 1) * qscale *
  3417. ((int) (quant_matrix[j]))) >> 4;
  3418. }
  3419. #ifdef PARANOID
  3420. if (level < -2048 || level > 2047)
  3421. fprintf(stderr, "unquant error %d %d\n", i, level);
  3422. #endif
  3423. block[j] = level;
  3424. sum+=level;
  3425. }
  3426. }
  3427. block[63]^=sum&1;
  3428. }
  3429. }
  3430. static void dct_unquantize_h263_c(MpegEncContext *s,
  3431. DCTELEM *block, int n, int qscale)
  3432. {
  3433. int i, level, qmul, qadd;
  3434. int nCoeffs;
  3435. assert(s->block_last_index[n]>=0);
  3436. qadd = (qscale - 1) | 1;
  3437. qmul = qscale << 1;
  3438. if (s->mb_intra) {
  3439. if (!s->h263_aic) {
  3440. if (n < 4)
  3441. block[0] = block[0] * s->y_dc_scale;
  3442. else
  3443. block[0] = block[0] * s->c_dc_scale;
  3444. }else
  3445. qadd = 0;
  3446. i = 1;
  3447. nCoeffs= 63; //does not allways use zigzag table
  3448. } else {
  3449. i = 0;
  3450. nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
  3451. }
  3452. for(;i<=nCoeffs;i++) {
  3453. level = block[i];
  3454. if (level) {
  3455. if (level < 0) {
  3456. level = level * qmul - qadd;
  3457. } else {
  3458. level = level * qmul + qadd;
  3459. }
  3460. #ifdef PARANOID
  3461. if (level < -2048 || level > 2047)
  3462. fprintf(stderr, "unquant error %d %d\n", i, level);
  3463. #endif
  3464. block[i] = level;
  3465. }
  3466. }
  3467. }
  3468. char ff_get_pict_type_char(int pict_type){
  3469. switch(pict_type){
  3470. case I_TYPE: return 'I';
  3471. case P_TYPE: return 'P';
  3472. case B_TYPE: return 'B';
  3473. case S_TYPE: return 'S';
  3474. default: return '?';
  3475. }
  3476. }
  3477. extern const AVOption common_options[2];
  3478. static const AVOption mpeg4_options[] =
  3479. {
  3480. AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
  3481. AVOPTION_CODEC_FLAG("vhq", "very high quality", flags, CODEC_FLAG_HQ, 0),
  3482. AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
  3483. "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
  3484. bit_rate_tolerance, 4, 240000000, 8000),
  3485. AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
  3486. AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
  3487. AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
  3488. rc_eq, "tex^qComp,option1,options2", 0),
  3489. AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
  3490. rc_min_rate, 4, 24000000, 0),
  3491. AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
  3492. rc_max_rate, 4, 24000000, 0),
  3493. AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
  3494. flags, CODEC_FLAG_PSNR, 0),
  3495. AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
  3496. rc_override),
  3497. AVOPTION_SUB(common_options),
  3498. AVOPTION_END()
  3499. };
  3500. #ifdef CONFIG_ENCODERS
  3501. AVCodec mpeg1video_encoder = {
  3502. "mpeg1video",
  3503. CODEC_TYPE_VIDEO,
  3504. CODEC_ID_MPEG1VIDEO,
  3505. sizeof(MpegEncContext),
  3506. MPV_encode_init,
  3507. MPV_encode_picture,
  3508. MPV_encode_end,
  3509. };
  3510. #ifdef CONFIG_RISKY
  3511. AVCodec h263_encoder = {
  3512. "h263",
  3513. CODEC_TYPE_VIDEO,
  3514. CODEC_ID_H263,
  3515. sizeof(MpegEncContext),
  3516. MPV_encode_init,
  3517. MPV_encode_picture,
  3518. MPV_encode_end,
  3519. };
  3520. AVCodec h263p_encoder = {
  3521. "h263p",
  3522. CODEC_TYPE_VIDEO,
  3523. CODEC_ID_H263P,
  3524. sizeof(MpegEncContext),
  3525. MPV_encode_init,
  3526. MPV_encode_picture,
  3527. MPV_encode_end,
  3528. };
  3529. AVCodec rv10_encoder = {
  3530. "rv10",
  3531. CODEC_TYPE_VIDEO,
  3532. CODEC_ID_RV10,
  3533. sizeof(MpegEncContext),
  3534. MPV_encode_init,
  3535. MPV_encode_picture,
  3536. MPV_encode_end,
  3537. };
  3538. AVCodec mpeg4_encoder = {
  3539. "mpeg4",
  3540. CODEC_TYPE_VIDEO,
  3541. CODEC_ID_MPEG4,
  3542. sizeof(MpegEncContext),
  3543. MPV_encode_init,
  3544. MPV_encode_picture,
  3545. MPV_encode_end,
  3546. .options = mpeg4_options,
  3547. };
  3548. AVCodec msmpeg4v1_encoder = {
  3549. "msmpeg4v1",
  3550. CODEC_TYPE_VIDEO,
  3551. CODEC_ID_MSMPEG4V1,
  3552. sizeof(MpegEncContext),
  3553. MPV_encode_init,
  3554. MPV_encode_picture,
  3555. MPV_encode_end,
  3556. };
  3557. AVCodec msmpeg4v2_encoder = {
  3558. "msmpeg4v2",
  3559. CODEC_TYPE_VIDEO,
  3560. CODEC_ID_MSMPEG4V2,
  3561. sizeof(MpegEncContext),
  3562. MPV_encode_init,
  3563. MPV_encode_picture,
  3564. MPV_encode_end,
  3565. };
  3566. AVCodec msmpeg4v3_encoder = {
  3567. "msmpeg4",
  3568. CODEC_TYPE_VIDEO,
  3569. CODEC_ID_MSMPEG4V3,
  3570. sizeof(MpegEncContext),
  3571. MPV_encode_init,
  3572. MPV_encode_picture,
  3573. MPV_encode_end,
  3574. };
  3575. AVCodec wmv1_encoder = {
  3576. "wmv1",
  3577. CODEC_TYPE_VIDEO,
  3578. CODEC_ID_WMV1,
  3579. sizeof(MpegEncContext),
  3580. MPV_encode_init,
  3581. MPV_encode_picture,
  3582. MPV_encode_end,
  3583. };
  3584. #endif
  3585. AVCodec mjpeg_encoder = {
  3586. "mjpeg",
  3587. CODEC_TYPE_VIDEO,
  3588. CODEC_ID_MJPEG,
  3589. sizeof(MpegEncContext),
  3590. MPV_encode_init,
  3591. MPV_encode_picture,
  3592. MPV_encode_end,
  3593. };
  3594. #endif //CONFIG_ENCODERS