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.

4160 lines
142KB

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