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.

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