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.

4800 lines
166KB

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