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.

4789 lines
166KB

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