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.

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