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.

4296 lines
149KB

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