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.

4764 lines
165KB

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