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.

4165 lines
144KB

  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_ptr->pict_type= s->pict_type;
  791. s->current_picture_ptr->quality= s->qscale;
  792. s->current_picture= *s->current_picture_ptr;
  793. if(s->out_format != FMT_H264){
  794. if (s->pict_type != B_TYPE) {
  795. s->last_picture_ptr= s->next_picture_ptr;
  796. s->next_picture_ptr= s->current_picture_ptr;
  797. }
  798. if(s->last_picture_ptr) s->last_picture= *s->last_picture_ptr;
  799. if(s->next_picture_ptr) s->next_picture= *s->next_picture_ptr;
  800. if(s->new_picture_ptr ) s->new_picture = *s->new_picture_ptr;
  801. if(s->picture_structure!=PICT_FRAME){
  802. int i;
  803. for(i=0; i<4; i++){
  804. if(s->picture_structure == PICT_BOTTOM_FIELD){
  805. s->current_picture.data[i] += s->current_picture.linesize[i];
  806. }
  807. s->current_picture.linesize[i] *= 2;
  808. s->last_picture.linesize[i] *=2;
  809. s->next_picture.linesize[i] *=2;
  810. }
  811. }
  812. if(s->pict_type != I_TYPE && s->last_picture_ptr==NULL){
  813. fprintf(stderr, "warning: first frame is no keyframe\n");
  814. assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
  815. goto alloc;
  816. }
  817. }
  818. s->hurry_up= s->avctx->hurry_up;
  819. s->error_resilience= avctx->error_resilience;
  820. /* set dequantizer, we cant do it during init as it might change for mpeg4
  821. and we cant do it in the header decode as init isnt called for mpeg4 there yet */
  822. if(s->out_format == FMT_H263){
  823. if(s->mpeg_quant)
  824. s->dct_unquantize = s->dct_unquantize_mpeg2;
  825. else
  826. s->dct_unquantize = s->dct_unquantize_h263;
  827. }else
  828. s->dct_unquantize = s->dct_unquantize_mpeg1;
  829. return 0;
  830. }
  831. /* generic function for encode/decode called after a frame has been coded/decoded */
  832. void MPV_frame_end(MpegEncContext *s)
  833. {
  834. int i;
  835. /* draw edge for correct motion prediction if outside */
  836. if(s->codec_id!=CODEC_ID_SVQ1){
  837. if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  838. draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  839. draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  840. draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  841. }
  842. }
  843. emms_c();
  844. s->last_pict_type = s->pict_type;
  845. if(s->pict_type!=B_TYPE){
  846. s->last_non_b_pict_type= s->pict_type;
  847. }
  848. #if 0
  849. /* copy back current_picture variables */
  850. for(i=0; i<MAX_PICTURE_COUNT; i++){
  851. if(s->picture[i].data[0] == s->current_picture.data[0]){
  852. s->picture[i]= s->current_picture;
  853. break;
  854. }
  855. }
  856. assert(i<MAX_PICTURE_COUNT);
  857. #endif
  858. s->current_picture_ptr->quality= s->qscale; //FIXME get average of qscale_table
  859. s->current_picture_ptr->pict_type= s->pict_type;
  860. s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
  861. /* release non refernce frames */
  862. for(i=0; i<MAX_PICTURE_COUNT; i++){
  863. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/)
  864. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  865. }
  866. if(s->avctx->debug&FF_DEBUG_SKIP){
  867. int x,y;
  868. for(y=0; y<s->mb_height; y++){
  869. for(x=0; x<s->mb_width; x++){
  870. int count= s->mbskip_table[x + y*s->mb_width];
  871. if(count>9) count=9;
  872. printf(" %1d", count);
  873. }
  874. printf("\n");
  875. }
  876. printf("pict type: %d\n", s->pict_type);
  877. }
  878. // clear copies, to avoid confusion
  879. #if 0
  880. memset(&s->last_picture, 0, sizeof(Picture));
  881. memset(&s->next_picture, 0, sizeof(Picture));
  882. memset(&s->current_picture, 0, sizeof(Picture));
  883. #endif
  884. }
  885. #ifdef CONFIG_ENCODERS
  886. static int get_sae(uint8_t *src, int ref, int stride){
  887. int x,y;
  888. int acc=0;
  889. for(y=0; y<16; y++){
  890. for(x=0; x<16; x++){
  891. acc+= ABS(src[x+y*stride] - ref);
  892. }
  893. }
  894. return acc;
  895. }
  896. static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
  897. int x, y, w, h;
  898. int acc=0;
  899. w= s->width &~15;
  900. h= s->height&~15;
  901. for(y=0; y<h; y+=16){
  902. for(x=0; x<w; x+=16){
  903. int offset= x + y*stride;
  904. int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
  905. int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
  906. int sae = get_sae(src + offset, mean, stride);
  907. acc+= sae + 500 < sad;
  908. }
  909. }
  910. return acc;
  911. }
  912. static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
  913. AVFrame *pic;
  914. int i;
  915. const int encoding_delay= s->max_b_frames;
  916. int direct=1;
  917. if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
  918. if(pic_arg->linesize[0] != s->linesize) direct=0;
  919. if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
  920. if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
  921. // printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
  922. if(direct){
  923. i= find_unused_picture(s, 1);
  924. pic= (AVFrame*)&s->picture[i];
  925. pic->reference= 3;
  926. for(i=0; i<4; i++){
  927. pic->data[i]= pic_arg->data[i];
  928. pic->linesize[i]= pic_arg->linesize[i];
  929. }
  930. alloc_picture(s, (Picture*)pic, 1);
  931. }else{
  932. i= find_unused_picture(s, 0);
  933. pic= (AVFrame*)&s->picture[i];
  934. pic->reference= 3;
  935. alloc_picture(s, (Picture*)pic, 0);
  936. for(i=0; i<4; i++){
  937. /* the input will be 16 pixels to the right relative to the actual buffer start
  938. * and the current_pic, so the buffer can be reused, yes its not beatifull
  939. */
  940. pic->data[i]+= 16;
  941. }
  942. if( pic->data[0] == pic_arg->data[0]
  943. && pic->data[1] == pic_arg->data[1]
  944. && pic->data[2] == pic_arg->data[2]){
  945. // empty
  946. }else{
  947. int h_chroma_shift, v_chroma_shift;
  948. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  949. for(i=0; i<3; i++){
  950. int src_stride= pic_arg->linesize[i];
  951. int dst_stride= i ? s->uvlinesize : s->linesize;
  952. int h_shift= i ? h_chroma_shift : 0;
  953. int v_shift= i ? v_chroma_shift : 0;
  954. int w= s->width >>h_shift;
  955. int h= s->height>>v_shift;
  956. uint8_t *src= pic_arg->data[i];
  957. uint8_t *dst= pic->data[i];
  958. if(src_stride==dst_stride)
  959. memcpy(dst, src, src_stride*h);
  960. else{
  961. while(h--){
  962. memcpy(dst, src, w);
  963. dst += dst_stride;
  964. src += src_stride;
  965. }
  966. }
  967. }
  968. }
  969. }
  970. pic->quality= pic_arg->quality;
  971. pic->pict_type= pic_arg->pict_type;
  972. pic->pts = pic_arg->pts;
  973. if(s->input_picture[encoding_delay])
  974. pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
  975. /* shift buffer entries */
  976. for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
  977. s->input_picture[i-1]= s->input_picture[i];
  978. s->input_picture[encoding_delay]= (Picture*)pic;
  979. return 0;
  980. }
  981. static void select_input_picture(MpegEncContext *s){
  982. int i;
  983. const int encoding_delay= s->max_b_frames;
  984. int coded_pic_num=0;
  985. if(s->reordered_input_picture[0])
  986. coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
  987. for(i=1; i<MAX_PICTURE_COUNT; i++)
  988. s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
  989. s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
  990. /* set next picture types & ordering */
  991. if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
  992. if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
  993. s->reordered_input_picture[0]= s->input_picture[0];
  994. s->reordered_input_picture[0]->pict_type= I_TYPE;
  995. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  996. }else{
  997. int b_frames;
  998. if(s->flags&CODEC_FLAG_PASS2){
  999. for(i=0; i<s->max_b_frames+1; i++){
  1000. int pict_num= s->input_picture[0]->display_picture_number + i;
  1001. int pict_type= s->rc_context.entry[pict_num].new_pict_type;
  1002. s->input_picture[i]->pict_type= pict_type;
  1003. if(i + 1 >= s->rc_context.num_entries) break;
  1004. }
  1005. }
  1006. if(s->input_picture[0]->pict_type){
  1007. /* user selected pict_type */
  1008. for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
  1009. if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
  1010. }
  1011. if(b_frames > s->max_b_frames){
  1012. fprintf(stderr, "warning, too many bframes in a row\n");
  1013. b_frames = s->max_b_frames;
  1014. }
  1015. }else if(s->b_frame_strategy==0){
  1016. b_frames= s->max_b_frames;
  1017. }else if(s->b_frame_strategy==1){
  1018. for(i=1; i<s->max_b_frames+1; i++){
  1019. if(s->input_picture[i]->b_frame_score==0){
  1020. s->input_picture[i]->b_frame_score=
  1021. get_intra_count(s, s->input_picture[i ]->data[0],
  1022. s->input_picture[i-1]->data[0], s->linesize) + 1;
  1023. }
  1024. }
  1025. for(i=0; i<s->max_b_frames; i++){
  1026. if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
  1027. }
  1028. b_frames= FFMAX(0, i-1);
  1029. /* reset scores */
  1030. for(i=0; i<b_frames+1; i++){
  1031. s->input_picture[i]->b_frame_score=0;
  1032. }
  1033. }else{
  1034. fprintf(stderr, "illegal b frame strategy\n");
  1035. b_frames=0;
  1036. }
  1037. emms_c();
  1038. //static int b_count=0;
  1039. //b_count+= b_frames;
  1040. //printf("b_frames: %d\n", b_count);
  1041. s->reordered_input_picture[0]= s->input_picture[b_frames];
  1042. if( s->picture_in_gop_number + b_frames >= s->gop_size
  1043. || s->reordered_input_picture[0]->pict_type== I_TYPE)
  1044. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1045. else
  1046. s->reordered_input_picture[0]->pict_type= P_TYPE;
  1047. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  1048. for(i=0; i<b_frames; i++){
  1049. coded_pic_num++;
  1050. s->reordered_input_picture[i+1]= s->input_picture[i];
  1051. s->reordered_input_picture[i+1]->pict_type= B_TYPE;
  1052. s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
  1053. }
  1054. }
  1055. }
  1056. if(s->reordered_input_picture[0]){
  1057. s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
  1058. s->new_picture= *s->reordered_input_picture[0];
  1059. if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
  1060. // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
  1061. int i= find_unused_picture(s, 0);
  1062. Picture *pic= &s->picture[i];
  1063. /* mark us unused / free shared pic */
  1064. for(i=0; i<4; i++)
  1065. s->reordered_input_picture[0]->data[i]= NULL;
  1066. s->reordered_input_picture[0]->type= 0;
  1067. //FIXME bad, copy * except
  1068. pic->pict_type = s->reordered_input_picture[0]->pict_type;
  1069. pic->quality = s->reordered_input_picture[0]->quality;
  1070. pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
  1071. pic->reference = s->reordered_input_picture[0]->reference;
  1072. alloc_picture(s, pic, 0);
  1073. s->current_picture_ptr= pic;
  1074. }else{
  1075. // input is not a shared pix -> reuse buffer for current_pix
  1076. assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
  1077. || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  1078. s->current_picture_ptr= s->reordered_input_picture[0];
  1079. for(i=0; i<4; i++){
  1080. //reverse the +16 we did before storing the input
  1081. s->current_picture_ptr->data[i]-=16;
  1082. }
  1083. }
  1084. s->current_picture= *s->current_picture_ptr;
  1085. s->picture_number= s->new_picture.display_picture_number;
  1086. //printf("dpn:%d\n", s->picture_number);
  1087. }else{
  1088. memset(&s->new_picture, 0, sizeof(Picture));
  1089. }
  1090. }
  1091. int MPV_encode_picture(AVCodecContext *avctx,
  1092. unsigned char *buf, int buf_size, void *data)
  1093. {
  1094. MpegEncContext *s = avctx->priv_data;
  1095. AVFrame *pic_arg = data;
  1096. int i;
  1097. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  1098. s->picture_in_gop_number++;
  1099. load_input_picture(s, pic_arg);
  1100. select_input_picture(s);
  1101. /* output? */
  1102. if(s->new_picture.data[0]){
  1103. s->pict_type= s->new_picture.pict_type;
  1104. if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
  1105. s->qscale= (int)(s->new_picture.quality+0.5);
  1106. assert(s->qscale);
  1107. }
  1108. //emms_c();
  1109. //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
  1110. MPV_frame_start(s, avctx);
  1111. encode_picture(s, s->picture_number);
  1112. avctx->real_pict_num = s->picture_number;
  1113. avctx->header_bits = s->header_bits;
  1114. avctx->mv_bits = s->mv_bits;
  1115. avctx->misc_bits = s->misc_bits;
  1116. avctx->i_tex_bits = s->i_tex_bits;
  1117. avctx->p_tex_bits = s->p_tex_bits;
  1118. avctx->i_count = s->i_count;
  1119. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  1120. avctx->skip_count = s->skip_count;
  1121. MPV_frame_end(s);
  1122. if (s->out_format == FMT_MJPEG)
  1123. mjpeg_picture_trailer(s);
  1124. if(s->flags&CODEC_FLAG_PASS1)
  1125. ff_write_pass1_stats(s);
  1126. for(i=0; i<4; i++){
  1127. avctx->error[i] += s->current_picture_ptr->error[i];
  1128. }
  1129. }
  1130. s->input_picture_number++;
  1131. flush_put_bits(&s->pb);
  1132. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  1133. s->total_bits += s->frame_bits;
  1134. avctx->frame_bits = s->frame_bits;
  1135. return pbBufPtr(&s->pb) - s->pb.buf;
  1136. }
  1137. #endif //CONFIG_ENCODERS
  1138. static inline void gmc1_motion(MpegEncContext *s,
  1139. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1140. int dest_offset,
  1141. uint8_t **ref_picture, int src_offset)
  1142. {
  1143. uint8_t *ptr;
  1144. int offset, src_x, src_y, linesize, uvlinesize;
  1145. int motion_x, motion_y;
  1146. int emu=0;
  1147. motion_x= s->sprite_offset[0][0];
  1148. motion_y= s->sprite_offset[0][1];
  1149. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  1150. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  1151. motion_x<<=(3-s->sprite_warping_accuracy);
  1152. motion_y<<=(3-s->sprite_warping_accuracy);
  1153. src_x = clip(src_x, -16, s->width);
  1154. if (src_x == s->width)
  1155. motion_x =0;
  1156. src_y = clip(src_y, -16, s->height);
  1157. if (src_y == s->height)
  1158. motion_y =0;
  1159. linesize = s->linesize;
  1160. uvlinesize = s->uvlinesize;
  1161. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1162. dest_y+=dest_offset;
  1163. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1164. if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
  1165. || src_y + 17 >= s->v_edge_pos){
  1166. ff_emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1167. ptr= s->edge_emu_buffer;
  1168. }
  1169. }
  1170. if((motion_x|motion_y)&7){
  1171. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1172. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1173. }else{
  1174. int dxy;
  1175. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  1176. if (s->no_rounding){
  1177. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  1178. }else{
  1179. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  1180. }
  1181. }
  1182. if(s->flags&CODEC_FLAG_GRAY) return;
  1183. motion_x= s->sprite_offset[1][0];
  1184. motion_y= s->sprite_offset[1][1];
  1185. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  1186. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  1187. motion_x<<=(3-s->sprite_warping_accuracy);
  1188. motion_y<<=(3-s->sprite_warping_accuracy);
  1189. src_x = clip(src_x, -8, s->width>>1);
  1190. if (src_x == s->width>>1)
  1191. motion_x =0;
  1192. src_y = clip(src_y, -8, s->height>>1);
  1193. if (src_y == s->height>>1)
  1194. motion_y =0;
  1195. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  1196. ptr = ref_picture[1] + offset;
  1197. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1198. if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
  1199. || src_y + 9 >= s->v_edge_pos>>1){
  1200. ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1201. ptr= s->edge_emu_buffer;
  1202. emu=1;
  1203. }
  1204. }
  1205. s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1206. ptr = ref_picture[2] + offset;
  1207. if(emu){
  1208. ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1209. ptr= s->edge_emu_buffer;
  1210. }
  1211. s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1212. return;
  1213. }
  1214. static inline void gmc_motion(MpegEncContext *s,
  1215. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1216. int dest_offset,
  1217. uint8_t **ref_picture, int src_offset)
  1218. {
  1219. uint8_t *ptr;
  1220. int linesize, uvlinesize;
  1221. const int a= s->sprite_warping_accuracy;
  1222. int ox, oy;
  1223. linesize = s->linesize;
  1224. uvlinesize = s->uvlinesize;
  1225. ptr = ref_picture[0] + src_offset;
  1226. dest_y+=dest_offset;
  1227. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  1228. oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
  1229. s->dsp.gmc(dest_y, ptr, linesize, 16,
  1230. ox,
  1231. oy,
  1232. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1233. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1234. a+1, (1<<(2*a+1)) - s->no_rounding,
  1235. s->h_edge_pos, s->v_edge_pos);
  1236. s->dsp.gmc(dest_y+8, ptr, linesize, 16,
  1237. ox + s->sprite_delta[0][0]*8,
  1238. oy + s->sprite_delta[1][0]*8,
  1239. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1240. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1241. a+1, (1<<(2*a+1)) - s->no_rounding,
  1242. s->h_edge_pos, s->v_edge_pos);
  1243. if(s->flags&CODEC_FLAG_GRAY) return;
  1244. dest_cb+=dest_offset>>1;
  1245. dest_cr+=dest_offset>>1;
  1246. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  1247. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  1248. ptr = ref_picture[1] + (src_offset>>1);
  1249. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  1250. ox,
  1251. oy,
  1252. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1253. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1254. a+1, (1<<(2*a+1)) - s->no_rounding,
  1255. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1256. ptr = ref_picture[2] + (src_offset>>1);
  1257. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  1258. ox,
  1259. oy,
  1260. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1261. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1262. a+1, (1<<(2*a+1)) - s->no_rounding,
  1263. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1264. }
  1265. void ff_emulated_edge_mc(MpegEncContext *s, uint8_t *src, int linesize, int block_w, int block_h,
  1266. int src_x, int src_y, int w, int h){
  1267. int x, y;
  1268. int start_y, start_x, end_y, end_x;
  1269. uint8_t *buf= s->edge_emu_buffer;
  1270. if(src_y>= h){
  1271. src+= (h-1-src_y)*linesize;
  1272. src_y=h-1;
  1273. }else if(src_y<=-block_h){
  1274. src+= (1-block_h-src_y)*linesize;
  1275. src_y=1-block_h;
  1276. }
  1277. if(src_x>= w){
  1278. src+= (w-1-src_x);
  1279. src_x=w-1;
  1280. }else if(src_x<=-block_w){
  1281. src+= (1-block_w-src_x);
  1282. src_x=1-block_w;
  1283. }
  1284. start_y= FFMAX(0, -src_y);
  1285. start_x= FFMAX(0, -src_x);
  1286. end_y= FFMIN(block_h, h-src_y);
  1287. end_x= FFMIN(block_w, w-src_x);
  1288. // copy existing part
  1289. for(y=start_y; y<end_y; y++){
  1290. for(x=start_x; x<end_x; x++){
  1291. buf[x + y*linesize]= src[x + y*linesize];
  1292. }
  1293. }
  1294. //top
  1295. for(y=0; y<start_y; y++){
  1296. for(x=start_x; x<end_x; x++){
  1297. buf[x + y*linesize]= buf[x + start_y*linesize];
  1298. }
  1299. }
  1300. //bottom
  1301. for(y=end_y; y<block_h; y++){
  1302. for(x=start_x; x<end_x; x++){
  1303. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  1304. }
  1305. }
  1306. for(y=0; y<block_h; y++){
  1307. //left
  1308. for(x=0; x<start_x; x++){
  1309. buf[x + y*linesize]= buf[start_x + y*linesize];
  1310. }
  1311. //right
  1312. for(x=end_x; x<block_w; x++){
  1313. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  1314. }
  1315. }
  1316. }
  1317. /* apply one mpeg motion vector to the three components */
  1318. static inline void mpeg_motion(MpegEncContext *s,
  1319. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1320. int dest_offset,
  1321. uint8_t **ref_picture, int src_offset,
  1322. int field_based, op_pixels_func (*pix_op)[4],
  1323. int motion_x, int motion_y, int h)
  1324. {
  1325. uint8_t *ptr;
  1326. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1327. int emu=0;
  1328. #if 0
  1329. if(s->quarter_sample)
  1330. {
  1331. motion_x>>=1;
  1332. motion_y>>=1;
  1333. }
  1334. #endif
  1335. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1336. src_x = s->mb_x * 16 + (motion_x >> 1);
  1337. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  1338. /* WARNING: do no forget half pels */
  1339. height = s->height >> field_based;
  1340. v_edge_pos = s->v_edge_pos >> field_based;
  1341. src_x = clip(src_x, -16, s->width);
  1342. if (src_x == s->width)
  1343. dxy &= ~1;
  1344. src_y = clip(src_y, -16, height);
  1345. if (src_y == height)
  1346. dxy &= ~2;
  1347. linesize = s->current_picture.linesize[0] << field_based;
  1348. uvlinesize = s->current_picture.linesize[1] << field_based;
  1349. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  1350. dest_y += dest_offset;
  1351. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1352. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
  1353. || src_y + (motion_y&1) + h > v_edge_pos){
  1354. ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, //FIXME linesize? and uv below
  1355. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1356. ptr= s->edge_emu_buffer + src_offset;
  1357. emu=1;
  1358. }
  1359. }
  1360. pix_op[0][dxy](dest_y, ptr, linesize, h);
  1361. if(s->flags&CODEC_FLAG_GRAY) return;
  1362. if (s->out_format == FMT_H263) {
  1363. dxy = 0;
  1364. if ((motion_x & 3) != 0)
  1365. dxy |= 1;
  1366. if ((motion_y & 3) != 0)
  1367. dxy |= 2;
  1368. mx = motion_x >> 2;
  1369. my = motion_y >> 2;
  1370. } else {
  1371. mx = motion_x / 2;
  1372. my = motion_y / 2;
  1373. dxy = ((my & 1) << 1) | (mx & 1);
  1374. mx >>= 1;
  1375. my >>= 1;
  1376. }
  1377. src_x = s->mb_x * 8 + mx;
  1378. src_y = s->mb_y * (8 >> field_based) + my;
  1379. src_x = clip(src_x, -8, s->width >> 1);
  1380. if (src_x == (s->width >> 1))
  1381. dxy &= ~1;
  1382. src_y = clip(src_y, -8, height >> 1);
  1383. if (src_y == (height >> 1))
  1384. dxy &= ~2;
  1385. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1386. ptr = ref_picture[1] + offset;
  1387. if(emu){
  1388. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1389. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1390. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1391. }
  1392. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1393. ptr = ref_picture[2] + offset;
  1394. if(emu){
  1395. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1396. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1397. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1398. }
  1399. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1400. }
  1401. static inline void qpel_motion(MpegEncContext *s,
  1402. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1403. int dest_offset,
  1404. uint8_t **ref_picture, int src_offset,
  1405. int field_based, op_pixels_func (*pix_op)[4],
  1406. qpel_mc_func (*qpix_op)[16],
  1407. int motion_x, int motion_y, int h)
  1408. {
  1409. uint8_t *ptr;
  1410. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1411. int emu=0;
  1412. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1413. src_x = s->mb_x * 16 + (motion_x >> 2);
  1414. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  1415. height = s->height >> field_based;
  1416. v_edge_pos = s->v_edge_pos >> field_based;
  1417. src_x = clip(src_x, -16, s->width);
  1418. if (src_x == s->width)
  1419. dxy &= ~3;
  1420. src_y = clip(src_y, -16, height);
  1421. if (src_y == height)
  1422. dxy &= ~12;
  1423. linesize = s->linesize << field_based;
  1424. uvlinesize = s->uvlinesize << field_based;
  1425. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1426. dest_y += dest_offset;
  1427. //printf("%d %d %d\n", src_x, src_y, dxy);
  1428. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1429. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
  1430. || src_y + (motion_y&3) + h > v_edge_pos){
  1431. ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
  1432. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1433. ptr= s->edge_emu_buffer + src_offset;
  1434. emu=1;
  1435. }
  1436. }
  1437. if(!field_based)
  1438. qpix_op[0][dxy](dest_y, ptr, linesize);
  1439. else{
  1440. //damn interlaced mode
  1441. //FIXME boundary mirroring is not exactly correct here
  1442. qpix_op[1][dxy](dest_y , ptr , linesize);
  1443. qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
  1444. }
  1445. if(s->flags&CODEC_FLAG_GRAY) return;
  1446. if(field_based){
  1447. mx= motion_x/2;
  1448. my= motion_y>>1;
  1449. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  1450. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  1451. mx= (motion_x>>1) + rtab[motion_x&7];
  1452. my= (motion_y>>1) + rtab[motion_y&7];
  1453. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  1454. mx= (motion_x>>1)|(motion_x&1);
  1455. my= (motion_y>>1)|(motion_y&1);
  1456. }else{
  1457. mx= motion_x/2;
  1458. my= motion_y/2;
  1459. }
  1460. mx= (mx>>1)|(mx&1);
  1461. my= (my>>1)|(my&1);
  1462. dxy= (mx&1) | ((my&1)<<1);
  1463. mx>>=1;
  1464. my>>=1;
  1465. src_x = s->mb_x * 8 + mx;
  1466. src_y = s->mb_y * (8 >> field_based) + my;
  1467. src_x = clip(src_x, -8, s->width >> 1);
  1468. if (src_x == (s->width >> 1))
  1469. dxy &= ~1;
  1470. src_y = clip(src_y, -8, height >> 1);
  1471. if (src_y == (height >> 1))
  1472. dxy &= ~2;
  1473. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1474. ptr = ref_picture[1] + offset;
  1475. if(emu){
  1476. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1477. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1478. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1479. }
  1480. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1481. ptr = ref_picture[2] + offset;
  1482. if(emu){
  1483. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1484. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1485. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1486. }
  1487. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1488. }
  1489. inline int ff_h263_round_chroma(int x){
  1490. if (x >= 0)
  1491. return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  1492. else {
  1493. x = -x;
  1494. return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  1495. }
  1496. }
  1497. static inline void MPV_motion(MpegEncContext *s,
  1498. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1499. int dir, uint8_t **ref_picture,
  1500. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  1501. {
  1502. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  1503. int mb_x, mb_y, i;
  1504. uint8_t *ptr, *dest;
  1505. int emu=0;
  1506. mb_x = s->mb_x;
  1507. mb_y = s->mb_y;
  1508. switch(s->mv_type) {
  1509. case MV_TYPE_16X16:
  1510. #ifdef CONFIG_RISKY
  1511. if(s->mcsel){
  1512. if(s->real_sprite_warping_points==1){
  1513. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  1514. ref_picture, 0);
  1515. }else{
  1516. gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
  1517. ref_picture, 0);
  1518. }
  1519. }else if(s->quarter_sample){
  1520. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1521. ref_picture, 0,
  1522. 0, pix_op, qpix_op,
  1523. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1524. }else if(s->mspel){
  1525. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  1526. ref_picture, pix_op,
  1527. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1528. }else
  1529. #endif
  1530. {
  1531. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1532. ref_picture, 0,
  1533. 0, pix_op,
  1534. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1535. }
  1536. break;
  1537. case MV_TYPE_8X8:
  1538. mx = 0;
  1539. my = 0;
  1540. if(s->quarter_sample){
  1541. for(i=0;i<4;i++) {
  1542. motion_x = s->mv[dir][i][0];
  1543. motion_y = s->mv[dir][i][1];
  1544. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1545. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  1546. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  1547. /* WARNING: do no forget half pels */
  1548. src_x = clip(src_x, -16, s->width);
  1549. if (src_x == s->width)
  1550. dxy &= ~3;
  1551. src_y = clip(src_y, -16, s->height);
  1552. if (src_y == s->height)
  1553. dxy &= ~12;
  1554. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1555. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1556. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
  1557. || src_y + (motion_y&3) + 8 > s->v_edge_pos){
  1558. ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1559. ptr= s->edge_emu_buffer;
  1560. }
  1561. }
  1562. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1563. qpix_op[1][dxy](dest, ptr, s->linesize);
  1564. mx += s->mv[dir][i][0]/2;
  1565. my += s->mv[dir][i][1]/2;
  1566. }
  1567. }else{
  1568. for(i=0;i<4;i++) {
  1569. motion_x = s->mv[dir][i][0];
  1570. motion_y = s->mv[dir][i][1];
  1571. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1572. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  1573. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  1574. /* WARNING: do no forget half pels */
  1575. src_x = clip(src_x, -16, s->width);
  1576. if (src_x == s->width)
  1577. dxy &= ~1;
  1578. src_y = clip(src_y, -16, s->height);
  1579. if (src_y == s->height)
  1580. dxy &= ~2;
  1581. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1582. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1583. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
  1584. || src_y + (motion_y&1) + 8 > s->v_edge_pos){
  1585. ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1586. ptr= s->edge_emu_buffer;
  1587. }
  1588. }
  1589. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1590. pix_op[1][dxy](dest, ptr, s->linesize, 8);
  1591. mx += s->mv[dir][i][0];
  1592. my += s->mv[dir][i][1];
  1593. }
  1594. }
  1595. if(s->flags&CODEC_FLAG_GRAY) break;
  1596. /* In case of 8X8, we construct a single chroma motion vector
  1597. with a special rounding */
  1598. mx= ff_h263_round_chroma(mx);
  1599. my= ff_h263_round_chroma(my);
  1600. dxy = ((my & 1) << 1) | (mx & 1);
  1601. mx >>= 1;
  1602. my >>= 1;
  1603. src_x = mb_x * 8 + mx;
  1604. src_y = mb_y * 8 + my;
  1605. src_x = clip(src_x, -8, s->width/2);
  1606. if (src_x == s->width/2)
  1607. dxy &= ~1;
  1608. src_y = clip(src_y, -8, s->height/2);
  1609. if (src_y == s->height/2)
  1610. dxy &= ~2;
  1611. offset = (src_y * (s->uvlinesize)) + src_x;
  1612. ptr = ref_picture[1] + offset;
  1613. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1614. if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
  1615. || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
  1616. ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1617. ptr= s->edge_emu_buffer;
  1618. emu=1;
  1619. }
  1620. }
  1621. pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
  1622. ptr = ref_picture[2] + offset;
  1623. if(emu){
  1624. ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1625. ptr= s->edge_emu_buffer;
  1626. }
  1627. pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
  1628. break;
  1629. case MV_TYPE_FIELD:
  1630. if (s->picture_structure == PICT_FRAME) {
  1631. if(s->quarter_sample){
  1632. /* top field */
  1633. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1634. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1635. 1, pix_op, qpix_op,
  1636. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1637. /* bottom field */
  1638. qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1639. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1640. 1, pix_op, qpix_op,
  1641. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1642. }else{
  1643. /* top field */
  1644. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1645. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1646. 1, pix_op,
  1647. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1648. /* bottom field */
  1649. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1650. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1651. 1, pix_op,
  1652. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1653. }
  1654. } else {
  1655. int offset;
  1656. if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
  1657. offset= s->field_select[dir][0] ? s->linesize : 0;
  1658. }else{
  1659. ref_picture= s->current_picture.data;
  1660. offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
  1661. }
  1662. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1663. ref_picture, offset,
  1664. 0, pix_op,
  1665. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1666. }
  1667. break;
  1668. }
  1669. }
  1670. /* put block[] to dest[] */
  1671. static inline void put_dct(MpegEncContext *s,
  1672. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1673. {
  1674. s->dct_unquantize(s, block, i, s->qscale);
  1675. s->dsp.idct_put (dest, line_size, block);
  1676. }
  1677. /* add block[] to dest[] */
  1678. static inline void add_dct(MpegEncContext *s,
  1679. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1680. {
  1681. if (s->block_last_index[i] >= 0) {
  1682. s->dsp.idct_add (dest, line_size, block);
  1683. }
  1684. }
  1685. static inline void add_dequant_dct(MpegEncContext *s,
  1686. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1687. {
  1688. if (s->block_last_index[i] >= 0) {
  1689. s->dct_unquantize(s, block, i, s->qscale);
  1690. s->dsp.idct_add (dest, line_size, block);
  1691. }
  1692. }
  1693. /**
  1694. * cleans dc, ac, coded_block for the current non intra MB
  1695. */
  1696. void ff_clean_intra_table_entries(MpegEncContext *s)
  1697. {
  1698. int wrap = s->block_wrap[0];
  1699. int xy = s->block_index[0];
  1700. s->dc_val[0][xy ] =
  1701. s->dc_val[0][xy + 1 ] =
  1702. s->dc_val[0][xy + wrap] =
  1703. s->dc_val[0][xy + 1 + wrap] = 1024;
  1704. /* ac pred */
  1705. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1706. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1707. if (s->msmpeg4_version>=3) {
  1708. s->coded_block[xy ] =
  1709. s->coded_block[xy + 1 ] =
  1710. s->coded_block[xy + wrap] =
  1711. s->coded_block[xy + 1 + wrap] = 0;
  1712. }
  1713. /* chroma */
  1714. wrap = s->block_wrap[4];
  1715. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  1716. s->dc_val[1][xy] =
  1717. s->dc_val[2][xy] = 1024;
  1718. /* ac pred */
  1719. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1720. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1721. s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
  1722. }
  1723. /* generic function called after a macroblock has been parsed by the
  1724. decoder or after it has been encoded by the encoder.
  1725. Important variables used:
  1726. s->mb_intra : true if intra macroblock
  1727. s->mv_dir : motion vector direction
  1728. s->mv_type : motion vector type
  1729. s->mv : motion vector
  1730. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1731. */
  1732. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  1733. {
  1734. int mb_x, mb_y;
  1735. const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
  1736. mb_x = s->mb_x;
  1737. mb_y = s->mb_y;
  1738. s->current_picture.qscale_table[mb_xy]= s->qscale;
  1739. /* update DC predictors for P macroblocks */
  1740. if (!s->mb_intra) {
  1741. if (s->h263_pred || s->h263_aic) {
  1742. if(s->mbintra_table[mb_xy])
  1743. ff_clean_intra_table_entries(s);
  1744. } else {
  1745. s->last_dc[0] =
  1746. s->last_dc[1] =
  1747. s->last_dc[2] = 128 << s->intra_dc_precision;
  1748. }
  1749. }
  1750. else if (s->h263_pred || s->h263_aic)
  1751. s->mbintra_table[mb_xy]=1;
  1752. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1753. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1754. //FIXME a lot of thet is only needed for !low_delay
  1755. const int wrap = s->block_wrap[0];
  1756. const int xy = s->block_index[0];
  1757. const int mb_index= s->mb_x + s->mb_y*s->mb_width;
  1758. if(s->mv_type == MV_TYPE_8X8){
  1759. s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
  1760. } else {
  1761. int motion_x, motion_y;
  1762. if (s->mb_intra) {
  1763. motion_x = 0;
  1764. motion_y = 0;
  1765. if(s->co_located_type_table)
  1766. s->co_located_type_table[mb_index]= 0;
  1767. } else if (s->mv_type == MV_TYPE_16X16) {
  1768. motion_x = s->mv[0][0][0];
  1769. motion_y = s->mv[0][0][1];
  1770. if(s->co_located_type_table)
  1771. s->co_located_type_table[mb_index]= 0;
  1772. } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
  1773. int i;
  1774. motion_x = s->mv[0][0][0] + s->mv[0][1][0];
  1775. motion_y = s->mv[0][0][1] + s->mv[0][1][1];
  1776. motion_x = (motion_x>>1) | (motion_x&1);
  1777. for(i=0; i<2; i++){
  1778. s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
  1779. s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
  1780. s->field_select_table[mb_index][i]= s->field_select[0][i];
  1781. }
  1782. s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
  1783. }
  1784. /* no update if 8X8 because it has been done during parsing */
  1785. s->motion_val[xy][0] = motion_x;
  1786. s->motion_val[xy][1] = motion_y;
  1787. s->motion_val[xy + 1][0] = motion_x;
  1788. s->motion_val[xy + 1][1] = motion_y;
  1789. s->motion_val[xy + wrap][0] = motion_x;
  1790. s->motion_val[xy + wrap][1] = motion_y;
  1791. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1792. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1793. }
  1794. }
  1795. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
  1796. uint8_t *dest_y, *dest_cb, *dest_cr;
  1797. int dct_linesize, dct_offset;
  1798. op_pixels_func (*op_pix)[4];
  1799. qpel_mc_func (*op_qpix)[16];
  1800. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  1801. const int uvlinesize= s->current_picture.linesize[1];
  1802. /* avoid copy if macroblock skipped in last frame too */
  1803. if (s->pict_type != B_TYPE) {
  1804. s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
  1805. }
  1806. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1807. if(!s->encoding){
  1808. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1809. const int age= s->current_picture.age;
  1810. assert(age);
  1811. if (s->mb_skiped) {
  1812. s->mb_skiped= 0;
  1813. assert(s->pict_type!=I_TYPE);
  1814. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  1815. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1816. /* if previous was skipped too, then nothing to do ! */
  1817. if (*mbskip_ptr >= age && s->current_picture.reference){
  1818. return;
  1819. }
  1820. } else if(!s->current_picture.reference){
  1821. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1822. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1823. } else{
  1824. *mbskip_ptr = 0; /* not skipped */
  1825. }
  1826. }else
  1827. s->mb_skiped= 0;
  1828. if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){ //FIXME precalc
  1829. dest_y = s->current_picture.data[0] + mb_x * 16;
  1830. dest_cb = s->current_picture.data[1] + mb_x * 8;
  1831. dest_cr = s->current_picture.data[2] + mb_x * 8;
  1832. }else{
  1833. dest_y = s->current_picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
  1834. dest_cb = s->current_picture.data[1] + (mb_y * 8 * uvlinesize) + mb_x * 8;
  1835. dest_cr = s->current_picture.data[2] + (mb_y * 8 * uvlinesize) + mb_x * 8;
  1836. }
  1837. if (s->interlaced_dct) {
  1838. dct_linesize = linesize * 2;
  1839. dct_offset = linesize;
  1840. } else {
  1841. dct_linesize = linesize;
  1842. dct_offset = linesize * 8;
  1843. }
  1844. if (!s->mb_intra) {
  1845. /* motion handling */
  1846. /* decoding or more than one mb_type (MC was allready done otherwise) */
  1847. if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
  1848. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1849. op_pix = s->dsp.put_pixels_tab;
  1850. op_qpix= s->dsp.put_qpel_pixels_tab;
  1851. }else{
  1852. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1853. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  1854. }
  1855. if (s->mv_dir & MV_DIR_FORWARD) {
  1856. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1857. op_pix = s->dsp.avg_pixels_tab;
  1858. op_qpix= s->dsp.avg_qpel_pixels_tab;
  1859. }
  1860. if (s->mv_dir & MV_DIR_BACKWARD) {
  1861. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1862. }
  1863. }
  1864. /* skip dequant / idct if we are really late ;) */
  1865. if(s->hurry_up>1) return;
  1866. /* add dct residue */
  1867. if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO
  1868. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1869. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
  1870. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1871. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1872. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1873. if(!(s->flags&CODEC_FLAG_GRAY)){
  1874. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize);
  1875. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize);
  1876. }
  1877. } else if(s->codec_id != CODEC_ID_WMV2){
  1878. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1879. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1880. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1881. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1882. if(!(s->flags&CODEC_FLAG_GRAY)){
  1883. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1884. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1885. }
  1886. }
  1887. #ifdef CONFIG_RISKY
  1888. else{
  1889. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1890. }
  1891. #endif
  1892. } else {
  1893. /* dct only in intra block */
  1894. if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
  1895. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1896. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1897. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1898. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1899. if(!(s->flags&CODEC_FLAG_GRAY)){
  1900. put_dct(s, block[4], 4, dest_cb, uvlinesize);
  1901. put_dct(s, block[5], 5, dest_cr, uvlinesize);
  1902. }
  1903. }else{
  1904. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1905. s->dsp.idct_put(dest_y + 8, dct_linesize, block[1]);
  1906. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1907. s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
  1908. if(!(s->flags&CODEC_FLAG_GRAY)){
  1909. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1910. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1911. }
  1912. }
  1913. }
  1914. }
  1915. }
  1916. #ifdef CONFIG_ENCODERS
  1917. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  1918. {
  1919. static const char tab[64]=
  1920. {3,2,2,1,1,1,1,1,
  1921. 1,1,1,1,1,1,1,1,
  1922. 1,1,1,1,1,1,1,1,
  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. 0,0,0,0,0,0,0,0,
  1927. 0,0,0,0,0,0,0,0};
  1928. int score=0;
  1929. int run=0;
  1930. int i;
  1931. DCTELEM *block= s->block[n];
  1932. const int last_index= s->block_last_index[n];
  1933. int skip_dc;
  1934. if(threshold<0){
  1935. skip_dc=0;
  1936. threshold= -threshold;
  1937. }else
  1938. skip_dc=1;
  1939. /* are all which we could set to zero are allready zero? */
  1940. if(last_index<=skip_dc - 1) return;
  1941. for(i=0; i<=last_index; i++){
  1942. const int j = s->intra_scantable.permutated[i];
  1943. const int level = ABS(block[j]);
  1944. if(level==1){
  1945. if(skip_dc && i==0) continue;
  1946. score+= tab[run];
  1947. run=0;
  1948. }else if(level>1){
  1949. return;
  1950. }else{
  1951. run++;
  1952. }
  1953. }
  1954. if(score >= threshold) return;
  1955. for(i=skip_dc; i<=last_index; i++){
  1956. const int j = s->intra_scantable.permutated[i];
  1957. block[j]=0;
  1958. }
  1959. if(block[0]) s->block_last_index[n]= 0;
  1960. else s->block_last_index[n]= -1;
  1961. }
  1962. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1963. {
  1964. int i;
  1965. const int maxlevel= s->max_qcoeff;
  1966. const int minlevel= s->min_qcoeff;
  1967. if(s->mb_intra){
  1968. i=1; //skip clipping of intra dc
  1969. }else
  1970. i=0;
  1971. for(;i<=last_index; i++){
  1972. const int j= s->intra_scantable.permutated[i];
  1973. int level = block[j];
  1974. if (level>maxlevel) level=maxlevel;
  1975. else if(level<minlevel) level=minlevel;
  1976. block[j]= level;
  1977. }
  1978. }
  1979. #if 0
  1980. static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
  1981. int score=0;
  1982. int x,y;
  1983. for(y=0; y<7; y++){
  1984. for(x=0; x<16; x+=4){
  1985. score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
  1986. +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
  1987. }
  1988. s+= stride;
  1989. }
  1990. return score;
  1991. }
  1992. static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
  1993. int score=0;
  1994. int x,y;
  1995. for(y=0; y<7; y++){
  1996. for(x=0; x<16; x++){
  1997. score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  1998. }
  1999. s1+= stride;
  2000. s2+= stride;
  2001. }
  2002. return score;
  2003. }
  2004. #else
  2005. #define SQ(a) ((a)*(a))
  2006. static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
  2007. int score=0;
  2008. int x,y;
  2009. for(y=0; y<7; y++){
  2010. for(x=0; x<16; x+=4){
  2011. score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
  2012. +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
  2013. }
  2014. s+= stride;
  2015. }
  2016. return score;
  2017. }
  2018. static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
  2019. int score=0;
  2020. int x,y;
  2021. for(y=0; y<7; y++){
  2022. for(x=0; x<16; x++){
  2023. score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2024. }
  2025. s1+= stride;
  2026. s2+= stride;
  2027. }
  2028. return score;
  2029. }
  2030. #endif
  2031. #endif //CONFIG_ENCODERS
  2032. /**
  2033. *
  2034. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2035. */
  2036. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2037. if ( s->avctx->draw_horiz_band
  2038. && (s->last_picture_ptr || s->low_delay) ) {
  2039. uint8_t *src_ptr[3];
  2040. int offset;
  2041. h= FFMIN(h, s->height - y);
  2042. if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME)
  2043. offset = 0;
  2044. else
  2045. offset = y * s->linesize;
  2046. if(s->pict_type==B_TYPE || s->low_delay){
  2047. src_ptr[0] = s->current_picture.data[0] + offset;
  2048. src_ptr[1] = s->current_picture.data[1] + (offset >> 2);
  2049. src_ptr[2] = s->current_picture.data[2] + (offset >> 2);
  2050. } else {
  2051. src_ptr[0] = s->last_picture.data[0] + offset;
  2052. src_ptr[1] = s->last_picture.data[1] + (offset >> 2);
  2053. src_ptr[2] = s->last_picture.data[2] + (offset >> 2);
  2054. }
  2055. emms_c();
  2056. s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
  2057. y, s->width, h);
  2058. }
  2059. }
  2060. #ifdef CONFIG_ENCODERS
  2061. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  2062. {
  2063. const int mb_x= s->mb_x;
  2064. const int mb_y= s->mb_y;
  2065. int i;
  2066. int skip_dct[6];
  2067. int dct_offset = s->linesize*8; //default for progressive frames
  2068. for(i=0; i<6; i++) skip_dct[i]=0;
  2069. if(s->adaptive_quant){
  2070. s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
  2071. if(s->out_format==FMT_H263){
  2072. if (s->dquant> 2) s->dquant= 2;
  2073. else if(s->dquant<-2) s->dquant=-2;
  2074. }
  2075. if(s->codec_id==CODEC_ID_MPEG4){
  2076. if(!s->mb_intra){
  2077. if(s->mv_dir&MV_DIRECT)
  2078. s->dquant=0;
  2079. assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
  2080. }
  2081. }
  2082. s->qscale+= s->dquant;
  2083. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  2084. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  2085. }
  2086. if (s->mb_intra) {
  2087. uint8_t *ptr;
  2088. int wrap_y;
  2089. int emu=0;
  2090. wrap_y = s->linesize;
  2091. ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2092. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2093. ff_emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2094. ptr= s->edge_emu_buffer;
  2095. emu=1;
  2096. }
  2097. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2098. int progressive_score, interlaced_score;
  2099. progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
  2100. interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
  2101. if(progressive_score > interlaced_score + 100){
  2102. s->interlaced_dct=1;
  2103. dct_offset= wrap_y;
  2104. wrap_y<<=1;
  2105. }else
  2106. s->interlaced_dct=0;
  2107. }
  2108. s->dsp.get_pixels(s->block[0], ptr , wrap_y);
  2109. s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
  2110. s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
  2111. s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
  2112. if(s->flags&CODEC_FLAG_GRAY){
  2113. skip_dct[4]= 1;
  2114. skip_dct[5]= 1;
  2115. }else{
  2116. int wrap_c = s->uvlinesize;
  2117. ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2118. if(emu){
  2119. ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2120. ptr= s->edge_emu_buffer;
  2121. }
  2122. s->dsp.get_pixels(s->block[4], ptr, wrap_c);
  2123. ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2124. if(emu){
  2125. ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2126. ptr= s->edge_emu_buffer;
  2127. }
  2128. s->dsp.get_pixels(s->block[5], ptr, wrap_c);
  2129. }
  2130. }else{
  2131. op_pixels_func (*op_pix)[4];
  2132. qpel_mc_func (*op_qpix)[16];
  2133. uint8_t *dest_y, *dest_cb, *dest_cr;
  2134. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2135. int wrap_y, wrap_c;
  2136. int emu=0;
  2137. dest_y = s->current_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  2138. dest_cb = s->current_picture.data[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2139. dest_cr = s->current_picture.data[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2140. wrap_y = s->linesize;
  2141. wrap_c = s->uvlinesize;
  2142. ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2143. ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2144. ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2145. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2146. op_pix = s->dsp.put_pixels_tab;
  2147. op_qpix= s->dsp.put_qpel_pixels_tab;
  2148. }else{
  2149. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2150. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2151. }
  2152. if (s->mv_dir & MV_DIR_FORWARD) {
  2153. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2154. op_pix = s->dsp.avg_pixels_tab;
  2155. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2156. }
  2157. if (s->mv_dir & MV_DIR_BACKWARD) {
  2158. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2159. }
  2160. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2161. ff_emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2162. ptr_y= s->edge_emu_buffer;
  2163. emu=1;
  2164. }
  2165. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2166. int progressive_score, interlaced_score;
  2167. progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
  2168. + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
  2169. interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
  2170. + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
  2171. if(progressive_score > interlaced_score + 600){
  2172. s->interlaced_dct=1;
  2173. dct_offset= wrap_y;
  2174. wrap_y<<=1;
  2175. }else
  2176. s->interlaced_dct=0;
  2177. }
  2178. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  2179. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  2180. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  2181. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  2182. if(s->flags&CODEC_FLAG_GRAY){
  2183. skip_dct[4]= 1;
  2184. skip_dct[5]= 1;
  2185. }else{
  2186. if(emu){
  2187. ff_emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2188. ptr_cb= s->edge_emu_buffer;
  2189. }
  2190. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  2191. if(emu){
  2192. ff_emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2193. ptr_cr= s->edge_emu_buffer;
  2194. }
  2195. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  2196. }
  2197. /* pre quantization */
  2198. if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
  2199. //FIXME optimize
  2200. if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  2201. if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  2202. if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  2203. if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  2204. if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
  2205. if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
  2206. #if 0
  2207. {
  2208. static int stat[7];
  2209. int num=0;
  2210. for(i=0; i<6; i++)
  2211. if(skip_dct[i]) num++;
  2212. stat[num]++;
  2213. if(s->mb_x==0 && s->mb_y==0){
  2214. for(i=0; i<7; i++){
  2215. printf("%6d %1d\n", stat[i], i);
  2216. }
  2217. }
  2218. }
  2219. #endif
  2220. }
  2221. }
  2222. #if 0
  2223. {
  2224. float adap_parm;
  2225. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  2226. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  2227. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  2228. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  2229. s->qscale, adap_parm, s->qscale*adap_parm,
  2230. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  2231. }
  2232. #endif
  2233. /* DCT & quantize */
  2234. if(s->out_format==FMT_MJPEG){
  2235. for(i=0;i<6;i++) {
  2236. int overflow;
  2237. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
  2238. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2239. }
  2240. }else{
  2241. for(i=0;i<6;i++) {
  2242. if(!skip_dct[i]){
  2243. int overflow;
  2244. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  2245. // FIXME we could decide to change to quantizer instead of clipping
  2246. // JS: I don't think that would be a good idea it could lower quality instead
  2247. // of improve it. Just INTRADC clipping deserves changes in quantizer
  2248. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2249. }else
  2250. s->block_last_index[i]= -1;
  2251. }
  2252. if(s->luma_elim_threshold && !s->mb_intra)
  2253. for(i=0; i<4; i++)
  2254. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  2255. if(s->chroma_elim_threshold && !s->mb_intra)
  2256. for(i=4; i<6; i++)
  2257. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  2258. }
  2259. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  2260. s->block_last_index[4]=
  2261. s->block_last_index[5]= 0;
  2262. s->block[4][0]=
  2263. s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  2264. }
  2265. /* huffman encode */
  2266. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  2267. case CODEC_ID_MPEG1VIDEO:
  2268. mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
  2269. #ifdef CONFIG_RISKY
  2270. case CODEC_ID_MPEG4:
  2271. mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2272. case CODEC_ID_MSMPEG4V2:
  2273. case CODEC_ID_MSMPEG4V3:
  2274. case CODEC_ID_WMV1:
  2275. msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2276. case CODEC_ID_WMV2:
  2277. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
  2278. case CODEC_ID_H263:
  2279. case CODEC_ID_H263P:
  2280. case CODEC_ID_RV10:
  2281. h263_encode_mb(s, s->block, motion_x, motion_y); break;
  2282. #endif
  2283. case CODEC_ID_MJPEG:
  2284. mjpeg_encode_mb(s, s->block); break;
  2285. default:
  2286. assert(0);
  2287. }
  2288. }
  2289. #endif //CONFIG_ENCODERS
  2290. /**
  2291. * combines the (truncated) bitstream to a complete frame
  2292. * @returns -1 if no complete frame could be created
  2293. */
  2294. int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
  2295. ParseContext *pc= &s->parse_context;
  2296. pc->last_index= pc->index;
  2297. if(next==-1){
  2298. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  2299. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  2300. pc->index += *buf_size;
  2301. return -1;
  2302. }
  2303. if(pc->index){
  2304. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  2305. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  2306. pc->index = 0;
  2307. *buf= pc->buffer;
  2308. *buf_size= pc->last_index + next;
  2309. }
  2310. return 0;
  2311. }
  2312. #ifdef CONFIG_ENCODERS
  2313. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
  2314. {
  2315. int bytes= length>>4;
  2316. int bits= length&15;
  2317. int i;
  2318. if(length==0) return;
  2319. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  2320. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  2321. }
  2322. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2323. int i;
  2324. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2325. /* mpeg1 */
  2326. d->mb_skip_run= s->mb_skip_run;
  2327. for(i=0; i<3; i++)
  2328. d->last_dc[i]= s->last_dc[i];
  2329. /* statistics */
  2330. d->mv_bits= s->mv_bits;
  2331. d->i_tex_bits= s->i_tex_bits;
  2332. d->p_tex_bits= s->p_tex_bits;
  2333. d->i_count= s->i_count;
  2334. d->f_count= s->f_count;
  2335. d->b_count= s->b_count;
  2336. d->skip_count= s->skip_count;
  2337. d->misc_bits= s->misc_bits;
  2338. d->last_bits= 0;
  2339. d->mb_skiped= s->mb_skiped;
  2340. d->qscale= s->qscale;
  2341. }
  2342. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2343. int i;
  2344. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  2345. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2346. /* mpeg1 */
  2347. d->mb_skip_run= s->mb_skip_run;
  2348. for(i=0; i<3; i++)
  2349. d->last_dc[i]= s->last_dc[i];
  2350. /* statistics */
  2351. d->mv_bits= s->mv_bits;
  2352. d->i_tex_bits= s->i_tex_bits;
  2353. d->p_tex_bits= s->p_tex_bits;
  2354. d->i_count= s->i_count;
  2355. d->f_count= s->f_count;
  2356. d->b_count= s->b_count;
  2357. d->skip_count= s->skip_count;
  2358. d->misc_bits= s->misc_bits;
  2359. d->mb_intra= s->mb_intra;
  2360. d->mb_skiped= s->mb_skiped;
  2361. d->mv_type= s->mv_type;
  2362. d->mv_dir= s->mv_dir;
  2363. d->pb= s->pb;
  2364. if(s->data_partitioning){
  2365. d->pb2= s->pb2;
  2366. d->tex_pb= s->tex_pb;
  2367. }
  2368. d->block= s->block;
  2369. for(i=0; i<6; i++)
  2370. d->block_last_index[i]= s->block_last_index[i];
  2371. d->interlaced_dct= s->interlaced_dct;
  2372. d->qscale= s->qscale;
  2373. }
  2374. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  2375. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  2376. int *dmin, int *next_block, int motion_x, int motion_y)
  2377. {
  2378. int bits_count;
  2379. copy_context_before_encode(s, backup, type);
  2380. s->block= s->blocks[*next_block];
  2381. s->pb= pb[*next_block];
  2382. if(s->data_partitioning){
  2383. s->pb2 = pb2 [*next_block];
  2384. s->tex_pb= tex_pb[*next_block];
  2385. }
  2386. encode_mb(s, motion_x, motion_y);
  2387. bits_count= get_bit_count(&s->pb);
  2388. if(s->data_partitioning){
  2389. bits_count+= get_bit_count(&s->pb2);
  2390. bits_count+= get_bit_count(&s->tex_pb);
  2391. }
  2392. if(bits_count<*dmin){
  2393. *dmin= bits_count;
  2394. *next_block^=1;
  2395. copy_context_after_encode(best, s, type);
  2396. }
  2397. }
  2398. static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  2399. uint32_t *sq = squareTbl + 256;
  2400. int acc=0;
  2401. int x,y;
  2402. if(w==16 && h==16)
  2403. return s->dsp.sse[0](NULL, src1, src2, stride);
  2404. else if(w==8 && h==8)
  2405. return s->dsp.sse[1](NULL, src1, src2, stride);
  2406. for(y=0; y<h; y++){
  2407. for(x=0; x<w; x++){
  2408. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  2409. }
  2410. }
  2411. assert(acc>=0);
  2412. return acc;
  2413. }
  2414. static void encode_picture(MpegEncContext *s, int picture_number)
  2415. {
  2416. int mb_x, mb_y, pdif = 0;
  2417. int i;
  2418. int bits;
  2419. MpegEncContext best_s, backup_s;
  2420. uint8_t bit_buf[2][3000];
  2421. uint8_t bit_buf2[2][3000];
  2422. uint8_t bit_buf_tex[2][3000];
  2423. PutBitContext pb[2], pb2[2], tex_pb[2];
  2424. for(i=0; i<2; i++){
  2425. init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
  2426. init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
  2427. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
  2428. }
  2429. s->picture_number = picture_number;
  2430. s->block_wrap[0]=
  2431. s->block_wrap[1]=
  2432. s->block_wrap[2]=
  2433. s->block_wrap[3]= s->mb_width*2 + 2;
  2434. s->block_wrap[4]=
  2435. s->block_wrap[5]= s->mb_width + 2;
  2436. /* Reset the average MB variance */
  2437. s->current_picture.mb_var_sum = 0;
  2438. s->current_picture.mc_mb_var_sum = 0;
  2439. #ifdef CONFIG_RISKY
  2440. /* we need to initialize some time vars before we can encode b-frames */
  2441. // RAL: Condition added for MPEG1VIDEO
  2442. if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
  2443. ff_set_mpeg4_time(s, s->picture_number);
  2444. #endif
  2445. s->scene_change_score=0;
  2446. s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
  2447. if(s->pict_type==I_TYPE){
  2448. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  2449. else s->no_rounding=0;
  2450. }else if(s->pict_type!=B_TYPE){
  2451. if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
  2452. s->no_rounding ^= 1;
  2453. }
  2454. /* Estimate motion for every MB */
  2455. s->mb_intra=0; //for the rate distoration & bit compare functions
  2456. if(s->pict_type != I_TYPE){
  2457. if(s->pict_type != B_TYPE){
  2458. if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
  2459. s->me.pre_pass=1;
  2460. s->me.dia_size= s->avctx->pre_dia_size;
  2461. for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
  2462. for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
  2463. s->mb_x = mb_x;
  2464. s->mb_y = mb_y;
  2465. ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
  2466. }
  2467. }
  2468. s->me.pre_pass=0;
  2469. }
  2470. }
  2471. s->me.dia_size= s->avctx->dia_size;
  2472. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2473. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  2474. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  2475. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  2476. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  2477. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2478. s->mb_x = mb_x;
  2479. s->mb_y = mb_y;
  2480. s->block_index[0]+=2;
  2481. s->block_index[1]+=2;
  2482. s->block_index[2]+=2;
  2483. s->block_index[3]+=2;
  2484. /* compute motion vector & mb_type and store in context */
  2485. if(s->pict_type==B_TYPE)
  2486. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  2487. else
  2488. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  2489. }
  2490. }
  2491. }else /* if(s->pict_type == I_TYPE) */{
  2492. /* I-Frame */
  2493. //FIXME do we need to zero them?
  2494. memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  2495. memset(s->p_mv_table , 0, sizeof(int16_t)*(s->mb_width+2)*(s->mb_height+2)*2);
  2496. memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
  2497. if(!s->fixed_qscale){
  2498. /* finding spatial complexity for I-frame rate control */
  2499. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2500. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2501. int xx = mb_x * 16;
  2502. int yy = mb_y * 16;
  2503. uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  2504. int varc;
  2505. int sum = s->dsp.pix_sum(pix, s->linesize);
  2506. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  2507. s->current_picture.mb_var [s->mb_width * mb_y + mb_x] = varc;
  2508. s->current_picture.mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8;
  2509. s->current_picture.mb_var_sum += varc;
  2510. }
  2511. }
  2512. }
  2513. }
  2514. emms_c();
  2515. if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
  2516. s->pict_type= I_TYPE;
  2517. memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
  2518. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  2519. }
  2520. if(!s->umvplus){
  2521. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
  2522. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  2523. ff_fix_long_p_mvs(s);
  2524. }
  2525. if(s->pict_type==B_TYPE){
  2526. int a, b;
  2527. a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  2528. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
  2529. s->f_code = FFMAX(a, b);
  2530. a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  2531. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
  2532. s->b_code = FFMAX(a, b);
  2533. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  2534. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  2535. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  2536. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  2537. }
  2538. }
  2539. if (s->fixed_qscale)
  2540. s->frame_qscale = s->current_picture.quality;
  2541. else
  2542. s->frame_qscale = ff_rate_estimate_qscale(s);
  2543. if(s->adaptive_quant){
  2544. #ifdef CONFIG_RISKY
  2545. switch(s->codec_id){
  2546. case CODEC_ID_MPEG4:
  2547. ff_clean_mpeg4_qscales(s);
  2548. break;
  2549. case CODEC_ID_H263:
  2550. case CODEC_ID_H263P:
  2551. ff_clean_h263_qscales(s);
  2552. break;
  2553. }
  2554. #endif
  2555. s->qscale= s->current_picture.qscale_table[0];
  2556. }else
  2557. s->qscale= (int)(s->frame_qscale + 0.5);
  2558. if (s->out_format == FMT_MJPEG) {
  2559. /* for mjpeg, we do include qscale in the matrix */
  2560. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  2561. for(i=1;i<64;i++){
  2562. int j= s->dsp.idct_permutation[i];
  2563. s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  2564. }
  2565. convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  2566. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
  2567. }
  2568. //FIXME var duplication
  2569. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2570. s->current_picture.pict_type= s->pict_type;
  2571. if(s->current_picture.key_frame)
  2572. s->picture_in_gop_number=0;
  2573. s->last_bits= get_bit_count(&s->pb);
  2574. switch(s->out_format) {
  2575. case FMT_MJPEG:
  2576. mjpeg_picture_header(s);
  2577. break;
  2578. #ifdef CONFIG_RISKY
  2579. case FMT_H263:
  2580. if (s->codec_id == CODEC_ID_WMV2)
  2581. ff_wmv2_encode_picture_header(s, picture_number);
  2582. else if (s->h263_msmpeg4)
  2583. msmpeg4_encode_picture_header(s, picture_number);
  2584. else if (s->h263_pred)
  2585. mpeg4_encode_picture_header(s, picture_number);
  2586. else if (s->h263_rv10)
  2587. rv10_encode_picture_header(s, picture_number);
  2588. else
  2589. h263_encode_picture_header(s, picture_number);
  2590. break;
  2591. #endif
  2592. case FMT_MPEG1:
  2593. mpeg1_encode_picture_header(s, picture_number);
  2594. break;
  2595. }
  2596. bits= get_bit_count(&s->pb);
  2597. s->header_bits= bits - s->last_bits;
  2598. s->last_bits= bits;
  2599. s->mv_bits=0;
  2600. s->misc_bits=0;
  2601. s->i_tex_bits=0;
  2602. s->p_tex_bits=0;
  2603. s->i_count=0;
  2604. s->f_count=0;
  2605. s->b_count=0;
  2606. s->skip_count=0;
  2607. for(i=0; i<3; i++){
  2608. /* init last dc values */
  2609. /* note: quant matrix value (8) is implied here */
  2610. s->last_dc[i] = 128;
  2611. s->current_picture_ptr->error[i] = 0;
  2612. }
  2613. s->mb_skip_run = 0;
  2614. s->last_mv[0][0][0] = 0;
  2615. s->last_mv[0][0][1] = 0;
  2616. s->last_mv[1][0][0] = 0;
  2617. s->last_mv[1][0][1] = 0;
  2618. s->last_mv_dir = 0;
  2619. #ifdef CONFIG_RISKY
  2620. if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
  2621. s->gob_index = ff_h263_get_gob_height(s);
  2622. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  2623. ff_mpeg4_init_partitions(s);
  2624. #endif
  2625. s->resync_mb_x=0;
  2626. s->resync_mb_y=0;
  2627. s->first_slice_line = 1;
  2628. s->ptr_lastgob = s->pb.buf;
  2629. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2630. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  2631. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  2632. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  2633. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  2634. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  2635. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  2636. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  2637. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  2638. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2639. int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  2640. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  2641. // int d;
  2642. int dmin=10000000;
  2643. s->mb_x = mb_x;
  2644. s->mb_y = mb_y;
  2645. s->block_index[0]+=2;
  2646. s->block_index[1]+=2;
  2647. s->block_index[2]+=2;
  2648. s->block_index[3]+=2;
  2649. s->block_index[4]++;
  2650. s->block_index[5]++;
  2651. /* write gob / video packet header */
  2652. #ifdef CONFIG_RISKY
  2653. if(s->rtp_mode){
  2654. int current_packet_size, is_gob_start;
  2655. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  2656. is_gob_start=0;
  2657. if(s->codec_id==CODEC_ID_MPEG4){
  2658. if(current_packet_size >= s->rtp_payload_size
  2659. && s->mb_y + s->mb_x>0){
  2660. if(s->partitioned_frame){
  2661. ff_mpeg4_merge_partitions(s);
  2662. ff_mpeg4_init_partitions(s);
  2663. }
  2664. ff_mpeg4_encode_video_packet_header(s);
  2665. if(s->flags&CODEC_FLAG_PASS1){
  2666. int bits= get_bit_count(&s->pb);
  2667. s->misc_bits+= bits - s->last_bits;
  2668. s->last_bits= bits;
  2669. }
  2670. ff_mpeg4_clean_buffers(s);
  2671. is_gob_start=1;
  2672. }
  2673. }else if(s->codec_id==CODEC_ID_MPEG1VIDEO){
  2674. if( current_packet_size >= s->rtp_payload_size
  2675. && s->mb_y + s->mb_x>0 && s->mb_skip_run==0){
  2676. ff_mpeg1_encode_slice_header(s);
  2677. ff_mpeg1_clean_buffers(s);
  2678. is_gob_start=1;
  2679. }
  2680. }else{
  2681. if(current_packet_size >= s->rtp_payload_size
  2682. && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
  2683. h263_encode_gob_header(s, mb_y);
  2684. is_gob_start=1;
  2685. }
  2686. }
  2687. if(is_gob_start){
  2688. s->ptr_lastgob = pbBufPtr(&s->pb);
  2689. s->first_slice_line=1;
  2690. s->resync_mb_x=mb_x;
  2691. s->resync_mb_y=mb_y;
  2692. }
  2693. }
  2694. #endif
  2695. if( (s->resync_mb_x == s->mb_x)
  2696. && s->resync_mb_y+1 == s->mb_y){
  2697. s->first_slice_line=0;
  2698. }
  2699. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  2700. int next_block=0;
  2701. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  2702. copy_context_before_encode(&backup_s, s, -1);
  2703. backup_s.pb= s->pb;
  2704. best_s.data_partitioning= s->data_partitioning;
  2705. best_s.partitioned_frame= s->partitioned_frame;
  2706. if(s->data_partitioning){
  2707. backup_s.pb2= s->pb2;
  2708. backup_s.tex_pb= s->tex_pb;
  2709. }
  2710. if(mb_type&MB_TYPE_INTER){
  2711. s->mv_dir = MV_DIR_FORWARD;
  2712. s->mv_type = MV_TYPE_16X16;
  2713. s->mb_intra= 0;
  2714. s->mv[0][0][0] = s->p_mv_table[xy][0];
  2715. s->mv[0][0][1] = s->p_mv_table[xy][1];
  2716. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  2717. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2718. }
  2719. if(mb_type&MB_TYPE_INTER4V){
  2720. s->mv_dir = MV_DIR_FORWARD;
  2721. s->mv_type = MV_TYPE_8X8;
  2722. s->mb_intra= 0;
  2723. for(i=0; i<4; i++){
  2724. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  2725. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  2726. }
  2727. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  2728. &dmin, &next_block, 0, 0);
  2729. }
  2730. if(mb_type&MB_TYPE_FORWARD){
  2731. s->mv_dir = MV_DIR_FORWARD;
  2732. s->mv_type = MV_TYPE_16X16;
  2733. s->mb_intra= 0;
  2734. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2735. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2736. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  2737. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2738. }
  2739. if(mb_type&MB_TYPE_BACKWARD){
  2740. s->mv_dir = MV_DIR_BACKWARD;
  2741. s->mv_type = MV_TYPE_16X16;
  2742. s->mb_intra= 0;
  2743. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2744. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2745. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  2746. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  2747. }
  2748. if(mb_type&MB_TYPE_BIDIR){
  2749. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2750. s->mv_type = MV_TYPE_16X16;
  2751. s->mb_intra= 0;
  2752. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2753. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2754. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2755. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2756. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  2757. &dmin, &next_block, 0, 0);
  2758. }
  2759. if(mb_type&MB_TYPE_DIRECT){
  2760. int mx= s->b_direct_mv_table[xy][0];
  2761. int my= s->b_direct_mv_table[xy][1];
  2762. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2763. s->mb_intra= 0;
  2764. #ifdef CONFIG_RISKY
  2765. ff_mpeg4_set_direct_mv(s, mx, my);
  2766. #endif
  2767. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2768. &dmin, &next_block, mx, my);
  2769. }
  2770. if(mb_type&MB_TYPE_INTRA){
  2771. s->mv_dir = 0;
  2772. s->mv_type = MV_TYPE_16X16;
  2773. s->mb_intra= 1;
  2774. s->mv[0][0][0] = 0;
  2775. s->mv[0][0][1] = 0;
  2776. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  2777. &dmin, &next_block, 0, 0);
  2778. /* force cleaning of ac/dc pred stuff if needed ... */
  2779. if(s->h263_pred || s->h263_aic)
  2780. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  2781. }
  2782. copy_context_after_encode(s, &best_s, -1);
  2783. pb_bits_count= get_bit_count(&s->pb);
  2784. flush_put_bits(&s->pb);
  2785. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  2786. s->pb= backup_s.pb;
  2787. if(s->data_partitioning){
  2788. pb2_bits_count= get_bit_count(&s->pb2);
  2789. flush_put_bits(&s->pb2);
  2790. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  2791. s->pb2= backup_s.pb2;
  2792. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  2793. flush_put_bits(&s->tex_pb);
  2794. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  2795. s->tex_pb= backup_s.tex_pb;
  2796. }
  2797. s->last_bits= get_bit_count(&s->pb);
  2798. } else {
  2799. int motion_x, motion_y;
  2800. int intra_score;
  2801. int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_width];
  2802. if(!(s->flags&CODEC_FLAG_HQ) && s->pict_type==P_TYPE){
  2803. /* get luma score */
  2804. if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  2805. 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
  2806. }else{
  2807. uint8_t *dest_y;
  2808. int mean= s->current_picture.mb_mean[mb_x + mb_y*s->mb_width]; //FIXME
  2809. mean*= 0x01010101;
  2810. dest_y = s->new_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  2811. for(i=0; i<16; i++){
  2812. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
  2813. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
  2814. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
  2815. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
  2816. }
  2817. s->mb_intra=1;
  2818. intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, dest_y, s->linesize);
  2819. /* printf("intra:%7d inter:%7d var:%7d mc_var.%7d\n", intra_score>>8, inter_score>>8,
  2820. s->current_picture.mb_var[mb_x + mb_y*s->mb_width],
  2821. s->current_picture.mc_mb_var[mb_x + mb_y*s->mb_width]);*/
  2822. }
  2823. /* get chroma score */
  2824. if(s->avctx->mb_cmp&FF_CMP_CHROMA){
  2825. int i;
  2826. s->mb_intra=1;
  2827. for(i=1; i<3; i++){
  2828. uint8_t *dest_c;
  2829. int mean;
  2830. if(s->out_format == FMT_H263){
  2831. mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
  2832. }else{
  2833. mean= (s->last_dc[i] + 4)>>3;
  2834. }
  2835. dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2836. mean*= 0x01010101;
  2837. for(i=0; i<8; i++){
  2838. *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
  2839. *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
  2840. }
  2841. intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
  2842. }
  2843. }
  2844. /* bias */
  2845. switch(s->avctx->mb_cmp&0xFF){
  2846. default:
  2847. case FF_CMP_SAD:
  2848. intra_score+= 32*s->qscale;
  2849. break;
  2850. case FF_CMP_SSE:
  2851. intra_score+= 24*s->qscale*s->qscale;
  2852. break;
  2853. case FF_CMP_SATD:
  2854. intra_score+= 96*s->qscale;
  2855. break;
  2856. case FF_CMP_DCT:
  2857. intra_score+= 48*s->qscale;
  2858. break;
  2859. case FF_CMP_BIT:
  2860. intra_score+= 16;
  2861. break;
  2862. case FF_CMP_PSNR:
  2863. case FF_CMP_RD:
  2864. intra_score+= (s->qscale*s->qscale*109*8 + 64)>>7;
  2865. break;
  2866. }
  2867. if(intra_score < inter_score)
  2868. mb_type= MB_TYPE_INTRA;
  2869. }
  2870. s->mv_type=MV_TYPE_16X16;
  2871. // only one MB-Type possible
  2872. switch(mb_type){
  2873. case MB_TYPE_INTRA:
  2874. s->mv_dir = 0;
  2875. s->mb_intra= 1;
  2876. motion_x= s->mv[0][0][0] = 0;
  2877. motion_y= s->mv[0][0][1] = 0;
  2878. break;
  2879. case MB_TYPE_INTER:
  2880. s->mv_dir = MV_DIR_FORWARD;
  2881. s->mb_intra= 0;
  2882. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  2883. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  2884. break;
  2885. case MB_TYPE_INTER4V:
  2886. s->mv_dir = MV_DIR_FORWARD;
  2887. s->mv_type = MV_TYPE_8X8;
  2888. s->mb_intra= 0;
  2889. for(i=0; i<4; i++){
  2890. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  2891. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  2892. }
  2893. motion_x= motion_y= 0;
  2894. break;
  2895. case MB_TYPE_DIRECT:
  2896. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2897. s->mb_intra= 0;
  2898. motion_x=s->b_direct_mv_table[xy][0];
  2899. motion_y=s->b_direct_mv_table[xy][1];
  2900. #ifdef CONFIG_RISKY
  2901. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  2902. #endif
  2903. break;
  2904. case MB_TYPE_BIDIR:
  2905. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2906. s->mb_intra= 0;
  2907. motion_x=0;
  2908. motion_y=0;
  2909. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2910. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2911. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2912. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2913. break;
  2914. case MB_TYPE_BACKWARD:
  2915. s->mv_dir = MV_DIR_BACKWARD;
  2916. s->mb_intra= 0;
  2917. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2918. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2919. break;
  2920. case MB_TYPE_FORWARD:
  2921. s->mv_dir = MV_DIR_FORWARD;
  2922. s->mb_intra= 0;
  2923. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2924. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2925. // printf(" %d %d ", motion_x, motion_y);
  2926. break;
  2927. default:
  2928. motion_x=motion_y=0; //gcc warning fix
  2929. printf("illegal MB type\n");
  2930. }
  2931. encode_mb(s, motion_x, motion_y);
  2932. // RAL: Update last macrobloc type
  2933. s->last_mv_dir = s->mv_dir;
  2934. }
  2935. /* clean the MV table in IPS frames for direct mode in B frames */
  2936. if(s->mb_intra /* && I,P,S_TYPE */){
  2937. s->p_mv_table[xy][0]=0;
  2938. s->p_mv_table[xy][1]=0;
  2939. }
  2940. MPV_decode_mb(s, s->block);
  2941. if(s->flags&CODEC_FLAG_PSNR){
  2942. int w= 16;
  2943. int h= 16;
  2944. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  2945. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  2946. s->current_picture_ptr->error[0] += sse(
  2947. s,
  2948. s->new_picture .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  2949. s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  2950. w, h, s->linesize);
  2951. s->current_picture_ptr->error[1] += sse(
  2952. s,
  2953. s->new_picture .data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2954. s->current_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2955. w>>1, h>>1, s->uvlinesize);
  2956. s->current_picture_ptr->error[2] += sse(
  2957. s,
  2958. s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2959. s->current_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2960. w>>1, h>>1, s->uvlinesize);
  2961. }
  2962. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
  2963. }
  2964. }
  2965. emms_c();
  2966. #ifdef CONFIG_RISKY
  2967. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  2968. ff_mpeg4_merge_partitions(s);
  2969. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  2970. msmpeg4_encode_ext_header(s);
  2971. if(s->codec_id==CODEC_ID_MPEG4)
  2972. ff_mpeg4_stuffing(&s->pb);
  2973. #endif
  2974. //if (s->gob_number)
  2975. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  2976. /* Send the last GOB if RTP */
  2977. if (s->rtp_mode) {
  2978. flush_put_bits(&s->pb);
  2979. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  2980. /* Call the RTP callback to send the last GOB */
  2981. if (s->rtp_callback)
  2982. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  2983. s->ptr_lastgob = pbBufPtr(&s->pb);
  2984. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  2985. }
  2986. }
  2987. static int dct_quantize_trellis_c(MpegEncContext *s,
  2988. DCTELEM *block, int n,
  2989. int qscale, int *overflow){
  2990. const int *qmat;
  2991. const uint8_t *scantable= s->intra_scantable.scantable;
  2992. int max=0;
  2993. unsigned int threshold1, threshold2;
  2994. int bias=0;
  2995. int run_tab[65];
  2996. int level_tab[65];
  2997. int score_tab[65];
  2998. int last_run=0;
  2999. int last_level=0;
  3000. int last_score= 0;
  3001. int last_i= 0;
  3002. int coeff[3][64];
  3003. int coeff_count[64];
  3004. int lambda, qmul, qadd, start_i, last_non_zero, i;
  3005. const int esc_length= s->ac_esc_length;
  3006. uint8_t * length;
  3007. uint8_t * last_length;
  3008. int score_limit=0;
  3009. int left_limit= 0;
  3010. s->dsp.fdct (block);
  3011. qmul= qscale*16;
  3012. qadd= ((qscale-1)|1)*8;
  3013. if (s->mb_intra) {
  3014. int q;
  3015. if (!s->h263_aic) {
  3016. if (n < 4)
  3017. q = s->y_dc_scale;
  3018. else
  3019. q = s->c_dc_scale;
  3020. q = q << 3;
  3021. } else{
  3022. /* For AIC we skip quant/dequant of INTRADC */
  3023. q = 1 << 3;
  3024. qadd=0;
  3025. }
  3026. /* note: block[0] is assumed to be positive */
  3027. block[0] = (block[0] + (q >> 1)) / q;
  3028. start_i = 1;
  3029. last_non_zero = 0;
  3030. qmat = s->q_intra_matrix[qscale];
  3031. if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO)
  3032. bias= 1<<(QMAT_SHIFT-1);
  3033. length = s->intra_ac_vlc_length;
  3034. last_length= s->intra_ac_vlc_last_length;
  3035. } else {
  3036. start_i = 0;
  3037. last_non_zero = -1;
  3038. qmat = s->q_inter_matrix[qscale];
  3039. length = s->inter_ac_vlc_length;
  3040. last_length= s->inter_ac_vlc_last_length;
  3041. }
  3042. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3043. threshold2= (threshold1<<1);
  3044. for(i=start_i; i<64; i++) {
  3045. const int j = scantable[i];
  3046. const int k= i-start_i;
  3047. int level = block[j];
  3048. level = level * qmat[j];
  3049. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3050. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3051. if(((unsigned)(level+threshold1))>threshold2){
  3052. if(level>0){
  3053. level= (bias + level)>>QMAT_SHIFT;
  3054. coeff[0][k]= level;
  3055. coeff[1][k]= level-1;
  3056. // coeff[2][k]= level-2;
  3057. }else{
  3058. level= (bias - level)>>QMAT_SHIFT;
  3059. coeff[0][k]= -level;
  3060. coeff[1][k]= -level+1;
  3061. // coeff[2][k]= -level+2;
  3062. }
  3063. coeff_count[k]= FFMIN(level, 2);
  3064. max |=level;
  3065. last_non_zero = i;
  3066. }else{
  3067. coeff[0][k]= (level>>31)|1;
  3068. coeff_count[k]= 1;
  3069. }
  3070. }
  3071. *overflow= s->max_qcoeff < max; //overflow might have happend
  3072. if(last_non_zero < start_i){
  3073. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3074. return last_non_zero;
  3075. }
  3076. lambda= (qscale*qscale*64*105 + 64)>>7; //FIXME finetune
  3077. score_tab[0]= 0;
  3078. for(i=0; i<=last_non_zero - start_i; i++){
  3079. int level_index, run, j;
  3080. const int dct_coeff= block[ scantable[i + start_i] ];
  3081. const int zero_distoration= dct_coeff*dct_coeff;
  3082. int best_score=256*256*256*120;
  3083. last_score += zero_distoration;
  3084. for(level_index=0; level_index < coeff_count[i]; level_index++){
  3085. int distoration;
  3086. int level= coeff[level_index][i];
  3087. int unquant_coeff;
  3088. assert(level);
  3089. if(s->out_format == FMT_H263){
  3090. if(level>0){
  3091. unquant_coeff= level*qmul + qadd;
  3092. }else{
  3093. unquant_coeff= level*qmul - qadd;
  3094. }
  3095. }else{ //MPEG1
  3096. j= s->dsp.idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
  3097. if(s->mb_intra){
  3098. if (level < 0) {
  3099. unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
  3100. unquant_coeff = -((unquant_coeff - 1) | 1);
  3101. } else {
  3102. unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3;
  3103. unquant_coeff = (unquant_coeff - 1) | 1;
  3104. }
  3105. }else{
  3106. if (level < 0) {
  3107. unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3108. unquant_coeff = -((unquant_coeff - 1) | 1);
  3109. } else {
  3110. unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3111. unquant_coeff = (unquant_coeff - 1) | 1;
  3112. }
  3113. }
  3114. unquant_coeff<<= 3;
  3115. }
  3116. distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
  3117. level+=64;
  3118. if((level&(~127)) == 0){
  3119. for(run=0; run<=i - left_limit; run++){
  3120. int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3121. score += score_tab[i-run];
  3122. if(score < best_score){
  3123. best_score=
  3124. score_tab[i+1]= score;
  3125. run_tab[i+1]= run;
  3126. level_tab[i+1]= level-64;
  3127. }
  3128. }
  3129. if(s->out_format == FMT_H263){
  3130. for(run=0; run<=i - left_limit; run++){
  3131. int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3132. score += score_tab[i-run];
  3133. if(score < last_score){
  3134. last_score= score;
  3135. last_run= run;
  3136. last_level= level-64;
  3137. last_i= i+1;
  3138. }
  3139. }
  3140. }
  3141. }else{
  3142. distoration += esc_length*lambda;
  3143. for(run=0; run<=i - left_limit; run++){
  3144. int score= distoration + score_tab[i-run];
  3145. if(score < best_score){
  3146. best_score=
  3147. score_tab[i+1]= score;
  3148. run_tab[i+1]= run;
  3149. level_tab[i+1]= level-64;
  3150. }
  3151. }
  3152. if(s->out_format == FMT_H263){
  3153. for(run=0; run<=i - left_limit; run++){
  3154. int score= distoration + score_tab[i-run];
  3155. if(score < last_score){
  3156. last_score= score;
  3157. last_run= run;
  3158. last_level= level-64;
  3159. last_i= i+1;
  3160. }
  3161. }
  3162. }
  3163. }
  3164. }
  3165. for(j=left_limit; j<=i; j++){
  3166. score_tab[j] += zero_distoration;
  3167. }
  3168. score_limit+= zero_distoration;
  3169. if(score_tab[i+1] < score_limit)
  3170. score_limit= score_tab[i+1];
  3171. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  3172. while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
  3173. }
  3174. //FIXME add some cbp penalty
  3175. if(s->out_format != FMT_H263){
  3176. last_score= 256*256*256*120;
  3177. for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
  3178. int score= score_tab[i];
  3179. if(i) score += lambda*2; //FIXME exacter?
  3180. if(score < last_score){
  3181. last_score= score;
  3182. last_i= i;
  3183. last_level= level_tab[i];
  3184. last_run= run_tab[i];
  3185. }
  3186. }
  3187. }
  3188. last_non_zero= last_i - 1 + start_i;
  3189. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3190. if(last_non_zero < start_i)
  3191. return last_non_zero;
  3192. i= last_i;
  3193. assert(last_level);
  3194. //FIXME use permutated scantable
  3195. block[ s->dsp.idct_permutation[ scantable[last_non_zero] ] ]= last_level;
  3196. i -= last_run + 1;
  3197. for(;i>0 ; i -= run_tab[i] + 1){
  3198. const int j= s->dsp.idct_permutation[ scantable[i - 1 + start_i] ];
  3199. block[j]= level_tab[i];
  3200. assert(block[j]);
  3201. }
  3202. return last_non_zero;
  3203. }
  3204. static int dct_quantize_c(MpegEncContext *s,
  3205. DCTELEM *block, int n,
  3206. int qscale, int *overflow)
  3207. {
  3208. int i, j, level, last_non_zero, q;
  3209. const int *qmat;
  3210. const uint8_t *scantable= s->intra_scantable.scantable;
  3211. int bias;
  3212. int max=0;
  3213. unsigned int threshold1, threshold2;
  3214. s->dsp.fdct (block);
  3215. if (s->mb_intra) {
  3216. if (!s->h263_aic) {
  3217. if (n < 4)
  3218. q = s->y_dc_scale;
  3219. else
  3220. q = s->c_dc_scale;
  3221. q = q << 3;
  3222. } else
  3223. /* For AIC we skip quant/dequant of INTRADC */
  3224. q = 1 << 3;
  3225. /* note: block[0] is assumed to be positive */
  3226. block[0] = (block[0] + (q >> 1)) / q;
  3227. i = 1;
  3228. last_non_zero = 0;
  3229. qmat = s->q_intra_matrix[qscale];
  3230. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3231. } else {
  3232. i = 0;
  3233. last_non_zero = -1;
  3234. qmat = s->q_inter_matrix[qscale];
  3235. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3236. }
  3237. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3238. threshold2= (threshold1<<1);
  3239. for(;i<64;i++) {
  3240. j = scantable[i];
  3241. level = block[j];
  3242. level = level * qmat[j];
  3243. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3244. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3245. if(((unsigned)(level+threshold1))>threshold2){
  3246. if(level>0){
  3247. level= (bias + level)>>QMAT_SHIFT;
  3248. block[j]= level;
  3249. }else{
  3250. level= (bias - level)>>QMAT_SHIFT;
  3251. block[j]= -level;
  3252. }
  3253. max |=level;
  3254. last_non_zero = i;
  3255. }else{
  3256. block[j]=0;
  3257. }
  3258. }
  3259. *overflow= s->max_qcoeff < max; //overflow might have happend
  3260. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  3261. if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
  3262. ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
  3263. return last_non_zero;
  3264. }
  3265. #endif //CONFIG_ENCODERS
  3266. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  3267. DCTELEM *block, int n, int qscale)
  3268. {
  3269. int i, level, nCoeffs;
  3270. const uint16_t *quant_matrix;
  3271. nCoeffs= s->block_last_index[n];
  3272. if (s->mb_intra) {
  3273. if (n < 4)
  3274. block[0] = block[0] * s->y_dc_scale;
  3275. else
  3276. block[0] = block[0] * s->c_dc_scale;
  3277. /* XXX: only mpeg1 */
  3278. quant_matrix = s->intra_matrix;
  3279. for(i=1;i<=nCoeffs;i++) {
  3280. int j= s->intra_scantable.permutated[i];
  3281. level = block[j];
  3282. if (level) {
  3283. if (level < 0) {
  3284. level = -level;
  3285. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3286. level = (level - 1) | 1;
  3287. level = -level;
  3288. } else {
  3289. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3290. level = (level - 1) | 1;
  3291. }
  3292. #ifdef PARANOID
  3293. if (level < -2048 || level > 2047)
  3294. fprintf(stderr, "unquant error %d %d\n", i, level);
  3295. #endif
  3296. block[j] = level;
  3297. }
  3298. }
  3299. } else {
  3300. i = 0;
  3301. quant_matrix = s->inter_matrix;
  3302. for(;i<=nCoeffs;i++) {
  3303. int j= s->intra_scantable.permutated[i];
  3304. level = block[j];
  3305. if (level) {
  3306. if (level < 0) {
  3307. level = -level;
  3308. level = (((level << 1) + 1) * qscale *
  3309. ((int) (quant_matrix[j]))) >> 4;
  3310. level = (level - 1) | 1;
  3311. level = -level;
  3312. } else {
  3313. level = (((level << 1) + 1) * qscale *
  3314. ((int) (quant_matrix[j]))) >> 4;
  3315. level = (level - 1) | 1;
  3316. }
  3317. #ifdef PARANOID
  3318. if (level < -2048 || level > 2047)
  3319. fprintf(stderr, "unquant error %d %d\n", i, level);
  3320. #endif
  3321. block[j] = level;
  3322. }
  3323. }
  3324. }
  3325. }
  3326. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  3327. DCTELEM *block, int n, int qscale)
  3328. {
  3329. int i, level, nCoeffs;
  3330. const uint16_t *quant_matrix;
  3331. if(s->alternate_scan) nCoeffs= 63;
  3332. else nCoeffs= s->block_last_index[n];
  3333. if (s->mb_intra) {
  3334. if (n < 4)
  3335. block[0] = block[0] * s->y_dc_scale;
  3336. else
  3337. block[0] = block[0] * s->c_dc_scale;
  3338. quant_matrix = s->intra_matrix;
  3339. for(i=1;i<=nCoeffs;i++) {
  3340. int j= s->intra_scantable.permutated[i];
  3341. level = block[j];
  3342. if (level) {
  3343. if (level < 0) {
  3344. level = -level;
  3345. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3346. level = -level;
  3347. } else {
  3348. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3349. }
  3350. #ifdef PARANOID
  3351. if (level < -2048 || level > 2047)
  3352. fprintf(stderr, "unquant error %d %d\n", i, level);
  3353. #endif
  3354. block[j] = level;
  3355. }
  3356. }
  3357. } else {
  3358. int sum=-1;
  3359. i = 0;
  3360. quant_matrix = s->inter_matrix;
  3361. for(;i<=nCoeffs;i++) {
  3362. int j= s->intra_scantable.permutated[i];
  3363. level = block[j];
  3364. if (level) {
  3365. if (level < 0) {
  3366. level = -level;
  3367. level = (((level << 1) + 1) * qscale *
  3368. ((int) (quant_matrix[j]))) >> 4;
  3369. level = -level;
  3370. } else {
  3371. level = (((level << 1) + 1) * qscale *
  3372. ((int) (quant_matrix[j]))) >> 4;
  3373. }
  3374. #ifdef PARANOID
  3375. if (level < -2048 || level > 2047)
  3376. fprintf(stderr, "unquant error %d %d\n", i, level);
  3377. #endif
  3378. block[j] = level;
  3379. sum+=level;
  3380. }
  3381. }
  3382. block[63]^=sum&1;
  3383. }
  3384. }
  3385. static void dct_unquantize_h263_c(MpegEncContext *s,
  3386. DCTELEM *block, int n, int qscale)
  3387. {
  3388. int i, level, qmul, qadd;
  3389. int nCoeffs;
  3390. assert(s->block_last_index[n]>=0);
  3391. qadd = (qscale - 1) | 1;
  3392. qmul = qscale << 1;
  3393. if (s->mb_intra) {
  3394. if (!s->h263_aic) {
  3395. if (n < 4)
  3396. block[0] = block[0] * s->y_dc_scale;
  3397. else
  3398. block[0] = block[0] * s->c_dc_scale;
  3399. }else
  3400. qadd = 0;
  3401. i = 1;
  3402. nCoeffs= 63; //does not allways use zigzag table
  3403. } else {
  3404. i = 0;
  3405. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  3406. }
  3407. for(;i<=nCoeffs;i++) {
  3408. level = block[i];
  3409. if (level) {
  3410. if (level < 0) {
  3411. level = level * qmul - qadd;
  3412. } else {
  3413. level = level * qmul + qadd;
  3414. }
  3415. #ifdef PARANOID
  3416. if (level < -2048 || level > 2047)
  3417. fprintf(stderr, "unquant error %d %d\n", i, level);
  3418. #endif
  3419. block[i] = level;
  3420. }
  3421. }
  3422. }
  3423. char ff_get_pict_type_char(int pict_type){
  3424. switch(pict_type){
  3425. case I_TYPE: return 'I';
  3426. case P_TYPE: return 'P';
  3427. case B_TYPE: return 'B';
  3428. case S_TYPE: return 'S';
  3429. case SI_TYPE:return 'i';
  3430. case SP_TYPE:return 'p';
  3431. default: return '?';
  3432. }
  3433. }
  3434. static const AVOption mpeg4_options[] =
  3435. {
  3436. AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
  3437. AVOPTION_CODEC_FLAG("vhq", "very high quality", flags, CODEC_FLAG_HQ, 0),
  3438. AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
  3439. "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
  3440. bit_rate_tolerance, 4, 240000000, 8000),
  3441. AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
  3442. AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
  3443. AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
  3444. rc_eq, "tex^qComp,option1,options2", 0),
  3445. AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
  3446. rc_min_rate, 4, 24000000, 0),
  3447. AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
  3448. rc_max_rate, 4, 24000000, 0),
  3449. AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
  3450. rc_buffer_aggressivity, 4, 24000000, 0),
  3451. AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
  3452. rc_initial_cplx, 0., 9999999., 0),
  3453. AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
  3454. i_quant_factor, 0., 0., 0),
  3455. AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
  3456. i_quant_factor, -999999., 999999., 0),
  3457. AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
  3458. dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
  3459. AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
  3460. lumi_masking, 0., 999999., 0),
  3461. AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
  3462. temporal_cplx_masking, 0., 999999., 0),
  3463. AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
  3464. spatial_cplx_masking, 0., 999999., 0),
  3465. AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
  3466. p_masking, 0., 999999., 0),
  3467. AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
  3468. dark_masking, 0., 999999., 0),
  3469. AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
  3470. idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
  3471. AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
  3472. mb_qmin, 0, 8, 0),
  3473. AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
  3474. mb_qmin, 0, 8, 0),
  3475. AVOPTION_CODEC_INT("me_cmp", "ME compare function",
  3476. me_cmp, 0, 24000000, 0),
  3477. AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
  3478. me_sub_cmp, 0, 24000000, 0),
  3479. AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
  3480. dia_size, 0, 24000000, 0),
  3481. AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
  3482. last_predictor_count, 0, 24000000, 0),
  3483. AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
  3484. pre_me, 0, 24000000, 0),
  3485. AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
  3486. me_pre_cmp, 0, 24000000, 0),
  3487. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  3488. me_range, 0, 24000000, 0),
  3489. AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
  3490. pre_dia_size, 0, 24000000, 0),
  3491. AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
  3492. me_subpel_quality, 0, 24000000, 0),
  3493. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  3494. me_range, 0, 24000000, 0),
  3495. AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
  3496. flags, CODEC_FLAG_PSNR, 0),
  3497. AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
  3498. rc_override),
  3499. AVOPTION_SUB(avoptions_common),
  3500. AVOPTION_END()
  3501. };
  3502. #ifdef CONFIG_ENCODERS
  3503. AVCodec mpeg1video_encoder = {
  3504. "mpeg1video",
  3505. CODEC_TYPE_VIDEO,
  3506. CODEC_ID_MPEG1VIDEO,
  3507. sizeof(MpegEncContext),
  3508. MPV_encode_init,
  3509. MPV_encode_picture,
  3510. MPV_encode_end,
  3511. };
  3512. #ifdef CONFIG_RISKY
  3513. AVCodec h263_encoder = {
  3514. "h263",
  3515. CODEC_TYPE_VIDEO,
  3516. CODEC_ID_H263,
  3517. sizeof(MpegEncContext),
  3518. MPV_encode_init,
  3519. MPV_encode_picture,
  3520. MPV_encode_end,
  3521. };
  3522. AVCodec h263p_encoder = {
  3523. "h263p",
  3524. CODEC_TYPE_VIDEO,
  3525. CODEC_ID_H263P,
  3526. sizeof(MpegEncContext),
  3527. MPV_encode_init,
  3528. MPV_encode_picture,
  3529. MPV_encode_end,
  3530. };
  3531. AVCodec rv10_encoder = {
  3532. "rv10",
  3533. CODEC_TYPE_VIDEO,
  3534. CODEC_ID_RV10,
  3535. sizeof(MpegEncContext),
  3536. MPV_encode_init,
  3537. MPV_encode_picture,
  3538. MPV_encode_end,
  3539. };
  3540. AVCodec mpeg4_encoder = {
  3541. "mpeg4",
  3542. CODEC_TYPE_VIDEO,
  3543. CODEC_ID_MPEG4,
  3544. sizeof(MpegEncContext),
  3545. MPV_encode_init,
  3546. MPV_encode_picture,
  3547. MPV_encode_end,
  3548. .options = mpeg4_options,
  3549. };
  3550. AVCodec msmpeg4v1_encoder = {
  3551. "msmpeg4v1",
  3552. CODEC_TYPE_VIDEO,
  3553. CODEC_ID_MSMPEG4V1,
  3554. sizeof(MpegEncContext),
  3555. MPV_encode_init,
  3556. MPV_encode_picture,
  3557. MPV_encode_end,
  3558. .options = mpeg4_options,
  3559. };
  3560. AVCodec msmpeg4v2_encoder = {
  3561. "msmpeg4v2",
  3562. CODEC_TYPE_VIDEO,
  3563. CODEC_ID_MSMPEG4V2,
  3564. sizeof(MpegEncContext),
  3565. MPV_encode_init,
  3566. MPV_encode_picture,
  3567. MPV_encode_end,
  3568. .options = mpeg4_options,
  3569. };
  3570. AVCodec msmpeg4v3_encoder = {
  3571. "msmpeg4",
  3572. CODEC_TYPE_VIDEO,
  3573. CODEC_ID_MSMPEG4V3,
  3574. sizeof(MpegEncContext),
  3575. MPV_encode_init,
  3576. MPV_encode_picture,
  3577. MPV_encode_end,
  3578. .options = mpeg4_options,
  3579. };
  3580. AVCodec wmv1_encoder = {
  3581. "wmv1",
  3582. CODEC_TYPE_VIDEO,
  3583. CODEC_ID_WMV1,
  3584. sizeof(MpegEncContext),
  3585. MPV_encode_init,
  3586. MPV_encode_picture,
  3587. MPV_encode_end,
  3588. .options = mpeg4_options,
  3589. };
  3590. #endif
  3591. AVCodec mjpeg_encoder = {
  3592. "mjpeg",
  3593. CODEC_TYPE_VIDEO,
  3594. CODEC_ID_MJPEG,
  3595. sizeof(MpegEncContext),
  3596. MPV_encode_init,
  3597. MPV_encode_picture,
  3598. MPV_encode_end,
  3599. };
  3600. #endif //CONFIG_ENCODERS