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.

4264 lines
148KB

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