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.

4670 lines
162KB

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