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.

4239 lines
147KB

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