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.

4161 lines
143KB

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