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.

4674 lines
162KB

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