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.

6129 lines
220KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  21. */
  22. /**
  23. * @file mpegvideo.c
  24. * The simplest mpeg encoder (well, it was the simplest!).
  25. */
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "mpegvideo.h"
  29. #include "faandct.h"
  30. #include <limits.h>
  31. #ifdef USE_FASTMEMCPY
  32. #include "fastmemcpy.h"
  33. #endif
  34. //#undef NDEBUG
  35. //#include <assert.h>
  36. #ifdef CONFIG_ENCODERS
  37. static void encode_picture(MpegEncContext *s, int picture_number);
  38. #endif //CONFIG_ENCODERS
  39. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  40. DCTELEM *block, int n, int qscale);
  41. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  42. DCTELEM *block, int n, int qscale);
  43. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  44. DCTELEM *block, int n, int qscale);
  45. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  46. DCTELEM *block, int n, int qscale);
  47. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  48. DCTELEM *block, int n, int qscale);
  49. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  50. DCTELEM *block, int n, int qscale);
  51. static void dct_unquantize_h261_intra_c(MpegEncContext *s,
  52. DCTELEM *block, int n, int qscale);
  53. static void dct_unquantize_h261_inter_c(MpegEncContext *s,
  54. DCTELEM *block, int n, int qscale);
  55. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
  56. #ifdef CONFIG_ENCODERS
  57. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  58. static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  59. static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
  60. static int sse_mb(MpegEncContext *s);
  61. static void denoise_dct_c(MpegEncContext *s, DCTELEM *block);
  62. #endif //CONFIG_ENCODERS
  63. #ifdef HAVE_XVMC
  64. extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
  65. extern void XVMC_field_end(MpegEncContext *s);
  66. extern void XVMC_decode_mb(MpegEncContext *s);
  67. #endif
  68. void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
  69. /* enable all paranoid tests for rounding, overflows, etc... */
  70. //#define PARANOID
  71. //#define DEBUG
  72. /* for jpeg fast DCT */
  73. #define CONST_BITS 14
  74. static const uint16_t aanscales[64] = {
  75. /* precomputed values scaled up by 14 bits */
  76. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  77. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  78. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  79. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  80. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  81. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  82. 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  83. 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
  84. };
  85. static const uint8_t h263_chroma_roundtab[16] = {
  86. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  87. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  88. };
  89. static const uint8_t ff_default_chroma_qscale_table[32]={
  90. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  91. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
  92. };
  93. #ifdef CONFIG_ENCODERS
  94. static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
  95. static uint8_t default_fcode_tab[MAX_MV*2+1];
  96. enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
  97. static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
  98. const uint16_t *quant_matrix, int bias, int qmin, int qmax)
  99. {
  100. int qscale;
  101. for(qscale=qmin; qscale<=qmax; qscale++){
  102. int i;
  103. if (dsp->fdct == ff_jpeg_fdct_islow
  104. #ifdef FAAN_POSTSCALE
  105. || dsp->fdct == ff_faandct
  106. #endif
  107. ) {
  108. for(i=0;i<64;i++) {
  109. const int j= dsp->idct_permutation[i];
  110. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  111. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  112. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  113. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  114. qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
  115. (qscale * quant_matrix[j]));
  116. }
  117. } else if (dsp->fdct == fdct_ifast
  118. #ifndef FAAN_POSTSCALE
  119. || dsp->fdct == ff_faandct
  120. #endif
  121. ) {
  122. for(i=0;i<64;i++) {
  123. const int j= dsp->idct_permutation[i];
  124. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  125. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  126. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  127. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  128. qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
  129. (aanscales[i] * qscale * quant_matrix[j]));
  130. }
  131. } else {
  132. for(i=0;i<64;i++) {
  133. const int j= dsp->idct_permutation[i];
  134. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  135. So 16 <= qscale * quant_matrix[i] <= 7905
  136. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  137. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  138. */
  139. qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
  140. // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  141. qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
  142. if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
  143. qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
  144. }
  145. }
  146. }
  147. }
  148. static inline void update_qscale(MpegEncContext *s){
  149. s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  150. s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
  151. s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  152. }
  153. #endif //CONFIG_ENCODERS
  154. void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
  155. int i;
  156. int end;
  157. st->scantable= src_scantable;
  158. for(i=0; i<64; i++){
  159. int j;
  160. j = src_scantable[i];
  161. st->permutated[i] = permutation[j];
  162. #ifdef ARCH_POWERPC
  163. st->inverse[j] = i;
  164. #endif
  165. }
  166. end=-1;
  167. for(i=0; i<64; i++){
  168. int j;
  169. j = st->permutated[i];
  170. if(j>end) end=j;
  171. st->raster_end[i]= end;
  172. }
  173. }
  174. #ifdef CONFIG_ENCODERS
  175. void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix){
  176. int i;
  177. if(matrix){
  178. put_bits(pb, 1, 1);
  179. for(i=0;i<64;i++) {
  180. put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
  181. }
  182. }else
  183. put_bits(pb, 1, 0);
  184. }
  185. #endif //CONFIG_ENCODERS
  186. /* init common dct for both encoder and decoder */
  187. int DCT_common_init(MpegEncContext *s)
  188. {
  189. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  190. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  191. s->dct_unquantize_h261_intra = dct_unquantize_h261_intra_c;
  192. s->dct_unquantize_h261_inter = dct_unquantize_h261_inter_c;
  193. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  194. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  195. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  196. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  197. #ifdef CONFIG_ENCODERS
  198. s->dct_quantize= dct_quantize_c;
  199. s->denoise_dct= denoise_dct_c;
  200. #endif
  201. #ifdef HAVE_MMX
  202. MPV_common_init_mmx(s);
  203. #endif
  204. #ifdef ARCH_ALPHA
  205. MPV_common_init_axp(s);
  206. #endif
  207. #ifdef HAVE_MLIB
  208. MPV_common_init_mlib(s);
  209. #endif
  210. #ifdef HAVE_MMI
  211. MPV_common_init_mmi(s);
  212. #endif
  213. #ifdef ARCH_ARMV4L
  214. MPV_common_init_armv4l(s);
  215. #endif
  216. #ifdef ARCH_POWERPC
  217. MPV_common_init_ppc(s);
  218. #endif
  219. #ifdef CONFIG_ENCODERS
  220. s->fast_dct_quantize= s->dct_quantize;
  221. if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
  222. s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
  223. }
  224. #endif //CONFIG_ENCODERS
  225. /* load & permutate scantables
  226. note: only wmv uses differnt ones
  227. */
  228. if(s->alternate_scan){
  229. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  230. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  231. }else{
  232. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  233. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  234. }
  235. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  236. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  237. return 0;
  238. }
  239. static void copy_picture(Picture *dst, Picture *src){
  240. *dst = *src;
  241. dst->type= FF_BUFFER_TYPE_COPY;
  242. }
  243. static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *src){
  244. int i;
  245. dst->pict_type = src->pict_type;
  246. dst->quality = src->quality;
  247. dst->coded_picture_number = src->coded_picture_number;
  248. dst->display_picture_number = src->display_picture_number;
  249. // dst->reference = src->reference;
  250. dst->pts = src->pts;
  251. dst->interlaced_frame = src->interlaced_frame;
  252. dst->top_field_first = src->top_field_first;
  253. if(s->avctx->me_threshold){
  254. if(!src->motion_val[0])
  255. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
  256. if(!src->mb_type)
  257. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
  258. if(!src->ref_index[0])
  259. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
  260. if(src->motion_subsample_log2 != dst->motion_subsample_log2)
  261. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesnt match! (%d!=%d)\n",
  262. src->motion_subsample_log2, dst->motion_subsample_log2);
  263. memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
  264. for(i=0; i<2; i++){
  265. int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1;
  266. int height= ((16*s->mb_height)>>src->motion_subsample_log2);
  267. if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){
  268. memcpy(dst->motion_val[i], src->motion_val[i], 2*stride*height*sizeof(int16_t));
  269. }
  270. if(src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]){
  271. memcpy(dst->ref_index[i], src->ref_index[i], s->b8_stride*2*s->mb_height*sizeof(int8_t));
  272. }
  273. }
  274. }
  275. }
  276. /**
  277. * allocates a Picture
  278. * The pixels are allocated/set by calling get_buffer() if shared=0
  279. */
  280. static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  281. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
  282. const int mb_array_size= s->mb_stride*s->mb_height;
  283. const int b8_array_size= s->b8_stride*s->mb_height*2;
  284. const int b4_array_size= s->b4_stride*s->mb_height*4;
  285. int i;
  286. if(shared){
  287. assert(pic->data[0]);
  288. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  289. pic->type= FF_BUFFER_TYPE_SHARED;
  290. }else{
  291. int r;
  292. assert(!pic->data[0]);
  293. r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
  294. if(r<0 || !pic->age || !pic->type || !pic->data[0]){
  295. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  296. return -1;
  297. }
  298. if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
  299. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  300. return -1;
  301. }
  302. if(pic->linesize[1] != pic->linesize[2]){
  303. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride missmatch)\n");
  304. return -1;
  305. }
  306. s->linesize = pic->linesize[0];
  307. s->uvlinesize= pic->linesize[1];
  308. }
  309. if(pic->qscale_table==NULL){
  310. if (s->encoding) {
  311. CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
  312. CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
  313. CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
  314. }
  315. CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
  316. CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
  317. CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(uint32_t))
  318. pic->mb_type= pic->mb_type_base + s->mb_stride+1;
  319. if(s->out_format == FMT_H264){
  320. for(i=0; i<2; i++){
  321. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+2) * sizeof(int16_t))
  322. pic->motion_val[i]= pic->motion_val_base[i]+2;
  323. CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
  324. }
  325. pic->motion_subsample_log2= 2;
  326. }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
  327. for(i=0; i<2; i++){
  328. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+2) * sizeof(int16_t))
  329. pic->motion_val[i]= pic->motion_val_base[i]+2;
  330. CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
  331. }
  332. pic->motion_subsample_log2= 3;
  333. }
  334. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  335. CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
  336. }
  337. pic->qstride= s->mb_stride;
  338. CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
  339. }
  340. //it might be nicer if the application would keep track of these but it would require a API change
  341. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  342. s->prev_pict_types[0]= s->pict_type;
  343. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
  344. pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
  345. return 0;
  346. fail: //for the CHECKED_ALLOCZ macro
  347. return -1;
  348. }
  349. /**
  350. * deallocates a picture
  351. */
  352. static void free_picture(MpegEncContext *s, Picture *pic){
  353. int i;
  354. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  355. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  356. }
  357. av_freep(&pic->mb_var);
  358. av_freep(&pic->mc_mb_var);
  359. av_freep(&pic->mb_mean);
  360. av_freep(&pic->mbskip_table);
  361. av_freep(&pic->qscale_table);
  362. av_freep(&pic->mb_type_base);
  363. av_freep(&pic->dct_coeff);
  364. av_freep(&pic->pan_scan);
  365. pic->mb_type= NULL;
  366. for(i=0; i<2; i++){
  367. av_freep(&pic->motion_val_base[i]);
  368. av_freep(&pic->ref_index[i]);
  369. }
  370. if(pic->type == FF_BUFFER_TYPE_SHARED){
  371. for(i=0; i<4; i++){
  372. pic->base[i]=
  373. pic->data[i]= NULL;
  374. }
  375. pic->type= 0;
  376. }
  377. }
  378. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
  379. int i;
  380. // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
  381. CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
  382. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
  383. //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
  384. CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t))
  385. s->rd_scratchpad= s->me.scratchpad;
  386. s->b_scratchpad= s->me.scratchpad;
  387. s->obmc_scratchpad= s->me.scratchpad + 16;
  388. if (s->encoding) {
  389. CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
  390. CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
  391. if(s->avctx->noise_reduction){
  392. CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
  393. }
  394. }
  395. CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
  396. s->block= s->blocks[0];
  397. for(i=0;i<12;i++){
  398. s->pblocks[i] = (short *)(&s->block[i]);
  399. }
  400. return 0;
  401. fail:
  402. return -1; //free() through MPV_common_end()
  403. }
  404. static void free_duplicate_context(MpegEncContext *s){
  405. if(s==NULL) return;
  406. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  407. av_freep(&s->me.scratchpad);
  408. s->rd_scratchpad=
  409. s->b_scratchpad=
  410. s->obmc_scratchpad= NULL;
  411. av_freep(&s->dct_error_sum);
  412. av_freep(&s->me.map);
  413. av_freep(&s->me.score_map);
  414. av_freep(&s->blocks);
  415. s->block= NULL;
  416. }
  417. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
  418. #define COPY(a) bak->a= src->a
  419. COPY(allocated_edge_emu_buffer);
  420. COPY(edge_emu_buffer);
  421. COPY(me.scratchpad);
  422. COPY(rd_scratchpad);
  423. COPY(b_scratchpad);
  424. COPY(obmc_scratchpad);
  425. COPY(me.map);
  426. COPY(me.score_map);
  427. COPY(blocks);
  428. COPY(block);
  429. COPY(start_mb_y);
  430. COPY(end_mb_y);
  431. COPY(me.map_generation);
  432. COPY(pb);
  433. COPY(dct_error_sum);
  434. COPY(dct_count[0]);
  435. COPY(dct_count[1]);
  436. #undef COPY
  437. }
  438. void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
  439. MpegEncContext bak;
  440. int i;
  441. //FIXME copy only needed parts
  442. //START_TIMER
  443. backup_duplicate_context(&bak, dst);
  444. memcpy(dst, src, sizeof(MpegEncContext));
  445. backup_duplicate_context(dst, &bak);
  446. for(i=0;i<12;i++){
  447. dst->pblocks[i] = (short *)(&dst->block[i]);
  448. }
  449. //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  450. }
  451. static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){
  452. #define COPY(a) dst->a= src->a
  453. COPY(pict_type);
  454. COPY(current_picture);
  455. COPY(f_code);
  456. COPY(b_code);
  457. COPY(qscale);
  458. COPY(lambda);
  459. COPY(lambda2);
  460. COPY(picture_in_gop_number);
  461. COPY(gop_picture_number);
  462. COPY(frame_pred_frame_dct); //FIXME dont set in encode_header
  463. COPY(progressive_frame); //FIXME dont set in encode_header
  464. COPY(partitioned_frame); //FIXME dont set in encode_header
  465. #undef COPY
  466. }
  467. /**
  468. * sets the given MpegEncContext to common defaults (same for encoding and decoding).
  469. * the changed fields will not depend upon the prior state of the MpegEncContext.
  470. */
  471. static void MPV_common_defaults(MpegEncContext *s){
  472. s->y_dc_scale_table=
  473. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  474. s->chroma_qscale_table= ff_default_chroma_qscale_table;
  475. s->progressive_frame= 1;
  476. s->progressive_sequence= 1;
  477. s->picture_structure= PICT_FRAME;
  478. s->coded_picture_number = 0;
  479. s->picture_number = 0;
  480. s->input_picture_number = 0;
  481. s->picture_in_gop_number = 0;
  482. s->f_code = 1;
  483. s->b_code = 1;
  484. }
  485. /**
  486. * sets the given MpegEncContext to defaults for decoding.
  487. * the changed fields will not depend upon the prior state of the MpegEncContext.
  488. */
  489. void MPV_decode_defaults(MpegEncContext *s){
  490. MPV_common_defaults(s);
  491. }
  492. /**
  493. * sets the given MpegEncContext to defaults for encoding.
  494. * the changed fields will not depend upon the prior state of the MpegEncContext.
  495. */
  496. #ifdef CONFIG_ENCODERS
  497. static void MPV_encode_defaults(MpegEncContext *s){
  498. static int done=0;
  499. MPV_common_defaults(s);
  500. if(!done){
  501. int i;
  502. done=1;
  503. default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
  504. memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
  505. memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
  506. for(i=-16; i<16; i++){
  507. default_fcode_tab[i + MAX_MV]= 1;
  508. }
  509. }
  510. s->me.mv_penalty= default_mv_penalty;
  511. s->fcode_tab= default_fcode_tab;
  512. }
  513. #endif //CONFIG_ENCODERS
  514. /**
  515. * init common structure for both encoder and decoder.
  516. * this assumes that some variables like width/height are already set
  517. */
  518. int MPV_common_init(MpegEncContext *s)
  519. {
  520. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  521. if(s->avctx->thread_count > MAX_THREADS || (16*s->avctx->thread_count > s->height && s->height)){
  522. av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
  523. return -1;
  524. }
  525. dsputil_init(&s->dsp, s->avctx);
  526. DCT_common_init(s);
  527. s->flags= s->avctx->flags;
  528. s->flags2= s->avctx->flags2;
  529. s->mb_width = (s->width + 15) / 16;
  530. s->mb_height = (s->height + 15) / 16;
  531. s->mb_stride = s->mb_width + 1;
  532. s->b8_stride = s->mb_width*2 + 1;
  533. s->b4_stride = s->mb_width*4 + 1;
  534. mb_array_size= s->mb_height * s->mb_stride;
  535. mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
  536. /* set chroma shifts */
  537. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
  538. &(s->chroma_y_shift) );
  539. /* set default edge pos, will be overriden in decode_header if needed */
  540. s->h_edge_pos= s->mb_width*16;
  541. s->v_edge_pos= s->mb_height*16;
  542. s->mb_num = s->mb_width * s->mb_height;
  543. s->block_wrap[0]=
  544. s->block_wrap[1]=
  545. s->block_wrap[2]=
  546. s->block_wrap[3]= s->b8_stride;
  547. s->block_wrap[4]=
  548. s->block_wrap[5]= s->mb_stride;
  549. y_size = s->b8_stride * (2 * s->mb_height + 1);
  550. c_size = s->mb_stride * (s->mb_height + 1);
  551. yc_size = y_size + 2 * c_size;
  552. /* convert fourcc to upper case */
  553. s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
  554. + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
  555. + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
  556. + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
  557. s->avctx->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
  558. + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
  559. + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
  560. + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
  561. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  562. CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
  563. for(y=0; y<s->mb_height; y++){
  564. for(x=0; x<s->mb_width; x++){
  565. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  566. }
  567. }
  568. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  569. if (s->encoding) {
  570. /* Allocate MV tables */
  571. CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  572. CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  573. CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  574. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  575. CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  576. CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  577. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  578. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  579. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  580. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  581. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  582. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  583. if(s->msmpeg4_version){
  584. CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
  585. }
  586. CHECKED_ALLOCZ(s->avctx->stats_out, 256);
  587. /* Allocate MB type table */
  588. CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t)) //needed for encoding
  589. CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
  590. CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
  591. CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
  592. CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
  593. CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
  594. CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  595. CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  596. if(s->avctx->noise_reduction){
  597. CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
  598. }
  599. }
  600. CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
  601. CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
  602. if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
  603. /* interlaced direct mode decoding tables */
  604. for(i=0; i<2; i++){
  605. int j, k;
  606. for(j=0; j<2; j++){
  607. for(k=0; k<2; k++){
  608. CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t))
  609. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  610. }
  611. CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t))
  612. CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t))
  613. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  614. }
  615. CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t))
  616. }
  617. }
  618. if (s->out_format == FMT_H263) {
  619. /* ac values */
  620. CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);
  621. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  622. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  623. s->ac_val[2] = s->ac_val[1] + c_size;
  624. /* cbp values */
  625. CHECKED_ALLOCZ(s->coded_block_base, y_size);
  626. s->coded_block= s->coded_block_base + s->b8_stride + 1;
  627. /* divx501 bitstream reorder buffer */
  628. CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
  629. /* cbp, ac_pred, pred_dir */
  630. CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
  631. CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
  632. }
  633. if (s->h263_pred || s->h263_plus || !s->encoding) {
  634. /* dc values */
  635. //MN: we need these for error resilience of intra-frames
  636. CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));
  637. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  638. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  639. s->dc_val[2] = s->dc_val[1] + c_size;
  640. for(i=0;i<yc_size;i++)
  641. s->dc_val_base[i] = 1024;
  642. }
  643. /* which mb is a intra block */
  644. CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
  645. memset(s->mbintra_table, 1, mb_array_size);
  646. /* init macroblock skip table */
  647. CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
  648. //Note the +1 is for a quicker mpeg4 slice_end detection
  649. CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  650. s->parse_context.state= -1;
  651. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  652. s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  653. s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
  654. s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
  655. }
  656. s->context_initialized = 1;
  657. s->thread_context[0]= s;
  658. for(i=1; i<s->avctx->thread_count; i++){
  659. s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
  660. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  661. }
  662. for(i=0; i<s->avctx->thread_count; i++){
  663. if(init_duplicate_context(s->thread_context[i], s) < 0)
  664. goto fail;
  665. s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
  666. s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
  667. }
  668. return 0;
  669. fail:
  670. MPV_common_end(s);
  671. return -1;
  672. }
  673. /* init common structure for both encoder and decoder */
  674. void MPV_common_end(MpegEncContext *s)
  675. {
  676. int i, j, k;
  677. for(i=0; i<s->avctx->thread_count; i++){
  678. free_duplicate_context(s->thread_context[i]);
  679. }
  680. for(i=1; i<s->avctx->thread_count; i++){
  681. av_freep(&s->thread_context[i]);
  682. }
  683. av_freep(&s->parse_context.buffer);
  684. s->parse_context.buffer_size=0;
  685. av_freep(&s->mb_type);
  686. av_freep(&s->p_mv_table_base);
  687. av_freep(&s->b_forw_mv_table_base);
  688. av_freep(&s->b_back_mv_table_base);
  689. av_freep(&s->b_bidir_forw_mv_table_base);
  690. av_freep(&s->b_bidir_back_mv_table_base);
  691. av_freep(&s->b_direct_mv_table_base);
  692. s->p_mv_table= NULL;
  693. s->b_forw_mv_table= NULL;
  694. s->b_back_mv_table= NULL;
  695. s->b_bidir_forw_mv_table= NULL;
  696. s->b_bidir_back_mv_table= NULL;
  697. s->b_direct_mv_table= NULL;
  698. for(i=0; i<2; i++){
  699. for(j=0; j<2; j++){
  700. for(k=0; k<2; k++){
  701. av_freep(&s->b_field_mv_table_base[i][j][k]);
  702. s->b_field_mv_table[i][j][k]=NULL;
  703. }
  704. av_freep(&s->b_field_select_table[i][j]);
  705. av_freep(&s->p_field_mv_table_base[i][j]);
  706. s->p_field_mv_table[i][j]=NULL;
  707. }
  708. av_freep(&s->p_field_select_table[i]);
  709. }
  710. av_freep(&s->dc_val_base);
  711. av_freep(&s->ac_val_base);
  712. av_freep(&s->coded_block_base);
  713. av_freep(&s->mbintra_table);
  714. av_freep(&s->cbp_table);
  715. av_freep(&s->pred_dir_table);
  716. av_freep(&s->mbskip_table);
  717. av_freep(&s->prev_pict_types);
  718. av_freep(&s->bitstream_buffer);
  719. av_freep(&s->avctx->stats_out);
  720. av_freep(&s->ac_stats);
  721. av_freep(&s->error_status_table);
  722. av_freep(&s->mb_index2xy);
  723. av_freep(&s->lambda_table);
  724. av_freep(&s->q_intra_matrix);
  725. av_freep(&s->q_inter_matrix);
  726. av_freep(&s->q_intra_matrix16);
  727. av_freep(&s->q_inter_matrix16);
  728. av_freep(&s->input_picture);
  729. av_freep(&s->reordered_input_picture);
  730. av_freep(&s->dct_offset);
  731. if(s->picture){
  732. for(i=0; i<MAX_PICTURE_COUNT; i++){
  733. free_picture(s, &s->picture[i]);
  734. }
  735. }
  736. av_freep(&s->picture);
  737. s->context_initialized = 0;
  738. s->last_picture_ptr=
  739. s->next_picture_ptr=
  740. s->current_picture_ptr= NULL;
  741. s->linesize= s->uvlinesize= 0;
  742. for(i=0; i<3; i++)
  743. av_freep(&s->visualization_buffer[i]);
  744. avcodec_default_free_buffers(s->avctx);
  745. }
  746. #ifdef CONFIG_ENCODERS
  747. /* init video encoder */
  748. int MPV_encode_init(AVCodecContext *avctx)
  749. {
  750. MpegEncContext *s = avctx->priv_data;
  751. int i, dummy;
  752. int chroma_h_shift, chroma_v_shift;
  753. MPV_encode_defaults(s);
  754. avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME
  755. s->bit_rate = avctx->bit_rate;
  756. s->width = avctx->width;
  757. s->height = avctx->height;
  758. if(avctx->gop_size > 600){
  759. av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
  760. avctx->gop_size=600;
  761. }
  762. s->gop_size = avctx->gop_size;
  763. s->avctx = avctx;
  764. s->flags= avctx->flags;
  765. s->flags2= avctx->flags2;
  766. s->max_b_frames= avctx->max_b_frames;
  767. s->codec_id= avctx->codec->id;
  768. s->luma_elim_threshold = avctx->luma_elim_threshold;
  769. s->chroma_elim_threshold= avctx->chroma_elim_threshold;
  770. s->strict_std_compliance= avctx->strict_std_compliance;
  771. s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
  772. s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
  773. s->mpeg_quant= avctx->mpeg_quant;
  774. s->rtp_mode= !!avctx->rtp_payload_size;
  775. s->intra_dc_precision= avctx->intra_dc_precision;
  776. s->user_specified_pts = AV_NOPTS_VALUE;
  777. if (s->gop_size <= 1) {
  778. s->intra_only = 1;
  779. s->gop_size = 12;
  780. } else {
  781. s->intra_only = 0;
  782. }
  783. s->me_method = avctx->me_method;
  784. /* Fixed QSCALE */
  785. s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
  786. s->adaptive_quant= ( s->avctx->lumi_masking
  787. || s->avctx->dark_masking
  788. || s->avctx->temporal_cplx_masking
  789. || s->avctx->spatial_cplx_masking
  790. || s->avctx->p_masking
  791. || (s->flags&CODEC_FLAG_QP_RD))
  792. && !s->fixed_qscale;
  793. s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
  794. s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
  795. s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
  796. if(avctx->rc_max_rate && !avctx->rc_buffer_size){
  797. av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
  798. return -1;
  799. }
  800. if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){
  801. av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isnt recommanded!\n");
  802. }
  803. if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){
  804. av_log(avctx, AV_LOG_INFO, "bitrate below min bitrate\n");
  805. return -1;
  806. }
  807. if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){
  808. av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
  809. return -1;
  810. }
  811. if( s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate
  812. && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO)
  813. && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){
  814. av_log(avctx, AV_LOG_INFO, "Warning vbv_delay will be set to 0xFFFF (=VBR) as the specified vbv buffer is too large for the given bitrate!\n");
  815. }
  816. if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
  817. && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){
  818. av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
  819. return -1;
  820. }
  821. if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
  822. av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decission\n");
  823. return -1;
  824. }
  825. if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
  826. av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
  827. return -1;
  828. }
  829. if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
  830. av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
  831. return -1;
  832. }
  833. if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
  834. av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
  835. return -1;
  836. }
  837. if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
  838. av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
  839. return -1;
  840. }
  841. if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN))
  842. && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){
  843. av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
  844. return -1;
  845. }
  846. if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
  847. av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supporetd by codec\n");
  848. return -1;
  849. }
  850. if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
  851. av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
  852. return -1;
  853. }
  854. if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
  855. av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
  856. return -1;
  857. }
  858. if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){
  859. av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection arent supported yet\n");
  860. return -1;
  861. }
  862. if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4
  863. && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO
  864. && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
  865. av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n");
  866. return -1;
  867. }
  868. if(s->avctx->thread_count > 1)
  869. s->rtp_mode= 1;
  870. i= ff_gcd(avctx->frame_rate, avctx->frame_rate_base);
  871. if(i > 1){
  872. av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
  873. avctx->frame_rate /= i;
  874. avctx->frame_rate_base /= i;
  875. // return -1;
  876. }
  877. if(s->codec_id==CODEC_ID_MJPEG){
  878. s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
  879. s->inter_quant_bias= 0;
  880. }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
  881. s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
  882. s->inter_quant_bias= 0;
  883. }else{
  884. s->intra_quant_bias=0;
  885. s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
  886. }
  887. if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
  888. s->intra_quant_bias= avctx->intra_quant_bias;
  889. if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
  890. s->inter_quant_bias= avctx->inter_quant_bias;
  891. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
  892. av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
  893. s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
  894. switch(avctx->codec->id) {
  895. case CODEC_ID_MPEG1VIDEO:
  896. s->out_format = FMT_MPEG1;
  897. s->low_delay= 0; //s->max_b_frames ? 0 : 1;
  898. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  899. break;
  900. case CODEC_ID_MPEG2VIDEO:
  901. s->out_format = FMT_MPEG1;
  902. s->low_delay= 0; //s->max_b_frames ? 0 : 1;
  903. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  904. s->rtp_mode= 1;
  905. break;
  906. case CODEC_ID_LJPEG:
  907. case CODEC_ID_MJPEG:
  908. s->out_format = FMT_MJPEG;
  909. s->intra_only = 1; /* force intra only for jpeg */
  910. s->mjpeg_write_tables = 1; /* write all tables */
  911. s->mjpeg_data_only_frames = 0; /* write all the needed headers */
  912. s->mjpeg_vsample[0] = 1<<chroma_v_shift;
  913. s->mjpeg_vsample[1] = 1;
  914. s->mjpeg_vsample[2] = 1;
  915. s->mjpeg_hsample[0] = 1<<chroma_h_shift;
  916. s->mjpeg_hsample[1] = 1;
  917. s->mjpeg_hsample[2] = 1;
  918. if (mjpeg_init(s) < 0)
  919. return -1;
  920. avctx->delay=0;
  921. s->low_delay=1;
  922. break;
  923. #ifdef CONFIG_RISKY
  924. case CODEC_ID_H263:
  925. if (h263_get_picture_format(s->width, s->height) == 7) {
  926. av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n");
  927. return -1;
  928. }
  929. s->out_format = FMT_H263;
  930. s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
  931. avctx->delay=0;
  932. s->low_delay=1;
  933. break;
  934. case CODEC_ID_H263P:
  935. s->out_format = FMT_H263;
  936. s->h263_plus = 1;
  937. /* Fx */
  938. s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
  939. s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
  940. s->modified_quant= s->h263_aic;
  941. s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
  942. s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
  943. s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
  944. s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
  945. s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
  946. /* /Fx */
  947. /* These are just to be sure */
  948. avctx->delay=0;
  949. s->low_delay=1;
  950. break;
  951. case CODEC_ID_FLV1:
  952. s->out_format = FMT_H263;
  953. s->h263_flv = 2; /* format = 1; 11-bit codes */
  954. s->unrestricted_mv = 1;
  955. s->rtp_mode=0; /* don't allow GOB */
  956. avctx->delay=0;
  957. s->low_delay=1;
  958. break;
  959. case CODEC_ID_RV10:
  960. s->out_format = FMT_H263;
  961. avctx->delay=0;
  962. s->low_delay=1;
  963. break;
  964. case CODEC_ID_MPEG4:
  965. s->out_format = FMT_H263;
  966. s->h263_pred = 1;
  967. s->unrestricted_mv = 1;
  968. s->low_delay= s->max_b_frames ? 0 : 1;
  969. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  970. break;
  971. case CODEC_ID_MSMPEG4V1:
  972. s->out_format = FMT_H263;
  973. s->h263_msmpeg4 = 1;
  974. s->h263_pred = 1;
  975. s->unrestricted_mv = 1;
  976. s->msmpeg4_version= 1;
  977. avctx->delay=0;
  978. s->low_delay=1;
  979. break;
  980. case CODEC_ID_MSMPEG4V2:
  981. s->out_format = FMT_H263;
  982. s->h263_msmpeg4 = 1;
  983. s->h263_pred = 1;
  984. s->unrestricted_mv = 1;
  985. s->msmpeg4_version= 2;
  986. avctx->delay=0;
  987. s->low_delay=1;
  988. break;
  989. case CODEC_ID_MSMPEG4V3:
  990. s->out_format = FMT_H263;
  991. s->h263_msmpeg4 = 1;
  992. s->h263_pred = 1;
  993. s->unrestricted_mv = 1;
  994. s->msmpeg4_version= 3;
  995. s->flipflop_rounding=1;
  996. avctx->delay=0;
  997. s->low_delay=1;
  998. break;
  999. case CODEC_ID_WMV1:
  1000. s->out_format = FMT_H263;
  1001. s->h263_msmpeg4 = 1;
  1002. s->h263_pred = 1;
  1003. s->unrestricted_mv = 1;
  1004. s->msmpeg4_version= 4;
  1005. s->flipflop_rounding=1;
  1006. avctx->delay=0;
  1007. s->low_delay=1;
  1008. break;
  1009. case CODEC_ID_WMV2:
  1010. s->out_format = FMT_H263;
  1011. s->h263_msmpeg4 = 1;
  1012. s->h263_pred = 1;
  1013. s->unrestricted_mv = 1;
  1014. s->msmpeg4_version= 5;
  1015. s->flipflop_rounding=1;
  1016. avctx->delay=0;
  1017. s->low_delay=1;
  1018. break;
  1019. #endif
  1020. default:
  1021. return -1;
  1022. }
  1023. avctx->has_b_frames= !s->low_delay;
  1024. s->encoding = 1;
  1025. /* init */
  1026. if (MPV_common_init(s) < 0)
  1027. return -1;
  1028. if(s->modified_quant)
  1029. s->chroma_qscale_table= ff_h263_chroma_qscale_table;
  1030. s->progressive_frame=
  1031. s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME));
  1032. s->quant_precision=5;
  1033. ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
  1034. #ifdef CONFIG_ENCODERS
  1035. #ifdef CONFIG_RISKY
  1036. if (s->out_format == FMT_H263)
  1037. h263_encode_init(s);
  1038. if(s->msmpeg4_version)
  1039. ff_msmpeg4_encode_init(s);
  1040. #endif
  1041. if (s->out_format == FMT_MPEG1)
  1042. ff_mpeg1_encode_init(s);
  1043. #endif
  1044. /* init q matrix */
  1045. for(i=0;i<64;i++) {
  1046. int j= s->dsp.idct_permutation[i];
  1047. #ifdef CONFIG_RISKY
  1048. if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
  1049. s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
  1050. s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
  1051. }else if(s->out_format == FMT_H263){
  1052. s->intra_matrix[j] =
  1053. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  1054. }else
  1055. #endif
  1056. { /* mpeg1/2 */
  1057. s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
  1058. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  1059. }
  1060. if(s->avctx->intra_matrix)
  1061. s->intra_matrix[j] = s->avctx->intra_matrix[i];
  1062. if(s->avctx->inter_matrix)
  1063. s->inter_matrix[j] = s->avctx->inter_matrix[i];
  1064. }
  1065. /* precompute matrix */
  1066. /* for mjpeg, we do include qscale in the matrix */
  1067. if (s->out_format != FMT_MJPEG) {
  1068. convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  1069. s->intra_matrix, s->intra_quant_bias, 1, 31);
  1070. convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
  1071. s->inter_matrix, s->inter_quant_bias, 1, 31);
  1072. }
  1073. if(ff_rate_control_init(s) < 0)
  1074. return -1;
  1075. return 0;
  1076. }
  1077. int MPV_encode_end(AVCodecContext *avctx)
  1078. {
  1079. MpegEncContext *s = avctx->priv_data;
  1080. #ifdef STATS
  1081. print_stats();
  1082. #endif
  1083. ff_rate_control_uninit(s);
  1084. MPV_common_end(s);
  1085. if (s->out_format == FMT_MJPEG)
  1086. mjpeg_close(s);
  1087. av_freep(&avctx->extradata);
  1088. return 0;
  1089. }
  1090. #endif //CONFIG_ENCODERS
  1091. void init_rl(RLTable *rl)
  1092. {
  1093. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  1094. uint8_t index_run[MAX_RUN+1];
  1095. int last, run, level, start, end, i;
  1096. /* compute max_level[], max_run[] and index_run[] */
  1097. for(last=0;last<2;last++) {
  1098. if (last == 0) {
  1099. start = 0;
  1100. end = rl->last;
  1101. } else {
  1102. start = rl->last;
  1103. end = rl->n;
  1104. }
  1105. memset(max_level, 0, MAX_RUN + 1);
  1106. memset(max_run, 0, MAX_LEVEL + 1);
  1107. memset(index_run, rl->n, MAX_RUN + 1);
  1108. for(i=start;i<end;i++) {
  1109. run = rl->table_run[i];
  1110. level = rl->table_level[i];
  1111. if (index_run[run] == rl->n)
  1112. index_run[run] = i;
  1113. if (level > max_level[run])
  1114. max_level[run] = level;
  1115. if (run > max_run[level])
  1116. max_run[level] = run;
  1117. }
  1118. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1119. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1120. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1121. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1122. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1123. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1124. }
  1125. }
  1126. /* draw the edges of width 'w' of an image of size width, height */
  1127. //FIXME check that this is ok for mpeg4 interlaced
  1128. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
  1129. {
  1130. uint8_t *ptr, *last_line;
  1131. int i;
  1132. last_line = buf + (height - 1) * wrap;
  1133. for(i=0;i<w;i++) {
  1134. /* top and bottom */
  1135. memcpy(buf - (i + 1) * wrap, buf, width);
  1136. memcpy(last_line + (i + 1) * wrap, last_line, width);
  1137. }
  1138. /* left and right */
  1139. ptr = buf;
  1140. for(i=0;i<height;i++) {
  1141. memset(ptr - w, ptr[0], w);
  1142. memset(ptr + width, ptr[width-1], w);
  1143. ptr += wrap;
  1144. }
  1145. /* corners */
  1146. for(i=0;i<w;i++) {
  1147. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  1148. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  1149. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  1150. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  1151. }
  1152. }
  1153. int ff_find_unused_picture(MpegEncContext *s, int shared){
  1154. int i;
  1155. if(shared){
  1156. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1157. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
  1158. }
  1159. }else{
  1160. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1161. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
  1162. }
  1163. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1164. if(s->picture[i].data[0]==NULL) return i;
  1165. }
  1166. }
  1167. assert(0);
  1168. return -1;
  1169. }
  1170. static void update_noise_reduction(MpegEncContext *s){
  1171. int intra, i;
  1172. for(intra=0; intra<2; intra++){
  1173. if(s->dct_count[intra] > (1<<16)){
  1174. for(i=0; i<64; i++){
  1175. s->dct_error_sum[intra][i] >>=1;
  1176. }
  1177. s->dct_count[intra] >>= 1;
  1178. }
  1179. for(i=0; i<64; i++){
  1180. s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
  1181. }
  1182. }
  1183. }
  1184. /**
  1185. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  1186. */
  1187. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1188. {
  1189. int i;
  1190. AVFrame *pic;
  1191. s->mb_skiped = 0;
  1192. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  1193. /* mark&release old frames */
  1194. if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
  1195. avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
  1196. /* release forgotten pictures */
  1197. /* if(mpeg124/h263) */
  1198. if(!s->encoding){
  1199. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1200. if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
  1201. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  1202. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  1203. }
  1204. }
  1205. }
  1206. }
  1207. alloc:
  1208. if(!s->encoding){
  1209. /* release non refernce frames */
  1210. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1211. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  1212. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  1213. }
  1214. }
  1215. if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
  1216. pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
  1217. else{
  1218. i= ff_find_unused_picture(s, 0);
  1219. pic= (AVFrame*)&s->picture[i];
  1220. }
  1221. pic->reference= s->pict_type != B_TYPE && !s->dropable ? 3 : 0;
  1222. pic->coded_picture_number= s->coded_picture_number++;
  1223. if( alloc_picture(s, (Picture*)pic, 0) < 0)
  1224. return -1;
  1225. s->current_picture_ptr= (Picture*)pic;
  1226. s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
  1227. s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
  1228. }
  1229. s->current_picture_ptr->pict_type= s->pict_type;
  1230. // if(s->flags && CODEC_FLAG_QSCALE)
  1231. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  1232. s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
  1233. copy_picture(&s->current_picture, s->current_picture_ptr);
  1234. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  1235. if (s->pict_type != B_TYPE) {
  1236. s->last_picture_ptr= s->next_picture_ptr;
  1237. if(!s->dropable)
  1238. s->next_picture_ptr= s->current_picture_ptr;
  1239. }
  1240. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1241. s->last_picture_ptr ? s->last_picture_ptr->data[0] : NULL,
  1242. s->next_picture_ptr ? s->next_picture_ptr->data[0] : NULL,
  1243. s->current_picture_ptr ? s->current_picture_ptr->data[0] : NULL,
  1244. s->pict_type, s->dropable);*/
  1245. if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
  1246. if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
  1247. if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
  1248. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  1249. assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
  1250. goto alloc;
  1251. }
  1252. assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  1253. if(s->picture_structure!=PICT_FRAME){
  1254. int i;
  1255. for(i=0; i<4; i++){
  1256. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1257. s->current_picture.data[i] += s->current_picture.linesize[i];
  1258. }
  1259. s->current_picture.linesize[i] *= 2;
  1260. s->last_picture.linesize[i] *=2;
  1261. s->next_picture.linesize[i] *=2;
  1262. }
  1263. }
  1264. }
  1265. s->hurry_up= s->avctx->hurry_up;
  1266. s->error_resilience= avctx->error_resilience;
  1267. /* set dequantizer, we cant do it during init as it might change for mpeg4
  1268. and we cant do it in the header decode as init isnt called for mpeg4 there yet */
  1269. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  1270. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1271. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1272. }else if(s->out_format == FMT_H263){
  1273. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1274. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1275. }else if(s->out_format == FMT_H261){
  1276. s->dct_unquantize_intra = s->dct_unquantize_h261_intra;
  1277. s->dct_unquantize_inter = s->dct_unquantize_h261_inter;
  1278. }else{
  1279. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1280. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1281. }
  1282. if(s->dct_error_sum){
  1283. assert(s->avctx->noise_reduction && s->encoding);
  1284. update_noise_reduction(s);
  1285. }
  1286. #ifdef HAVE_XVMC
  1287. if(s->avctx->xvmc_acceleration)
  1288. return XVMC_field_start(s, avctx);
  1289. #endif
  1290. return 0;
  1291. }
  1292. /* generic function for encode/decode called after a frame has been coded/decoded */
  1293. void MPV_frame_end(MpegEncContext *s)
  1294. {
  1295. int i;
  1296. /* draw edge for correct motion prediction if outside */
  1297. #ifdef HAVE_XVMC
  1298. //just to make sure that all data is rendered.
  1299. if(s->avctx->xvmc_acceleration){
  1300. XVMC_field_end(s);
  1301. }else
  1302. #endif
  1303. if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  1304. draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  1305. draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  1306. draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  1307. }
  1308. emms_c();
  1309. s->last_pict_type = s->pict_type;
  1310. if(s->pict_type!=B_TYPE){
  1311. s->last_non_b_pict_type= s->pict_type;
  1312. }
  1313. #if 0
  1314. /* copy back current_picture variables */
  1315. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1316. if(s->picture[i].data[0] == s->current_picture.data[0]){
  1317. s->picture[i]= s->current_picture;
  1318. break;
  1319. }
  1320. }
  1321. assert(i<MAX_PICTURE_COUNT);
  1322. #endif
  1323. if(s->encoding){
  1324. /* release non refernce frames */
  1325. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1326. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  1327. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  1328. }
  1329. }
  1330. }
  1331. // clear copies, to avoid confusion
  1332. #if 0
  1333. memset(&s->last_picture, 0, sizeof(Picture));
  1334. memset(&s->next_picture, 0, sizeof(Picture));
  1335. memset(&s->current_picture, 0, sizeof(Picture));
  1336. #endif
  1337. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  1338. }
  1339. /**
  1340. * draws an line from (ex, ey) -> (sx, sy).
  1341. * @param w width of the image
  1342. * @param h height of the image
  1343. * @param stride stride/linesize of the image
  1344. * @param color color of the arrow
  1345. */
  1346. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1347. int t, x, y, fr, f;
  1348. sx= clip(sx, 0, w-1);
  1349. sy= clip(sy, 0, h-1);
  1350. ex= clip(ex, 0, w-1);
  1351. ey= clip(ey, 0, h-1);
  1352. buf[sy*stride + sx]+= color;
  1353. if(ABS(ex - sx) > ABS(ey - sy)){
  1354. if(sx > ex){
  1355. t=sx; sx=ex; ex=t;
  1356. t=sy; sy=ey; ey=t;
  1357. }
  1358. buf+= sx + sy*stride;
  1359. ex-= sx;
  1360. f= ((ey-sy)<<16)/ex;
  1361. for(x= 0; x <= ex; x++){
  1362. y = (x*f)>>16;
  1363. fr= (x*f)&0xFFFF;
  1364. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  1365. buf[(y+1)*stride + x]+= (color* fr )>>16;
  1366. }
  1367. }else{
  1368. if(sy > ey){
  1369. t=sx; sx=ex; ex=t;
  1370. t=sy; sy=ey; ey=t;
  1371. }
  1372. buf+= sx + sy*stride;
  1373. ey-= sy;
  1374. if(ey) f= ((ex-sx)<<16)/ey;
  1375. else f= 0;
  1376. for(y= 0; y <= ey; y++){
  1377. x = (y*f)>>16;
  1378. fr= (y*f)&0xFFFF;
  1379. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;;
  1380. buf[y*stride + x+1]+= (color* fr )>>16;;
  1381. }
  1382. }
  1383. }
  1384. /**
  1385. * draws an arrow from (ex, ey) -> (sx, sy).
  1386. * @param w width of the image
  1387. * @param h height of the image
  1388. * @param stride stride/linesize of the image
  1389. * @param color color of the arrow
  1390. */
  1391. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1392. int dx,dy;
  1393. sx= clip(sx, -100, w+100);
  1394. sy= clip(sy, -100, h+100);
  1395. ex= clip(ex, -100, w+100);
  1396. ey= clip(ey, -100, h+100);
  1397. dx= ex - sx;
  1398. dy= ey - sy;
  1399. if(dx*dx + dy*dy > 3*3){
  1400. int rx= dx + dy;
  1401. int ry= -dx + dy;
  1402. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  1403. //FIXME subpixel accuracy
  1404. rx= ROUNDED_DIV(rx*3<<4, length);
  1405. ry= ROUNDED_DIV(ry*3<<4, length);
  1406. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1407. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1408. }
  1409. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1410. }
  1411. /**
  1412. * prints debuging info for the given picture.
  1413. */
  1414. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  1415. if(!pict || !pict->mb_type) return;
  1416. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  1417. int x,y;
  1418. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1419. switch (pict->pict_type) {
  1420. case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
  1421. case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
  1422. case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
  1423. case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
  1424. case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
  1425. case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
  1426. }
  1427. for(y=0; y<s->mb_height; y++){
  1428. for(x=0; x<s->mb_width; x++){
  1429. if(s->avctx->debug&FF_DEBUG_SKIP){
  1430. int count= s->mbskip_table[x + y*s->mb_stride];
  1431. if(count>9) count=9;
  1432. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1433. }
  1434. if(s->avctx->debug&FF_DEBUG_QP){
  1435. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1436. }
  1437. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1438. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1439. //Type & MV direction
  1440. if(IS_PCM(mb_type))
  1441. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1442. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1443. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1444. else if(IS_INTRA4x4(mb_type))
  1445. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1446. else if(IS_INTRA16x16(mb_type))
  1447. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1448. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1449. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1450. else if(IS_DIRECT(mb_type))
  1451. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1452. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1453. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1454. else if(IS_GMC(mb_type))
  1455. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1456. else if(IS_SKIP(mb_type))
  1457. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1458. else if(!USES_LIST(mb_type, 1))
  1459. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1460. else if(!USES_LIST(mb_type, 0))
  1461. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1462. else{
  1463. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1464. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1465. }
  1466. //segmentation
  1467. if(IS_8X8(mb_type))
  1468. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1469. else if(IS_16X8(mb_type))
  1470. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1471. else if(IS_8X16(mb_type))
  1472. av_log(s->avctx, AV_LOG_DEBUG, "¦");
  1473. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1474. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1475. else
  1476. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1477. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
  1478. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1479. else
  1480. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1481. }
  1482. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1483. }
  1484. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1485. }
  1486. }
  1487. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1488. const int shift= 1 + s->quarter_sample;
  1489. int mb_y;
  1490. uint8_t *ptr;
  1491. int i;
  1492. int h_chroma_shift, v_chroma_shift;
  1493. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1494. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1495. for(i=0; i<3; i++){
  1496. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*s->height:pict->linesize[i]*s->height >> v_chroma_shift);
  1497. pict->data[i]= s->visualization_buffer[i];
  1498. }
  1499. pict->type= FF_BUFFER_TYPE_COPY;
  1500. ptr= pict->data[0];
  1501. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1502. int mb_x;
  1503. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1504. const int mb_index= mb_x + mb_y*s->mb_stride;
  1505. if((s->avctx->debug_mv) && pict->motion_val){
  1506. int type;
  1507. for(type=0; type<3; type++){
  1508. int direction = 0;
  1509. switch (type) {
  1510. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
  1511. continue;
  1512. direction = 0;
  1513. break;
  1514. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
  1515. continue;
  1516. direction = 0;
  1517. break;
  1518. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
  1519. continue;
  1520. direction = 1;
  1521. break;
  1522. }
  1523. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1524. continue;
  1525. //FIXME for h264
  1526. if(IS_8X8(pict->mb_type[mb_index])){
  1527. int i;
  1528. for(i=0; i<4; i++){
  1529. int sx= mb_x*16 + 4 + 8*(i&1);
  1530. int sy= mb_y*16 + 4 + 8*(i>>1);
  1531. int xy= mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*s->b8_stride;
  1532. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1533. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1534. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  1535. }
  1536. }else if(IS_16X8(pict->mb_type[mb_index])){
  1537. int i;
  1538. for(i=0; i<2; i++){
  1539. int sx=mb_x*16 + 8;
  1540. int sy=mb_y*16 + 4 + 8*i;
  1541. int xy= mb_x*2 + (mb_y*2 + i)*s->b8_stride;
  1542. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1543. int my=(pict->motion_val[direction][xy][1]>>shift);
  1544. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1545. my*=2;
  1546. draw_arrow(ptr, sx, sy, mx+sx, my+sy, s->width, s->height, s->linesize, 100);
  1547. }
  1548. }else{
  1549. int sx= mb_x*16 + 8;
  1550. int sy= mb_y*16 + 8;
  1551. int xy= mb_x*2 + mb_y*2*s->b8_stride;
  1552. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1553. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1554. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  1555. }
  1556. }
  1557. }
  1558. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1559. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1560. int y;
  1561. for(y=0; y<8; y++){
  1562. *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= c;
  1563. *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= c;
  1564. }
  1565. }
  1566. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1567. int mb_type= pict->mb_type[mb_index];
  1568. uint64_t u,v;
  1569. int y;
  1570. #define COLOR(theta, r)\
  1571. u= (int)(128 + r*cos(theta*3.141592/180));\
  1572. v= (int)(128 + r*sin(theta*3.141592/180));
  1573. u=v=128;
  1574. if(IS_PCM(mb_type)){
  1575. COLOR(120,48)
  1576. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1577. COLOR(30,48)
  1578. }else if(IS_INTRA4x4(mb_type)){
  1579. COLOR(90,48)
  1580. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1581. // COLOR(120,48)
  1582. }else if(IS_DIRECT(mb_type)){
  1583. COLOR(150,48)
  1584. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1585. COLOR(170,48)
  1586. }else if(IS_GMC(mb_type)){
  1587. COLOR(190,48)
  1588. }else if(IS_SKIP(mb_type)){
  1589. // COLOR(180,48)
  1590. }else if(!USES_LIST(mb_type, 1)){
  1591. COLOR(240,48)
  1592. }else if(!USES_LIST(mb_type, 0)){
  1593. COLOR(0,48)
  1594. }else{
  1595. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1596. COLOR(300,48)
  1597. }
  1598. u*= 0x0101010101010101ULL;
  1599. v*= 0x0101010101010101ULL;
  1600. for(y=0; y<8; y++){
  1601. *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= u;
  1602. *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= v;
  1603. }
  1604. //segmentation
  1605. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1606. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1607. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1608. }
  1609. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1610. for(y=0; y<16; y++)
  1611. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1612. }
  1613. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1614. // hmm
  1615. }
  1616. }
  1617. s->mbskip_table[mb_index]=0;
  1618. }
  1619. }
  1620. }
  1621. }
  1622. #ifdef CONFIG_ENCODERS
  1623. static int get_sae(uint8_t *src, int ref, int stride){
  1624. int x,y;
  1625. int acc=0;
  1626. for(y=0; y<16; y++){
  1627. for(x=0; x<16; x++){
  1628. acc+= ABS(src[x+y*stride] - ref);
  1629. }
  1630. }
  1631. return acc;
  1632. }
  1633. static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
  1634. int x, y, w, h;
  1635. int acc=0;
  1636. w= s->width &~15;
  1637. h= s->height&~15;
  1638. for(y=0; y<h; y+=16){
  1639. for(x=0; x<w; x+=16){
  1640. int offset= x + y*stride;
  1641. int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
  1642. int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
  1643. int sae = get_sae(src + offset, mean, stride);
  1644. acc+= sae + 500 < sad;
  1645. }
  1646. }
  1647. return acc;
  1648. }
  1649. static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
  1650. AVFrame *pic=NULL;
  1651. int i;
  1652. const int encoding_delay= s->max_b_frames;
  1653. int direct=1;
  1654. if(pic_arg){
  1655. if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
  1656. if(pic_arg->linesize[0] != s->linesize) direct=0;
  1657. if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
  1658. if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
  1659. // av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
  1660. if(direct){
  1661. i= ff_find_unused_picture(s, 1);
  1662. pic= (AVFrame*)&s->picture[i];
  1663. pic->reference= 3;
  1664. for(i=0; i<4; i++){
  1665. pic->data[i]= pic_arg->data[i];
  1666. pic->linesize[i]= pic_arg->linesize[i];
  1667. }
  1668. alloc_picture(s, (Picture*)pic, 1);
  1669. }else{
  1670. int offset= 16;
  1671. i= ff_find_unused_picture(s, 0);
  1672. pic= (AVFrame*)&s->picture[i];
  1673. pic->reference= 3;
  1674. alloc_picture(s, (Picture*)pic, 0);
  1675. if( pic->data[0] + offset == pic_arg->data[0]
  1676. && pic->data[1] + offset == pic_arg->data[1]
  1677. && pic->data[2] + offset == pic_arg->data[2]){
  1678. // empty
  1679. }else{
  1680. int h_chroma_shift, v_chroma_shift;
  1681. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1682. for(i=0; i<3; i++){
  1683. int src_stride= pic_arg->linesize[i];
  1684. int dst_stride= i ? s->uvlinesize : s->linesize;
  1685. int h_shift= i ? h_chroma_shift : 0;
  1686. int v_shift= i ? v_chroma_shift : 0;
  1687. int w= s->width >>h_shift;
  1688. int h= s->height>>v_shift;
  1689. uint8_t *src= pic_arg->data[i];
  1690. uint8_t *dst= pic->data[i] + offset;
  1691. if(src_stride==dst_stride)
  1692. memcpy(dst, src, src_stride*h);
  1693. else{
  1694. while(h--){
  1695. memcpy(dst, src, w);
  1696. dst += dst_stride;
  1697. src += src_stride;
  1698. }
  1699. }
  1700. }
  1701. }
  1702. }
  1703. copy_picture_attributes(s, pic, pic_arg);
  1704. pic->display_picture_number= s->input_picture_number++;
  1705. if(pic->pts != AV_NOPTS_VALUE){
  1706. if(s->user_specified_pts != AV_NOPTS_VALUE){
  1707. int64_t time= av_rescale(pic->pts, s->avctx->frame_rate, s->avctx->frame_rate_base*(int64_t)AV_TIME_BASE);
  1708. int64_t last= av_rescale(s->user_specified_pts, s->avctx->frame_rate, s->avctx->frame_rate_base*(int64_t)AV_TIME_BASE);
  1709. if(time <= last){
  1710. av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%Ld, last=%Ld\n", pic->pts, s->user_specified_pts);
  1711. return -1;
  1712. }
  1713. }
  1714. }else{
  1715. if(s->user_specified_pts != AV_NOPTS_VALUE){
  1716. pic->pts= s->user_specified_pts + AV_TIME_BASE*(int64_t)s->avctx->frame_rate_base / s->avctx->frame_rate;
  1717. av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%Ld)\n", pic->pts);
  1718. }else{
  1719. pic->pts= av_rescale(pic->display_picture_number*(int64_t)s->avctx->frame_rate_base, AV_TIME_BASE, s->avctx->frame_rate);
  1720. }
  1721. }
  1722. s->user_specified_pts= pic->pts;
  1723. }
  1724. /* shift buffer entries */
  1725. for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
  1726. s->input_picture[i-1]= s->input_picture[i];
  1727. s->input_picture[encoding_delay]= (Picture*)pic;
  1728. return 0;
  1729. }
  1730. static void select_input_picture(MpegEncContext *s){
  1731. int i;
  1732. for(i=1; i<MAX_PICTURE_COUNT; i++)
  1733. s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
  1734. s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
  1735. /* set next picture types & ordering */
  1736. if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
  1737. if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
  1738. s->reordered_input_picture[0]= s->input_picture[0];
  1739. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1740. s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
  1741. }else{
  1742. int b_frames;
  1743. if(s->flags&CODEC_FLAG_PASS2){
  1744. for(i=0; i<s->max_b_frames+1; i++){
  1745. int pict_num= s->input_picture[0]->display_picture_number + i;
  1746. int pict_type= s->rc_context.entry[pict_num].new_pict_type;
  1747. s->input_picture[i]->pict_type= pict_type;
  1748. if(i + 1 >= s->rc_context.num_entries) break;
  1749. }
  1750. }
  1751. if(s->input_picture[0]->pict_type){
  1752. /* user selected pict_type */
  1753. for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
  1754. if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
  1755. }
  1756. if(b_frames > s->max_b_frames){
  1757. av_log(s->avctx, AV_LOG_ERROR, "warning, too many bframes in a row\n");
  1758. b_frames = s->max_b_frames;
  1759. }
  1760. }else if(s->avctx->b_frame_strategy==0){
  1761. b_frames= s->max_b_frames;
  1762. while(b_frames && !s->input_picture[b_frames]) b_frames--;
  1763. }else if(s->avctx->b_frame_strategy==1){
  1764. for(i=1; i<s->max_b_frames+1; i++){
  1765. if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
  1766. s->input_picture[i]->b_frame_score=
  1767. get_intra_count(s, s->input_picture[i ]->data[0],
  1768. s->input_picture[i-1]->data[0], s->linesize) + 1;
  1769. }
  1770. }
  1771. for(i=0; i<s->max_b_frames; i++){
  1772. if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
  1773. }
  1774. b_frames= FFMAX(0, i-1);
  1775. /* reset scores */
  1776. for(i=0; i<b_frames+1; i++){
  1777. s->input_picture[i]->b_frame_score=0;
  1778. }
  1779. }else{
  1780. av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
  1781. b_frames=0;
  1782. }
  1783. emms_c();
  1784. //static int b_count=0;
  1785. //b_count+= b_frames;
  1786. //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
  1787. if(s->picture_in_gop_number + b_frames >= s->gop_size){
  1788. if(s->flags & CODEC_FLAG_CLOSED_GOP)
  1789. b_frames=0;
  1790. s->input_picture[b_frames]->pict_type= I_TYPE;
  1791. }
  1792. if( (s->flags & CODEC_FLAG_CLOSED_GOP)
  1793. && b_frames
  1794. && s->input_picture[b_frames]->pict_type== I_TYPE)
  1795. b_frames--;
  1796. s->reordered_input_picture[0]= s->input_picture[b_frames];
  1797. if(s->reordered_input_picture[0]->pict_type != I_TYPE)
  1798. s->reordered_input_picture[0]->pict_type= P_TYPE;
  1799. s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
  1800. for(i=0; i<b_frames; i++){
  1801. s->reordered_input_picture[i+1]= s->input_picture[i];
  1802. s->reordered_input_picture[i+1]->pict_type= B_TYPE;
  1803. s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++;
  1804. }
  1805. }
  1806. }
  1807. if(s->reordered_input_picture[0]){
  1808. s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
  1809. copy_picture(&s->new_picture, s->reordered_input_picture[0]);
  1810. if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
  1811. // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
  1812. int i= ff_find_unused_picture(s, 0);
  1813. Picture *pic= &s->picture[i];
  1814. /* mark us unused / free shared pic */
  1815. for(i=0; i<4; i++)
  1816. s->reordered_input_picture[0]->data[i]= NULL;
  1817. s->reordered_input_picture[0]->type= 0;
  1818. pic->reference = s->reordered_input_picture[0]->reference;
  1819. alloc_picture(s, pic, 0);
  1820. copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
  1821. s->current_picture_ptr= pic;
  1822. }else{
  1823. // input is not a shared pix -> reuse buffer for current_pix
  1824. assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
  1825. || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  1826. s->current_picture_ptr= s->reordered_input_picture[0];
  1827. for(i=0; i<4; i++){
  1828. s->new_picture.data[i]+=16;
  1829. }
  1830. }
  1831. copy_picture(&s->current_picture, s->current_picture_ptr);
  1832. s->picture_number= s->new_picture.display_picture_number;
  1833. //printf("dpn:%d\n", s->picture_number);
  1834. }else{
  1835. memset(&s->new_picture, 0, sizeof(Picture));
  1836. }
  1837. }
  1838. int MPV_encode_picture(AVCodecContext *avctx,
  1839. unsigned char *buf, int buf_size, void *data)
  1840. {
  1841. MpegEncContext *s = avctx->priv_data;
  1842. AVFrame *pic_arg = data;
  1843. int i, stuffing_count;
  1844. if(avctx->pix_fmt != PIX_FMT_YUV420P){
  1845. av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
  1846. return -1;
  1847. }
  1848. for(i=0; i<avctx->thread_count; i++){
  1849. int start_y= s->thread_context[i]->start_mb_y;
  1850. int end_y= s->thread_context[i]-> end_mb_y;
  1851. int h= s->mb_height;
  1852. uint8_t *start= buf + buf_size*start_y/h;
  1853. uint8_t *end = buf + buf_size* end_y/h;
  1854. init_put_bits(&s->thread_context[i]->pb, start, end - start);
  1855. }
  1856. s->picture_in_gop_number++;
  1857. if(load_input_picture(s, pic_arg) < 0)
  1858. return -1;
  1859. select_input_picture(s);
  1860. /* output? */
  1861. if(s->new_picture.data[0]){
  1862. s->pict_type= s->new_picture.pict_type;
  1863. //emms_c();
  1864. //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
  1865. MPV_frame_start(s, avctx);
  1866. encode_picture(s, s->picture_number);
  1867. avctx->real_pict_num = s->picture_number;
  1868. avctx->header_bits = s->header_bits;
  1869. avctx->mv_bits = s->mv_bits;
  1870. avctx->misc_bits = s->misc_bits;
  1871. avctx->i_tex_bits = s->i_tex_bits;
  1872. avctx->p_tex_bits = s->p_tex_bits;
  1873. avctx->i_count = s->i_count;
  1874. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  1875. avctx->skip_count = s->skip_count;
  1876. MPV_frame_end(s);
  1877. if (s->out_format == FMT_MJPEG)
  1878. mjpeg_picture_trailer(s);
  1879. if(s->flags&CODEC_FLAG_PASS1)
  1880. ff_write_pass1_stats(s);
  1881. for(i=0; i<4; i++){
  1882. avctx->error[i] += s->current_picture_ptr->error[i];
  1883. }
  1884. flush_put_bits(&s->pb);
  1885. s->frame_bits = put_bits_count(&s->pb);
  1886. stuffing_count= ff_vbv_update(s, s->frame_bits);
  1887. if(stuffing_count){
  1888. switch(s->codec_id){
  1889. case CODEC_ID_MPEG1VIDEO:
  1890. case CODEC_ID_MPEG2VIDEO:
  1891. while(stuffing_count--){
  1892. put_bits(&s->pb, 8, 0);
  1893. }
  1894. break;
  1895. case CODEC_ID_MPEG4:
  1896. put_bits(&s->pb, 16, 0);
  1897. put_bits(&s->pb, 16, 0x1C3);
  1898. stuffing_count -= 4;
  1899. while(stuffing_count--){
  1900. put_bits(&s->pb, 8, 0xFF);
  1901. }
  1902. break;
  1903. default:
  1904. av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
  1905. }
  1906. flush_put_bits(&s->pb);
  1907. s->frame_bits = put_bits_count(&s->pb);
  1908. }
  1909. /* update mpeg1/2 vbv_delay for CBR */
  1910. if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1
  1911. && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){
  1912. int vbv_delay;
  1913. assert(s->repeat_first_field==0);
  1914. vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate);
  1915. assert(vbv_delay < 0xFFFF);
  1916. s->vbv_delay_ptr[0] &= 0xF8;
  1917. s->vbv_delay_ptr[0] |= vbv_delay>>13;
  1918. s->vbv_delay_ptr[1] = vbv_delay>>5;
  1919. s->vbv_delay_ptr[2] &= 0x07;
  1920. s->vbv_delay_ptr[2] |= vbv_delay<<3;
  1921. }
  1922. s->total_bits += s->frame_bits;
  1923. avctx->frame_bits = s->frame_bits;
  1924. }else{
  1925. assert((pbBufPtr(&s->pb) == s->pb.buf));
  1926. s->frame_bits=0;
  1927. }
  1928. assert((s->frame_bits&7)==0);
  1929. return s->frame_bits/8;
  1930. }
  1931. #endif //CONFIG_ENCODERS
  1932. static inline void gmc1_motion(MpegEncContext *s,
  1933. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1934. uint8_t **ref_picture)
  1935. {
  1936. uint8_t *ptr;
  1937. int offset, src_x, src_y, linesize, uvlinesize;
  1938. int motion_x, motion_y;
  1939. int emu=0;
  1940. motion_x= s->sprite_offset[0][0];
  1941. motion_y= s->sprite_offset[0][1];
  1942. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  1943. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  1944. motion_x<<=(3-s->sprite_warping_accuracy);
  1945. motion_y<<=(3-s->sprite_warping_accuracy);
  1946. src_x = clip(src_x, -16, s->width);
  1947. if (src_x == s->width)
  1948. motion_x =0;
  1949. src_y = clip(src_y, -16, s->height);
  1950. if (src_y == s->height)
  1951. motion_y =0;
  1952. linesize = s->linesize;
  1953. uvlinesize = s->uvlinesize;
  1954. ptr = ref_picture[0] + (src_y * linesize) + src_x;
  1955. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1956. if( (unsigned)src_x >= s->h_edge_pos - 17
  1957. || (unsigned)src_y >= s->v_edge_pos - 17){
  1958. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1959. ptr= s->edge_emu_buffer;
  1960. }
  1961. }
  1962. if((motion_x|motion_y)&7){
  1963. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1964. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1965. }else{
  1966. int dxy;
  1967. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  1968. if (s->no_rounding){
  1969. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  1970. }else{
  1971. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  1972. }
  1973. }
  1974. if(s->flags&CODEC_FLAG_GRAY) return;
  1975. motion_x= s->sprite_offset[1][0];
  1976. motion_y= s->sprite_offset[1][1];
  1977. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  1978. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  1979. motion_x<<=(3-s->sprite_warping_accuracy);
  1980. motion_y<<=(3-s->sprite_warping_accuracy);
  1981. src_x = clip(src_x, -8, s->width>>1);
  1982. if (src_x == s->width>>1)
  1983. motion_x =0;
  1984. src_y = clip(src_y, -8, s->height>>1);
  1985. if (src_y == s->height>>1)
  1986. motion_y =0;
  1987. offset = (src_y * uvlinesize) + src_x;
  1988. ptr = ref_picture[1] + offset;
  1989. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1990. if( (unsigned)src_x >= (s->h_edge_pos>>1) - 9
  1991. || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
  1992. 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);
  1993. ptr= s->edge_emu_buffer;
  1994. emu=1;
  1995. }
  1996. }
  1997. s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1998. ptr = ref_picture[2] + offset;
  1999. if(emu){
  2000. 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);
  2001. ptr= s->edge_emu_buffer;
  2002. }
  2003. s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  2004. return;
  2005. }
  2006. static inline void gmc_motion(MpegEncContext *s,
  2007. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2008. uint8_t **ref_picture)
  2009. {
  2010. uint8_t *ptr;
  2011. int linesize, uvlinesize;
  2012. const int a= s->sprite_warping_accuracy;
  2013. int ox, oy;
  2014. linesize = s->linesize;
  2015. uvlinesize = s->uvlinesize;
  2016. ptr = ref_picture[0];
  2017. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  2018. oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
  2019. s->dsp.gmc(dest_y, ptr, linesize, 16,
  2020. ox,
  2021. oy,
  2022. s->sprite_delta[0][0], s->sprite_delta[0][1],
  2023. s->sprite_delta[1][0], s->sprite_delta[1][1],
  2024. a+1, (1<<(2*a+1)) - s->no_rounding,
  2025. s->h_edge_pos, s->v_edge_pos);
  2026. s->dsp.gmc(dest_y+8, ptr, linesize, 16,
  2027. ox + s->sprite_delta[0][0]*8,
  2028. oy + s->sprite_delta[1][0]*8,
  2029. s->sprite_delta[0][0], s->sprite_delta[0][1],
  2030. s->sprite_delta[1][0], s->sprite_delta[1][1],
  2031. a+1, (1<<(2*a+1)) - s->no_rounding,
  2032. s->h_edge_pos, s->v_edge_pos);
  2033. if(s->flags&CODEC_FLAG_GRAY) return;
  2034. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  2035. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  2036. ptr = ref_picture[1];
  2037. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  2038. ox,
  2039. oy,
  2040. s->sprite_delta[0][0], s->sprite_delta[0][1],
  2041. s->sprite_delta[1][0], s->sprite_delta[1][1],
  2042. a+1, (1<<(2*a+1)) - s->no_rounding,
  2043. s->h_edge_pos>>1, s->v_edge_pos>>1);
  2044. ptr = ref_picture[2];
  2045. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  2046. ox,
  2047. oy,
  2048. s->sprite_delta[0][0], s->sprite_delta[0][1],
  2049. s->sprite_delta[1][0], s->sprite_delta[1][1],
  2050. a+1, (1<<(2*a+1)) - s->no_rounding,
  2051. s->h_edge_pos>>1, s->v_edge_pos>>1);
  2052. }
  2053. /**
  2054. * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
  2055. * @param buf destination buffer
  2056. * @param src source buffer
  2057. * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
  2058. * @param block_w width of block
  2059. * @param block_h height of block
  2060. * @param src_x x coordinate of the top left sample of the block in the source buffer
  2061. * @param src_y y coordinate of the top left sample of the block in the source buffer
  2062. * @param w width of the source buffer
  2063. * @param h height of the source buffer
  2064. */
  2065. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  2066. int src_x, int src_y, int w, int h){
  2067. int x, y;
  2068. int start_y, start_x, end_y, end_x;
  2069. if(src_y>= h){
  2070. src+= (h-1-src_y)*linesize;
  2071. src_y=h-1;
  2072. }else if(src_y<=-block_h){
  2073. src+= (1-block_h-src_y)*linesize;
  2074. src_y=1-block_h;
  2075. }
  2076. if(src_x>= w){
  2077. src+= (w-1-src_x);
  2078. src_x=w-1;
  2079. }else if(src_x<=-block_w){
  2080. src+= (1-block_w-src_x);
  2081. src_x=1-block_w;
  2082. }
  2083. start_y= FFMAX(0, -src_y);
  2084. start_x= FFMAX(0, -src_x);
  2085. end_y= FFMIN(block_h, h-src_y);
  2086. end_x= FFMIN(block_w, w-src_x);
  2087. // copy existing part
  2088. for(y=start_y; y<end_y; y++){
  2089. for(x=start_x; x<end_x; x++){
  2090. buf[x + y*linesize]= src[x + y*linesize];
  2091. }
  2092. }
  2093. //top
  2094. for(y=0; y<start_y; y++){
  2095. for(x=start_x; x<end_x; x++){
  2096. buf[x + y*linesize]= buf[x + start_y*linesize];
  2097. }
  2098. }
  2099. //bottom
  2100. for(y=end_y; y<block_h; y++){
  2101. for(x=start_x; x<end_x; x++){
  2102. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  2103. }
  2104. }
  2105. for(y=0; y<block_h; y++){
  2106. //left
  2107. for(x=0; x<start_x; x++){
  2108. buf[x + y*linesize]= buf[start_x + y*linesize];
  2109. }
  2110. //right
  2111. for(x=end_x; x<block_w; x++){
  2112. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  2113. }
  2114. }
  2115. }
  2116. static inline int hpel_motion(MpegEncContext *s,
  2117. uint8_t *dest, uint8_t *src,
  2118. int field_based, int field_select,
  2119. int src_x, int src_y,
  2120. int width, int height, int stride,
  2121. int h_edge_pos, int v_edge_pos,
  2122. int w, int h, op_pixels_func *pix_op,
  2123. int motion_x, int motion_y)
  2124. {
  2125. int dxy;
  2126. int emu=0;
  2127. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  2128. src_x += motion_x >> 1;
  2129. src_y += motion_y >> 1;
  2130. /* WARNING: do no forget half pels */
  2131. src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
  2132. if (src_x == width)
  2133. dxy &= ~1;
  2134. src_y = clip(src_y, -16, height);
  2135. if (src_y == height)
  2136. dxy &= ~2;
  2137. src += src_y * stride + src_x;
  2138. if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
  2139. if( (unsigned)src_x > h_edge_pos - (motion_x&1) - w
  2140. || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
  2141. ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  2142. src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos);
  2143. src= s->edge_emu_buffer;
  2144. emu=1;
  2145. }
  2146. }
  2147. if(field_select)
  2148. src += s->linesize;
  2149. pix_op[dxy](dest, src, stride, h);
  2150. return emu;
  2151. }
  2152. /* apply one mpeg motion vector to the three components */
  2153. static always_inline void mpeg_motion(MpegEncContext *s,
  2154. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2155. int field_based, int bottom_field, int field_select,
  2156. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  2157. int motion_x, int motion_y, int h)
  2158. {
  2159. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2160. int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
  2161. #if 0
  2162. if(s->quarter_sample)
  2163. {
  2164. motion_x>>=1;
  2165. motion_y>>=1;
  2166. }
  2167. #endif
  2168. v_edge_pos = s->v_edge_pos >> field_based;
  2169. linesize = s->current_picture.linesize[0] << field_based;
  2170. uvlinesize = s->current_picture.linesize[1] << field_based;
  2171. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  2172. src_x = s->mb_x* 16 + (motion_x >> 1);
  2173. src_y =(s->mb_y<<(4-field_based)) + (motion_y >> 1);
  2174. if (s->out_format == FMT_H263) {
  2175. if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
  2176. mx = (motion_x>>1)|(motion_x&1);
  2177. my = motion_y >>1;
  2178. uvdxy = ((my & 1) << 1) | (mx & 1);
  2179. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  2180. uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1);
  2181. }else{
  2182. uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
  2183. uvsrc_x = src_x>>1;
  2184. uvsrc_y = src_y>>1;
  2185. }
  2186. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  2187. mx = motion_x / 4;
  2188. my = motion_y / 4;
  2189. uvdxy = 0;
  2190. uvsrc_x = s->mb_x*8 + mx;
  2191. uvsrc_y = s->mb_y*8 + my;
  2192. } else {
  2193. if(s->chroma_y_shift){
  2194. mx = motion_x / 2;
  2195. my = motion_y / 2;
  2196. uvdxy = ((my & 1) << 1) | (mx & 1);
  2197. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  2198. uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1);
  2199. } else {
  2200. if(s->chroma_x_shift){
  2201. //Chroma422
  2202. mx = motion_x / 2;
  2203. uvdxy = ((motion_y & 1) << 1) | (mx & 1);
  2204. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  2205. uvsrc_y = src_y;
  2206. } else {
  2207. //Chroma444
  2208. uvdxy = dxy;
  2209. uvsrc_x = src_x;
  2210. uvsrc_y = src_y;
  2211. }
  2212. }
  2213. }
  2214. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2215. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2216. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2217. if( (unsigned)src_x > s->h_edge_pos - (motion_x&1) - 16
  2218. || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
  2219. if(s->codec_id == CODEC_ID_MPEG2VIDEO ||
  2220. s->codec_id == CODEC_ID_MPEG1VIDEO){
  2221. av_log(s->avctx,AV_LOG_DEBUG,"MPEG motion vector out of boundary\n");
  2222. return ;
  2223. }
  2224. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  2225. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  2226. ptr_y = s->edge_emu_buffer;
  2227. if(!(s->flags&CODEC_FLAG_GRAY)){
  2228. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  2229. ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  2230. uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2231. ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  2232. uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2233. ptr_cb= uvbuf;
  2234. ptr_cr= uvbuf+16;
  2235. }
  2236. }
  2237. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  2238. dest_y += s->linesize;
  2239. dest_cb+= s->uvlinesize;
  2240. dest_cr+= s->uvlinesize;
  2241. }
  2242. if(field_select){
  2243. ptr_y += s->linesize;
  2244. ptr_cb+= s->uvlinesize;
  2245. ptr_cr+= s->uvlinesize;
  2246. }
  2247. pix_op[0][dxy](dest_y, ptr_y, linesize, h);
  2248. if(!(s->flags&CODEC_FLAG_GRAY)){
  2249. pix_op[s->chroma_x_shift][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
  2250. pix_op[s->chroma_x_shift][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
  2251. }
  2252. }
  2253. //FIXME move to dsputil, avg variant, 16x16 version
  2254. static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
  2255. int x;
  2256. uint8_t * const top = src[1];
  2257. uint8_t * const left = src[2];
  2258. uint8_t * const mid = src[0];
  2259. uint8_t * const right = src[3];
  2260. uint8_t * const bottom= src[4];
  2261. #define OBMC_FILTER(x, t, l, m, r, b)\
  2262. dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
  2263. #define OBMC_FILTER4(x, t, l, m, r, b)\
  2264. OBMC_FILTER(x , t, l, m, r, b);\
  2265. OBMC_FILTER(x+1 , t, l, m, r, b);\
  2266. OBMC_FILTER(x +stride, t, l, m, r, b);\
  2267. OBMC_FILTER(x+1+stride, t, l, m, r, b);
  2268. x=0;
  2269. OBMC_FILTER (x , 2, 2, 4, 0, 0);
  2270. OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
  2271. OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
  2272. OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
  2273. OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
  2274. OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
  2275. x+= stride;
  2276. OBMC_FILTER (x , 1, 2, 5, 0, 0);
  2277. OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
  2278. OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
  2279. OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
  2280. x+= stride;
  2281. OBMC_FILTER4(x , 1, 2, 5, 0, 0);
  2282. OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
  2283. OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
  2284. OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
  2285. x+= 2*stride;
  2286. OBMC_FILTER4(x , 0, 2, 5, 0, 1);
  2287. OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
  2288. OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
  2289. OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
  2290. x+= 2*stride;
  2291. OBMC_FILTER (x , 0, 2, 5, 0, 1);
  2292. OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
  2293. OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
  2294. OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
  2295. OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
  2296. OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
  2297. x+= stride;
  2298. OBMC_FILTER (x , 0, 2, 4, 0, 2);
  2299. OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
  2300. OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
  2301. OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
  2302. }
  2303. /* obmc for 1 8x8 luma block */
  2304. static inline void obmc_motion(MpegEncContext *s,
  2305. uint8_t *dest, uint8_t *src,
  2306. int src_x, int src_y,
  2307. op_pixels_func *pix_op,
  2308. int16_t mv[5][2]/* mid top left right bottom*/)
  2309. #define MID 0
  2310. {
  2311. int i;
  2312. uint8_t *ptr[5];
  2313. assert(s->quarter_sample==0);
  2314. for(i=0; i<5; i++){
  2315. if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
  2316. ptr[i]= ptr[MID];
  2317. }else{
  2318. ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
  2319. hpel_motion(s, ptr[i], src, 0, 0,
  2320. src_x, src_y,
  2321. s->width, s->height, s->linesize,
  2322. s->h_edge_pos, s->v_edge_pos,
  2323. 8, 8, pix_op,
  2324. mv[i][0], mv[i][1]);
  2325. }
  2326. }
  2327. put_obmc(dest, ptr, s->linesize);
  2328. }
  2329. static inline void qpel_motion(MpegEncContext *s,
  2330. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2331. int field_based, int bottom_field, int field_select,
  2332. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  2333. qpel_mc_func (*qpix_op)[16],
  2334. int motion_x, int motion_y, int h)
  2335. {
  2336. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2337. int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
  2338. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  2339. src_x = s->mb_x * 16 + (motion_x >> 2);
  2340. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  2341. v_edge_pos = s->v_edge_pos >> field_based;
  2342. linesize = s->linesize << field_based;
  2343. uvlinesize = s->uvlinesize << field_based;
  2344. if(field_based){
  2345. mx= motion_x/2;
  2346. my= motion_y>>1;
  2347. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  2348. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  2349. mx= (motion_x>>1) + rtab[motion_x&7];
  2350. my= (motion_y>>1) + rtab[motion_y&7];
  2351. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  2352. mx= (motion_x>>1)|(motion_x&1);
  2353. my= (motion_y>>1)|(motion_y&1);
  2354. }else{
  2355. mx= motion_x/2;
  2356. my= motion_y/2;
  2357. }
  2358. mx= (mx>>1)|(mx&1);
  2359. my= (my>>1)|(my&1);
  2360. uvdxy= (mx&1) | ((my&1)<<1);
  2361. mx>>=1;
  2362. my>>=1;
  2363. uvsrc_x = s->mb_x * 8 + mx;
  2364. uvsrc_y = s->mb_y * (8 >> field_based) + my;
  2365. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2366. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2367. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2368. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
  2369. || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
  2370. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  2371. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  2372. ptr_y= s->edge_emu_buffer;
  2373. if(!(s->flags&CODEC_FLAG_GRAY)){
  2374. uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
  2375. ff_emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize, 9, 9 + field_based,
  2376. uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2377. ff_emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9, 9 + field_based,
  2378. uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2379. ptr_cb= uvbuf;
  2380. ptr_cr= uvbuf + 16;
  2381. }
  2382. }
  2383. if(!field_based)
  2384. qpix_op[0][dxy](dest_y, ptr_y, linesize);
  2385. else{
  2386. if(bottom_field){
  2387. dest_y += s->linesize;
  2388. dest_cb+= s->uvlinesize;
  2389. dest_cr+= s->uvlinesize;
  2390. }
  2391. if(field_select){
  2392. ptr_y += s->linesize;
  2393. ptr_cb += s->uvlinesize;
  2394. ptr_cr += s->uvlinesize;
  2395. }
  2396. //damn interlaced mode
  2397. //FIXME boundary mirroring is not exactly correct here
  2398. qpix_op[1][dxy](dest_y , ptr_y , linesize);
  2399. qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
  2400. }
  2401. if(!(s->flags&CODEC_FLAG_GRAY)){
  2402. pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
  2403. pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
  2404. }
  2405. }
  2406. inline int ff_h263_round_chroma(int x){
  2407. if (x >= 0)
  2408. return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  2409. else {
  2410. x = -x;
  2411. return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  2412. }
  2413. }
  2414. /**
  2415. * h263 chorma 4mv motion compensation.
  2416. */
  2417. static inline void chroma_4mv_motion(MpegEncContext *s,
  2418. uint8_t *dest_cb, uint8_t *dest_cr,
  2419. uint8_t **ref_picture,
  2420. op_pixels_func *pix_op,
  2421. int mx, int my){
  2422. int dxy, emu=0, src_x, src_y, offset;
  2423. uint8_t *ptr;
  2424. /* In case of 8X8, we construct a single chroma motion vector
  2425. with a special rounding */
  2426. mx= ff_h263_round_chroma(mx);
  2427. my= ff_h263_round_chroma(my);
  2428. dxy = ((my & 1) << 1) | (mx & 1);
  2429. mx >>= 1;
  2430. my >>= 1;
  2431. src_x = s->mb_x * 8 + mx;
  2432. src_y = s->mb_y * 8 + my;
  2433. src_x = clip(src_x, -8, s->width/2);
  2434. if (src_x == s->width/2)
  2435. dxy &= ~1;
  2436. src_y = clip(src_y, -8, s->height/2);
  2437. if (src_y == s->height/2)
  2438. dxy &= ~2;
  2439. offset = (src_y * (s->uvlinesize)) + src_x;
  2440. ptr = ref_picture[1] + offset;
  2441. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2442. if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
  2443. || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
  2444. 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);
  2445. ptr= s->edge_emu_buffer;
  2446. emu=1;
  2447. }
  2448. }
  2449. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  2450. ptr = ref_picture[2] + offset;
  2451. if(emu){
  2452. 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);
  2453. ptr= s->edge_emu_buffer;
  2454. }
  2455. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  2456. }
  2457. /**
  2458. * motion compesation of a single macroblock
  2459. * @param s context
  2460. * @param dest_y luma destination pointer
  2461. * @param dest_cb chroma cb/u destination pointer
  2462. * @param dest_cr chroma cr/v destination pointer
  2463. * @param dir direction (0->forward, 1->backward)
  2464. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2465. * @param pic_op halfpel motion compensation function (average or put normally)
  2466. * @param pic_op qpel motion compensation function (average or put normally)
  2467. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2468. */
  2469. static inline void MPV_motion(MpegEncContext *s,
  2470. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2471. int dir, uint8_t **ref_picture,
  2472. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  2473. {
  2474. int dxy, mx, my, src_x, src_y, motion_x, motion_y;
  2475. int mb_x, mb_y, i;
  2476. uint8_t *ptr, *dest;
  2477. mb_x = s->mb_x;
  2478. mb_y = s->mb_y;
  2479. if(s->obmc && s->pict_type != B_TYPE){
  2480. int16_t mv_cache[4][4][2];
  2481. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  2482. const int mot_stride= s->b8_stride;
  2483. const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
  2484. assert(!s->mb_skiped);
  2485. memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4);
  2486. memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  2487. memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  2488. if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
  2489. memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
  2490. }else{
  2491. memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4);
  2492. }
  2493. if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
  2494. *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
  2495. *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
  2496. }else{
  2497. *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1];
  2498. *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride];
  2499. }
  2500. if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
  2501. *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
  2502. *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
  2503. }else{
  2504. *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
  2505. *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
  2506. }
  2507. mx = 0;
  2508. my = 0;
  2509. for(i=0;i<4;i++) {
  2510. const int x= (i&1)+1;
  2511. const int y= (i>>1)+1;
  2512. int16_t mv[5][2]= {
  2513. {mv_cache[y][x ][0], mv_cache[y][x ][1]},
  2514. {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
  2515. {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
  2516. {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
  2517. {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
  2518. //FIXME cleanup
  2519. obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  2520. ref_picture[0],
  2521. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  2522. pix_op[1],
  2523. mv);
  2524. mx += mv[0][0];
  2525. my += mv[0][1];
  2526. }
  2527. if(!(s->flags&CODEC_FLAG_GRAY))
  2528. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  2529. return;
  2530. }
  2531. switch(s->mv_type) {
  2532. case MV_TYPE_16X16:
  2533. #ifdef CONFIG_RISKY
  2534. if(s->mcsel){
  2535. if(s->real_sprite_warping_points==1){
  2536. gmc1_motion(s, dest_y, dest_cb, dest_cr,
  2537. ref_picture);
  2538. }else{
  2539. gmc_motion(s, dest_y, dest_cb, dest_cr,
  2540. ref_picture);
  2541. }
  2542. }else if(s->quarter_sample){
  2543. qpel_motion(s, dest_y, dest_cb, dest_cr,
  2544. 0, 0, 0,
  2545. ref_picture, pix_op, qpix_op,
  2546. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2547. }else if(s->mspel){
  2548. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  2549. ref_picture, pix_op,
  2550. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2551. }else
  2552. #endif
  2553. {
  2554. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2555. 0, 0, 0,
  2556. ref_picture, pix_op,
  2557. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2558. }
  2559. break;
  2560. case MV_TYPE_8X8:
  2561. mx = 0;
  2562. my = 0;
  2563. if(s->quarter_sample){
  2564. for(i=0;i<4;i++) {
  2565. motion_x = s->mv[dir][i][0];
  2566. motion_y = s->mv[dir][i][1];
  2567. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  2568. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  2569. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  2570. /* WARNING: do no forget half pels */
  2571. src_x = clip(src_x, -16, s->width);
  2572. if (src_x == s->width)
  2573. dxy &= ~3;
  2574. src_y = clip(src_y, -16, s->height);
  2575. if (src_y == s->height)
  2576. dxy &= ~12;
  2577. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  2578. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2579. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
  2580. || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
  2581. 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);
  2582. ptr= s->edge_emu_buffer;
  2583. }
  2584. }
  2585. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  2586. qpix_op[1][dxy](dest, ptr, s->linesize);
  2587. mx += s->mv[dir][i][0]/2;
  2588. my += s->mv[dir][i][1]/2;
  2589. }
  2590. }else{
  2591. for(i=0;i<4;i++) {
  2592. hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  2593. ref_picture[0], 0, 0,
  2594. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  2595. s->width, s->height, s->linesize,
  2596. s->h_edge_pos, s->v_edge_pos,
  2597. 8, 8, pix_op[1],
  2598. s->mv[dir][i][0], s->mv[dir][i][1]);
  2599. mx += s->mv[dir][i][0];
  2600. my += s->mv[dir][i][1];
  2601. }
  2602. }
  2603. if(!(s->flags&CODEC_FLAG_GRAY))
  2604. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  2605. break;
  2606. case MV_TYPE_FIELD:
  2607. if (s->picture_structure == PICT_FRAME) {
  2608. if(s->quarter_sample){
  2609. for(i=0; i<2; i++){
  2610. qpel_motion(s, dest_y, dest_cb, dest_cr,
  2611. 1, i, s->field_select[dir][i],
  2612. ref_picture, pix_op, qpix_op,
  2613. s->mv[dir][i][0], s->mv[dir][i][1], 8);
  2614. }
  2615. }else{
  2616. /* top field */
  2617. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2618. 1, 0, s->field_select[dir][0],
  2619. ref_picture, pix_op,
  2620. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2621. /* bottom field */
  2622. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2623. 1, 1, s->field_select[dir][1],
  2624. ref_picture, pix_op,
  2625. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  2626. }
  2627. } else {
  2628. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
  2629. ref_picture= s->current_picture_ptr->data;
  2630. }
  2631. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2632. 0, 0, s->field_select[dir][0],
  2633. ref_picture, pix_op,
  2634. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2635. }
  2636. break;
  2637. case MV_TYPE_16X8:
  2638. for(i=0; i<2; i++){
  2639. uint8_t ** ref2picture;
  2640. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
  2641. ref2picture= ref_picture;
  2642. }else{
  2643. ref2picture= s->current_picture_ptr->data;
  2644. }
  2645. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2646. 0, 0, s->field_select[dir][i],
  2647. ref2picture, pix_op,
  2648. s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8);
  2649. dest_y += 16*s->linesize;
  2650. dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
  2651. dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
  2652. }
  2653. break;
  2654. case MV_TYPE_DMV:
  2655. if(s->picture_structure == PICT_FRAME){
  2656. for(i=0; i<2; i++){
  2657. int j;
  2658. for(j=0; j<2; j++){
  2659. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2660. 1, j, j^i,
  2661. ref_picture, pix_op,
  2662. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8);
  2663. }
  2664. pix_op = s->dsp.avg_pixels_tab;
  2665. }
  2666. }else{
  2667. for(i=0; i<2; i++){
  2668. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2669. 0, 0, s->picture_structure != i+1,
  2670. ref_picture, pix_op,
  2671. s->mv[dir][2*i][0],s->mv[dir][2*i][1],16);
  2672. // after put we make avg of the same block
  2673. pix_op=s->dsp.avg_pixels_tab;
  2674. //opposite parity is always in the same frame if this is second field
  2675. if(!s->first_field){
  2676. ref_picture = s->current_picture_ptr->data;
  2677. }
  2678. }
  2679. }
  2680. break;
  2681. default: assert(0);
  2682. }
  2683. }
  2684. /* put block[] to dest[] */
  2685. static inline void put_dct(MpegEncContext *s,
  2686. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  2687. {
  2688. s->dct_unquantize_intra(s, block, i, qscale);
  2689. s->dsp.idct_put (dest, line_size, block);
  2690. }
  2691. /* add block[] to dest[] */
  2692. static inline void add_dct(MpegEncContext *s,
  2693. DCTELEM *block, int i, uint8_t *dest, int line_size)
  2694. {
  2695. if (s->block_last_index[i] >= 0) {
  2696. s->dsp.idct_add (dest, line_size, block);
  2697. }
  2698. }
  2699. static inline void add_dequant_dct(MpegEncContext *s,
  2700. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  2701. {
  2702. if (s->block_last_index[i] >= 0) {
  2703. s->dct_unquantize_inter(s, block, i, qscale);
  2704. s->dsp.idct_add (dest, line_size, block);
  2705. }
  2706. }
  2707. /**
  2708. * cleans dc, ac, coded_block for the current non intra MB
  2709. */
  2710. void ff_clean_intra_table_entries(MpegEncContext *s)
  2711. {
  2712. int wrap = s->b8_stride;
  2713. int xy = s->block_index[0];
  2714. s->dc_val[0][xy ] =
  2715. s->dc_val[0][xy + 1 ] =
  2716. s->dc_val[0][xy + wrap] =
  2717. s->dc_val[0][xy + 1 + wrap] = 1024;
  2718. /* ac pred */
  2719. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2720. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2721. if (s->msmpeg4_version>=3) {
  2722. s->coded_block[xy ] =
  2723. s->coded_block[xy + 1 ] =
  2724. s->coded_block[xy + wrap] =
  2725. s->coded_block[xy + 1 + wrap] = 0;
  2726. }
  2727. /* chroma */
  2728. wrap = s->mb_stride;
  2729. xy = s->mb_x + s->mb_y * wrap;
  2730. s->dc_val[1][xy] =
  2731. s->dc_val[2][xy] = 1024;
  2732. /* ac pred */
  2733. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2734. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2735. s->mbintra_table[xy]= 0;
  2736. }
  2737. /* generic function called after a macroblock has been parsed by the
  2738. decoder or after it has been encoded by the encoder.
  2739. Important variables used:
  2740. s->mb_intra : true if intra macroblock
  2741. s->mv_dir : motion vector direction
  2742. s->mv_type : motion vector type
  2743. s->mv : motion vector
  2744. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2745. */
  2746. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
  2747. {
  2748. int mb_x, mb_y;
  2749. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2750. #ifdef HAVE_XVMC
  2751. if(s->avctx->xvmc_acceleration){
  2752. XVMC_decode_mb(s);//xvmc uses pblocks
  2753. return;
  2754. }
  2755. #endif
  2756. mb_x = s->mb_x;
  2757. mb_y = s->mb_y;
  2758. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2759. /* save DCT coefficients */
  2760. int i,j;
  2761. DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
  2762. for(i=0; i<6; i++)
  2763. for(j=0; j<64; j++)
  2764. *dct++ = block[i][s->dsp.idct_permutation[j]];
  2765. }
  2766. s->current_picture.qscale_table[mb_xy]= s->qscale;
  2767. /* update DC predictors for P macroblocks */
  2768. if (!s->mb_intra) {
  2769. if (s->h263_pred || s->h263_aic) {
  2770. if(s->mbintra_table[mb_xy])
  2771. ff_clean_intra_table_entries(s);
  2772. } else {
  2773. s->last_dc[0] =
  2774. s->last_dc[1] =
  2775. s->last_dc[2] = 128 << s->intra_dc_precision;
  2776. }
  2777. }
  2778. else if (s->h263_pred || s->h263_aic)
  2779. s->mbintra_table[mb_xy]=1;
  2780. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
  2781. uint8_t *dest_y, *dest_cb, *dest_cr;
  2782. int dct_linesize, dct_offset;
  2783. op_pixels_func (*op_pix)[4];
  2784. qpel_mc_func (*op_qpix)[16];
  2785. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  2786. const int uvlinesize= s->current_picture.linesize[1];
  2787. const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band;
  2788. /* avoid copy if macroblock skipped in last frame too */
  2789. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2790. if(!s->encoding){
  2791. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2792. const int age= s->current_picture.age;
  2793. assert(age);
  2794. if (s->mb_skiped) {
  2795. s->mb_skiped= 0;
  2796. assert(s->pict_type!=I_TYPE);
  2797. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  2798. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  2799. /* if previous was skipped too, then nothing to do ! */
  2800. if (*mbskip_ptr >= age && s->current_picture.reference){
  2801. return;
  2802. }
  2803. } else if(!s->current_picture.reference){
  2804. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  2805. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  2806. } else{
  2807. *mbskip_ptr = 0; /* not skipped */
  2808. }
  2809. }
  2810. dct_linesize = linesize << s->interlaced_dct;
  2811. dct_offset =(s->interlaced_dct)? linesize : linesize*8;
  2812. if(readable){
  2813. dest_y= s->dest[0];
  2814. dest_cb= s->dest[1];
  2815. dest_cr= s->dest[2];
  2816. }else{
  2817. dest_y = s->b_scratchpad;
  2818. dest_cb= s->b_scratchpad+16*linesize;
  2819. dest_cr= s->b_scratchpad+32*linesize;
  2820. }
  2821. if (!s->mb_intra) {
  2822. /* motion handling */
  2823. /* decoding or more than one mb_type (MC was allready done otherwise) */
  2824. if(!s->encoding){
  2825. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2826. op_pix = s->dsp.put_pixels_tab;
  2827. op_qpix= s->dsp.put_qpel_pixels_tab;
  2828. }else{
  2829. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2830. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2831. }
  2832. if (s->mv_dir & MV_DIR_FORWARD) {
  2833. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2834. op_pix = s->dsp.avg_pixels_tab;
  2835. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2836. }
  2837. if (s->mv_dir & MV_DIR_BACKWARD) {
  2838. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2839. }
  2840. }
  2841. /* skip dequant / idct if we are really late ;) */
  2842. if(s->hurry_up>1) return;
  2843. /* add dct residue */
  2844. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  2845. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2846. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize, s->qscale);
  2847. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize, s->qscale);
  2848. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize, s->qscale);
  2849. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize, s->qscale);
  2850. if(!(s->flags&CODEC_FLAG_GRAY)){
  2851. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2852. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2853. }
  2854. } else if(s->codec_id != CODEC_ID_WMV2){
  2855. add_dct(s, block[0], 0, dest_y, dct_linesize);
  2856. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  2857. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  2858. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  2859. if(!(s->flags&CODEC_FLAG_GRAY)){
  2860. if(s->chroma_y_shift){//Chroma420
  2861. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2862. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2863. }else{
  2864. //chroma422
  2865. dct_linesize = uvlinesize << s->interlaced_dct;
  2866. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  2867. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2868. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2869. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2870. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2871. if(!s->chroma_x_shift){//Chroma444
  2872. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  2873. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  2874. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  2875. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  2876. }
  2877. }
  2878. }//fi gray
  2879. }
  2880. #ifdef CONFIG_RISKY
  2881. else{
  2882. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2883. }
  2884. #endif
  2885. } else {
  2886. /* dct only in intra block */
  2887. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  2888. put_dct(s, block[0], 0, dest_y, dct_linesize, s->qscale);
  2889. put_dct(s, block[1], 1, dest_y + 8, dct_linesize, s->qscale);
  2890. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize, s->qscale);
  2891. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize, s->qscale);
  2892. if(!(s->flags&CODEC_FLAG_GRAY)){
  2893. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2894. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2895. }
  2896. }else{
  2897. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2898. s->dsp.idct_put(dest_y + 8, dct_linesize, block[1]);
  2899. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2900. s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
  2901. if(!(s->flags&CODEC_FLAG_GRAY)){
  2902. if(s->chroma_y_shift){
  2903. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2904. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2905. }else{
  2906. dct_linesize = uvlinesize << s->interlaced_dct;
  2907. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  2908. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2909. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2910. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2911. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2912. if(!s->chroma_x_shift){//Chroma444
  2913. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  2914. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  2915. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  2916. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  2917. }
  2918. }
  2919. }//gray
  2920. }
  2921. }
  2922. if(!readable){
  2923. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2924. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2925. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2926. }
  2927. }
  2928. }
  2929. #ifdef CONFIG_ENCODERS
  2930. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  2931. {
  2932. static const char tab[64]=
  2933. {3,2,2,1,1,1,1,1,
  2934. 1,1,1,1,1,1,1,1,
  2935. 1,1,1,1,1,1,1,1,
  2936. 0,0,0,0,0,0,0,0,
  2937. 0,0,0,0,0,0,0,0,
  2938. 0,0,0,0,0,0,0,0,
  2939. 0,0,0,0,0,0,0,0,
  2940. 0,0,0,0,0,0,0,0};
  2941. int score=0;
  2942. int run=0;
  2943. int i;
  2944. DCTELEM *block= s->block[n];
  2945. const int last_index= s->block_last_index[n];
  2946. int skip_dc;
  2947. if(threshold<0){
  2948. skip_dc=0;
  2949. threshold= -threshold;
  2950. }else
  2951. skip_dc=1;
  2952. /* are all which we could set to zero are allready zero? */
  2953. if(last_index<=skip_dc - 1) return;
  2954. for(i=0; i<=last_index; i++){
  2955. const int j = s->intra_scantable.permutated[i];
  2956. const int level = ABS(block[j]);
  2957. if(level==1){
  2958. if(skip_dc && i==0) continue;
  2959. score+= tab[run];
  2960. run=0;
  2961. }else if(level>1){
  2962. return;
  2963. }else{
  2964. run++;
  2965. }
  2966. }
  2967. if(score >= threshold) return;
  2968. for(i=skip_dc; i<=last_index; i++){
  2969. const int j = s->intra_scantable.permutated[i];
  2970. block[j]=0;
  2971. }
  2972. if(block[0]) s->block_last_index[n]= 0;
  2973. else s->block_last_index[n]= -1;
  2974. }
  2975. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  2976. {
  2977. int i;
  2978. const int maxlevel= s->max_qcoeff;
  2979. const int minlevel= s->min_qcoeff;
  2980. int overflow=0;
  2981. if(s->mb_intra){
  2982. i=1; //skip clipping of intra dc
  2983. }else
  2984. i=0;
  2985. for(;i<=last_index; i++){
  2986. const int j= s->intra_scantable.permutated[i];
  2987. int level = block[j];
  2988. if (level>maxlevel){
  2989. level=maxlevel;
  2990. overflow++;
  2991. }else if(level<minlevel){
  2992. level=minlevel;
  2993. overflow++;
  2994. }
  2995. block[j]= level;
  2996. }
  2997. if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
  2998. av_log(s->avctx, AV_LOG_INFO, "warning, cliping %d dct coefficents to %d..%d\n", overflow, minlevel, maxlevel);
  2999. }
  3000. #endif //CONFIG_ENCODERS
  3001. /**
  3002. *
  3003. * @param h is the normal height, this will be reduced automatically if needed for the last row
  3004. */
  3005. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  3006. if (s->avctx->draw_horiz_band) {
  3007. AVFrame *src;
  3008. int offset[4];
  3009. if(s->picture_structure != PICT_FRAME){
  3010. h <<= 1;
  3011. y <<= 1;
  3012. if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  3013. }
  3014. h= FFMIN(h, s->height - y);
  3015. if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  3016. src= (AVFrame*)s->current_picture_ptr;
  3017. else if(s->last_picture_ptr)
  3018. src= (AVFrame*)s->last_picture_ptr;
  3019. else
  3020. return;
  3021. if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  3022. offset[0]=
  3023. offset[1]=
  3024. offset[2]=
  3025. offset[3]= 0;
  3026. }else{
  3027. offset[0]= y * s->linesize;;
  3028. offset[1]=
  3029. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  3030. offset[3]= 0;
  3031. }
  3032. emms_c();
  3033. s->avctx->draw_horiz_band(s->avctx, src, offset,
  3034. y, s->picture_structure, h);
  3035. }
  3036. }
  3037. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  3038. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  3039. const int uvlinesize= s->current_picture.linesize[1];
  3040. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  3041. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  3042. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  3043. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  3044. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  3045. s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  3046. //block_index is not used by mpeg2, so it is not affected by chroma_format
  3047. s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << 4);
  3048. s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (4 - s->chroma_x_shift));
  3049. s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (4 - s->chroma_x_shift));
  3050. if(!(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  3051. {
  3052. s->dest[0] += s->mb_y * linesize << 4;
  3053. s->dest[1] += s->mb_y * uvlinesize << (4 - s->chroma_y_shift);
  3054. s->dest[2] += s->mb_y * uvlinesize << (4 - s->chroma_y_shift);
  3055. }
  3056. }
  3057. #ifdef CONFIG_ENCODERS
  3058. static void get_vissual_weight(int16_t *weight, uint8_t *ptr, int stride){
  3059. int x, y;
  3060. //FIXME optimize
  3061. for(y=0; y<8; y++){
  3062. for(x=0; x<8; x++){
  3063. int x2, y2;
  3064. int sum=0;
  3065. int sqr=0;
  3066. int count=0;
  3067. for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){
  3068. for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){
  3069. int v= ptr[x2 + y2*stride];
  3070. sum += v;
  3071. sqr += v*v;
  3072. count++;
  3073. }
  3074. }
  3075. weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count;
  3076. }
  3077. }
  3078. }
  3079. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  3080. {
  3081. int16_t weight[6][64];
  3082. DCTELEM orig[6][64];
  3083. const int mb_x= s->mb_x;
  3084. const int mb_y= s->mb_y;
  3085. int i;
  3086. int skip_dct[6];
  3087. int dct_offset = s->linesize*8; //default for progressive frames
  3088. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  3089. int wrap_y, wrap_c;
  3090. for(i=0; i<6; i++) skip_dct[i]=0;
  3091. if(s->adaptive_quant){
  3092. const int last_qp= s->qscale;
  3093. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3094. s->lambda= s->lambda_table[mb_xy];
  3095. update_qscale(s);
  3096. if(!(s->flags&CODEC_FLAG_QP_RD)){
  3097. s->dquant= s->qscale - last_qp;
  3098. if(s->out_format==FMT_H263){
  3099. s->dquant= clip(s->dquant, -2, 2); //FIXME RD
  3100. if(s->codec_id==CODEC_ID_MPEG4){
  3101. if(!s->mb_intra){
  3102. if(s->pict_type == B_TYPE){
  3103. if(s->dquant&1)
  3104. s->dquant= (s->dquant/2)*2;
  3105. if(s->mv_dir&MV_DIRECT)
  3106. s->dquant= 0;
  3107. }
  3108. if(s->mv_type==MV_TYPE_8X8)
  3109. s->dquant=0;
  3110. }
  3111. }
  3112. }
  3113. }
  3114. ff_set_qscale(s, last_qp + s->dquant);
  3115. }else if(s->flags&CODEC_FLAG_QP_RD)
  3116. ff_set_qscale(s, s->qscale + s->dquant);
  3117. wrap_y = s->linesize;
  3118. wrap_c = s->uvlinesize;
  3119. ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  3120. ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  3121. ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  3122. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  3123. uint8_t *ebuf= s->edge_emu_buffer + 32;
  3124. ff_emulated_edge_mc(ebuf , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width , s->height);
  3125. ptr_y= ebuf;
  3126. ff_emulated_edge_mc(ebuf+18*wrap_y , ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  3127. ptr_cb= ebuf+18*wrap_y;
  3128. ff_emulated_edge_mc(ebuf+18*wrap_y+8, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  3129. ptr_cr= ebuf+18*wrap_y+8;
  3130. }
  3131. if (s->mb_intra) {
  3132. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  3133. int progressive_score, interlaced_score;
  3134. s->interlaced_dct=0;
  3135. progressive_score= s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y, 8)
  3136. +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
  3137. if(progressive_score > 0){
  3138. interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y*2, 8)
  3139. +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y , NULL, wrap_y*2, 8);
  3140. if(progressive_score > interlaced_score){
  3141. s->interlaced_dct=1;
  3142. dct_offset= wrap_y;
  3143. wrap_y<<=1;
  3144. }
  3145. }
  3146. }
  3147. s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
  3148. s->dsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
  3149. s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
  3150. s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
  3151. if(s->flags&CODEC_FLAG_GRAY){
  3152. skip_dct[4]= 1;
  3153. skip_dct[5]= 1;
  3154. }else{
  3155. s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
  3156. s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
  3157. }
  3158. }else{
  3159. op_pixels_func (*op_pix)[4];
  3160. qpel_mc_func (*op_qpix)[16];
  3161. uint8_t *dest_y, *dest_cb, *dest_cr;
  3162. dest_y = s->dest[0];
  3163. dest_cb = s->dest[1];
  3164. dest_cr = s->dest[2];
  3165. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  3166. op_pix = s->dsp.put_pixels_tab;
  3167. op_qpix= s->dsp.put_qpel_pixels_tab;
  3168. }else{
  3169. op_pix = s->dsp.put_no_rnd_pixels_tab;
  3170. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  3171. }
  3172. if (s->mv_dir & MV_DIR_FORWARD) {
  3173. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  3174. op_pix = s->dsp.avg_pixels_tab;
  3175. op_qpix= s->dsp.avg_qpel_pixels_tab;
  3176. }
  3177. if (s->mv_dir & MV_DIR_BACKWARD) {
  3178. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  3179. }
  3180. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  3181. int progressive_score, interlaced_score;
  3182. s->interlaced_dct=0;
  3183. progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8)
  3184. +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
  3185. if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
  3186. if(progressive_score>0){
  3187. interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8)
  3188. +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8);
  3189. if(progressive_score > interlaced_score){
  3190. s->interlaced_dct=1;
  3191. dct_offset= wrap_y;
  3192. wrap_y<<=1;
  3193. }
  3194. }
  3195. }
  3196. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  3197. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  3198. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  3199. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  3200. if(s->flags&CODEC_FLAG_GRAY){
  3201. skip_dct[4]= 1;
  3202. skip_dct[5]= 1;
  3203. }else{
  3204. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  3205. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  3206. }
  3207. /* pre quantization */
  3208. if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
  3209. //FIXME optimize
  3210. if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
  3211. if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
  3212. if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
  3213. if(s->dsp.sad[1](NULL, ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y, 8) < 20*s->qscale) skip_dct[3]= 1;
  3214. if(s->dsp.sad[1](NULL, ptr_cb , dest_cb , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
  3215. if(s->dsp.sad[1](NULL, ptr_cr , dest_cr , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
  3216. }
  3217. }
  3218. if(s->avctx->quantizer_noise_shaping){
  3219. if(!skip_dct[0]) get_vissual_weight(weight[0], ptr_y , wrap_y);
  3220. if(!skip_dct[1]) get_vissual_weight(weight[1], ptr_y + 8, wrap_y);
  3221. if(!skip_dct[2]) get_vissual_weight(weight[2], ptr_y + dct_offset , wrap_y);
  3222. if(!skip_dct[3]) get_vissual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
  3223. if(!skip_dct[4]) get_vissual_weight(weight[4], ptr_cb , wrap_c);
  3224. if(!skip_dct[5]) get_vissual_weight(weight[5], ptr_cr , wrap_c);
  3225. memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*6);
  3226. }
  3227. /* DCT & quantize */
  3228. assert(s->out_format!=FMT_MJPEG || s->qscale==8);
  3229. {
  3230. for(i=0;i<6;i++) {
  3231. if(!skip_dct[i]){
  3232. int overflow;
  3233. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  3234. // FIXME we could decide to change to quantizer instead of clipping
  3235. // JS: I don't think that would be a good idea it could lower quality instead
  3236. // of improve it. Just INTRADC clipping deserves changes in quantizer
  3237. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  3238. }else
  3239. s->block_last_index[i]= -1;
  3240. }
  3241. if(s->avctx->quantizer_noise_shaping){
  3242. for(i=0;i<6;i++) {
  3243. if(!skip_dct[i]){
  3244. s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
  3245. }
  3246. }
  3247. }
  3248. if(s->luma_elim_threshold && !s->mb_intra)
  3249. for(i=0; i<4; i++)
  3250. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  3251. if(s->chroma_elim_threshold && !s->mb_intra)
  3252. for(i=4; i<6; i++)
  3253. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  3254. if(s->flags & CODEC_FLAG_CBP_RD){
  3255. for(i=0;i<6;i++) {
  3256. if(s->block_last_index[i] == -1)
  3257. s->coded_score[i]= INT_MAX/256;
  3258. }
  3259. }
  3260. }
  3261. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  3262. s->block_last_index[4]=
  3263. s->block_last_index[5]= 0;
  3264. s->block[4][0]=
  3265. s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  3266. }
  3267. //non c quantize code returns incorrect block_last_index FIXME
  3268. if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
  3269. for(i=0; i<6; i++){
  3270. int j;
  3271. if(s->block_last_index[i]>0){
  3272. for(j=63; j>0; j--){
  3273. if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
  3274. }
  3275. s->block_last_index[i]= j;
  3276. }
  3277. }
  3278. }
  3279. /* huffman encode */
  3280. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  3281. case CODEC_ID_MPEG1VIDEO:
  3282. case CODEC_ID_MPEG2VIDEO:
  3283. mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
  3284. #ifdef CONFIG_RISKY
  3285. case CODEC_ID_MPEG4:
  3286. mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  3287. case CODEC_ID_MSMPEG4V2:
  3288. case CODEC_ID_MSMPEG4V3:
  3289. case CODEC_ID_WMV1:
  3290. msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  3291. case CODEC_ID_WMV2:
  3292. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
  3293. case CODEC_ID_H263:
  3294. case CODEC_ID_H263P:
  3295. case CODEC_ID_FLV1:
  3296. case CODEC_ID_RV10:
  3297. h263_encode_mb(s, s->block, motion_x, motion_y); break;
  3298. #endif
  3299. case CODEC_ID_MJPEG:
  3300. mjpeg_encode_mb(s, s->block); break;
  3301. default:
  3302. assert(0);
  3303. }
  3304. }
  3305. #endif //CONFIG_ENCODERS
  3306. void ff_mpeg_flush(AVCodecContext *avctx){
  3307. int i;
  3308. MpegEncContext *s = avctx->priv_data;
  3309. if(s==NULL || s->picture==NULL)
  3310. return;
  3311. for(i=0; i<MAX_PICTURE_COUNT; i++){
  3312. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  3313. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  3314. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  3315. }
  3316. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  3317. s->parse_context.state= -1;
  3318. s->parse_context.frame_start_found= 0;
  3319. s->parse_context.overread= 0;
  3320. s->parse_context.overread_index= 0;
  3321. s->parse_context.index= 0;
  3322. s->parse_context.last_index= 0;
  3323. s->bitstream_buffer_size=0;
  3324. }
  3325. #ifdef CONFIG_ENCODERS
  3326. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
  3327. {
  3328. const uint16_t *srcw= (uint16_t*)src;
  3329. int words= length>>4;
  3330. int bits= length&15;
  3331. int i;
  3332. if(length==0) return;
  3333. if(words < 16){
  3334. for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
  3335. }else if(put_bits_count(pb)&7){
  3336. for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
  3337. }else{
  3338. for(i=0; put_bits_count(pb)&31; i++)
  3339. put_bits(pb, 8, src[i]);
  3340. flush_put_bits(pb);
  3341. memcpy(pbBufPtr(pb), src+i, 2*words-i);
  3342. skip_put_bytes(pb, 2*words-i);
  3343. }
  3344. put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
  3345. }
  3346. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  3347. int i;
  3348. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  3349. /* mpeg1 */
  3350. d->mb_skip_run= s->mb_skip_run;
  3351. for(i=0; i<3; i++)
  3352. d->last_dc[i]= s->last_dc[i];
  3353. /* statistics */
  3354. d->mv_bits= s->mv_bits;
  3355. d->i_tex_bits= s->i_tex_bits;
  3356. d->p_tex_bits= s->p_tex_bits;
  3357. d->i_count= s->i_count;
  3358. d->f_count= s->f_count;
  3359. d->b_count= s->b_count;
  3360. d->skip_count= s->skip_count;
  3361. d->misc_bits= s->misc_bits;
  3362. d->last_bits= 0;
  3363. d->mb_skiped= 0;
  3364. d->qscale= s->qscale;
  3365. d->dquant= s->dquant;
  3366. }
  3367. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  3368. int i;
  3369. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  3370. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  3371. /* mpeg1 */
  3372. d->mb_skip_run= s->mb_skip_run;
  3373. for(i=0; i<3; i++)
  3374. d->last_dc[i]= s->last_dc[i];
  3375. /* statistics */
  3376. d->mv_bits= s->mv_bits;
  3377. d->i_tex_bits= s->i_tex_bits;
  3378. d->p_tex_bits= s->p_tex_bits;
  3379. d->i_count= s->i_count;
  3380. d->f_count= s->f_count;
  3381. d->b_count= s->b_count;
  3382. d->skip_count= s->skip_count;
  3383. d->misc_bits= s->misc_bits;
  3384. d->mb_intra= s->mb_intra;
  3385. d->mb_skiped= s->mb_skiped;
  3386. d->mv_type= s->mv_type;
  3387. d->mv_dir= s->mv_dir;
  3388. d->pb= s->pb;
  3389. if(s->data_partitioning){
  3390. d->pb2= s->pb2;
  3391. d->tex_pb= s->tex_pb;
  3392. }
  3393. d->block= s->block;
  3394. for(i=0; i<6; i++)
  3395. d->block_last_index[i]= s->block_last_index[i];
  3396. d->interlaced_dct= s->interlaced_dct;
  3397. d->qscale= s->qscale;
  3398. }
  3399. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  3400. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  3401. int *dmin, int *next_block, int motion_x, int motion_y)
  3402. {
  3403. int score;
  3404. uint8_t *dest_backup[3];
  3405. copy_context_before_encode(s, backup, type);
  3406. s->block= s->blocks[*next_block];
  3407. s->pb= pb[*next_block];
  3408. if(s->data_partitioning){
  3409. s->pb2 = pb2 [*next_block];
  3410. s->tex_pb= tex_pb[*next_block];
  3411. }
  3412. if(*next_block){
  3413. memcpy(dest_backup, s->dest, sizeof(s->dest));
  3414. s->dest[0] = s->rd_scratchpad;
  3415. s->dest[1] = s->rd_scratchpad + 16*s->linesize;
  3416. s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
  3417. assert(s->linesize >= 32); //FIXME
  3418. }
  3419. encode_mb(s, motion_x, motion_y);
  3420. score= put_bits_count(&s->pb);
  3421. if(s->data_partitioning){
  3422. score+= put_bits_count(&s->pb2);
  3423. score+= put_bits_count(&s->tex_pb);
  3424. }
  3425. if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  3426. MPV_decode_mb(s, s->block);
  3427. score *= s->lambda2;
  3428. score += sse_mb(s) << FF_LAMBDA_SHIFT;
  3429. }
  3430. if(*next_block){
  3431. memcpy(s->dest, dest_backup, sizeof(s->dest));
  3432. }
  3433. if(score<*dmin){
  3434. *dmin= score;
  3435. *next_block^=1;
  3436. copy_context_after_encode(best, s, type);
  3437. }
  3438. }
  3439. static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  3440. uint32_t *sq = squareTbl + 256;
  3441. int acc=0;
  3442. int x,y;
  3443. if(w==16 && h==16)
  3444. return s->dsp.sse[0](NULL, src1, src2, stride, 16);
  3445. else if(w==8 && h==8)
  3446. return s->dsp.sse[1](NULL, src1, src2, stride, 8);
  3447. for(y=0; y<h; y++){
  3448. for(x=0; x<w; x++){
  3449. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  3450. }
  3451. }
  3452. assert(acc>=0);
  3453. return acc;
  3454. }
  3455. static int sse_mb(MpegEncContext *s){
  3456. int w= 16;
  3457. int h= 16;
  3458. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  3459. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  3460. if(w==16 && h==16)
  3461. if(s->avctx->mb_cmp == FF_CMP_NSSE){
  3462. return s->dsp.nsse[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
  3463. +s->dsp.nsse[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
  3464. +s->dsp.nsse[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
  3465. }else{
  3466. 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, 16)
  3467. +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, 8)
  3468. +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, 8);
  3469. }
  3470. else
  3471. 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)
  3472. +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)
  3473. +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);
  3474. }
  3475. static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
  3476. MpegEncContext *s= arg;
  3477. s->me.pre_pass=1;
  3478. s->me.dia_size= s->avctx->pre_dia_size;
  3479. s->first_slice_line=1;
  3480. for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
  3481. for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
  3482. ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  3483. }
  3484. s->first_slice_line=0;
  3485. }
  3486. s->me.pre_pass=0;
  3487. return 0;
  3488. }
  3489. static int estimate_motion_thread(AVCodecContext *c, void *arg){
  3490. MpegEncContext *s= arg;
  3491. s->me.dia_size= s->avctx->dia_size;
  3492. s->first_slice_line=1;
  3493. for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
  3494. s->mb_x=0; //for block init below
  3495. ff_init_block_index(s);
  3496. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  3497. s->block_index[0]+=2;
  3498. s->block_index[1]+=2;
  3499. s->block_index[2]+=2;
  3500. s->block_index[3]+=2;
  3501. /* compute motion vector & mb_type and store in context */
  3502. if(s->pict_type==B_TYPE)
  3503. ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
  3504. else
  3505. ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  3506. }
  3507. s->first_slice_line=0;
  3508. }
  3509. return 0;
  3510. }
  3511. static int mb_var_thread(AVCodecContext *c, void *arg){
  3512. MpegEncContext *s= arg;
  3513. int mb_x, mb_y;
  3514. for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  3515. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3516. int xx = mb_x * 16;
  3517. int yy = mb_y * 16;
  3518. uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  3519. int varc;
  3520. int sum = s->dsp.pix_sum(pix, s->linesize);
  3521. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  3522. s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  3523. s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  3524. s->me.mb_var_sum_temp += varc;
  3525. }
  3526. }
  3527. return 0;
  3528. }
  3529. static void write_slice_end(MpegEncContext *s){
  3530. if(s->codec_id==CODEC_ID_MPEG4){
  3531. if(s->partitioned_frame){
  3532. ff_mpeg4_merge_partitions(s);
  3533. }
  3534. ff_mpeg4_stuffing(&s->pb);
  3535. }else if(s->out_format == FMT_MJPEG){
  3536. ff_mjpeg_stuffing(&s->pb);
  3537. }
  3538. align_put_bits(&s->pb);
  3539. flush_put_bits(&s->pb);
  3540. }
  3541. static int encode_thread(AVCodecContext *c, void *arg){
  3542. MpegEncContext *s= arg;
  3543. int mb_x, mb_y, pdif = 0;
  3544. int i, j;
  3545. MpegEncContext best_s, backup_s;
  3546. uint8_t bit_buf[2][3000];
  3547. uint8_t bit_buf2[2][3000];
  3548. uint8_t bit_buf_tex[2][3000];
  3549. PutBitContext pb[2], pb2[2], tex_pb[2];
  3550. //printf("%d->%d\n", s->resync_mb_y, s->end_mb_y);
  3551. for(i=0; i<2; i++){
  3552. init_put_bits(&pb [i], bit_buf [i], 3000);
  3553. init_put_bits(&pb2 [i], bit_buf2 [i], 3000);
  3554. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000);
  3555. }
  3556. s->last_bits= put_bits_count(&s->pb);
  3557. s->mv_bits=0;
  3558. s->misc_bits=0;
  3559. s->i_tex_bits=0;
  3560. s->p_tex_bits=0;
  3561. s->i_count=0;
  3562. s->f_count=0;
  3563. s->b_count=0;
  3564. s->skip_count=0;
  3565. for(i=0; i<3; i++){
  3566. /* init last dc values */
  3567. /* note: quant matrix value (8) is implied here */
  3568. s->last_dc[i] = 128 << s->intra_dc_precision;
  3569. s->current_picture_ptr->error[i] = 0;
  3570. }
  3571. s->mb_skip_run = 0;
  3572. memset(s->last_mv, 0, sizeof(s->last_mv));
  3573. s->last_mv_dir = 0;
  3574. #ifdef CONFIG_RISKY
  3575. switch(s->codec_id){
  3576. case CODEC_ID_H263:
  3577. case CODEC_ID_H263P:
  3578. case CODEC_ID_FLV1:
  3579. s->gob_index = ff_h263_get_gob_height(s);
  3580. break;
  3581. case CODEC_ID_MPEG4:
  3582. if(s->partitioned_frame)
  3583. ff_mpeg4_init_partitions(s);
  3584. break;
  3585. }
  3586. #endif
  3587. s->resync_mb_x=0;
  3588. s->resync_mb_y=0;
  3589. s->first_slice_line = 1;
  3590. s->ptr_lastgob = s->pb.buf;
  3591. for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  3592. // printf("row %d at %X\n", s->mb_y, (int)s);
  3593. s->mb_x=0;
  3594. s->mb_y= mb_y;
  3595. ff_set_qscale(s, s->qscale);
  3596. ff_init_block_index(s);
  3597. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3598. const int xy= mb_y*s->mb_stride + mb_x;
  3599. int mb_type= s->mb_type[xy];
  3600. // int d;
  3601. int dmin= INT_MAX;
  3602. int dir;
  3603. s->mb_x = mb_x;
  3604. ff_update_block_index(s);
  3605. /* write gob / video packet header */
  3606. #ifdef CONFIG_RISKY
  3607. if(s->rtp_mode){
  3608. int current_packet_size, is_gob_start;
  3609. current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
  3610. is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
  3611. if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
  3612. switch(s->codec_id){
  3613. case CODEC_ID_H263:
  3614. case CODEC_ID_H263P:
  3615. if(!s->h263_slice_structured)
  3616. if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
  3617. break;
  3618. case CODEC_ID_MPEG2VIDEO:
  3619. if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
  3620. case CODEC_ID_MPEG1VIDEO:
  3621. if(s->mb_skip_run) is_gob_start=0;
  3622. break;
  3623. }
  3624. if(is_gob_start){
  3625. if(s->start_mb_y != mb_y || mb_x!=0){
  3626. write_slice_end(s);
  3627. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
  3628. ff_mpeg4_init_partitions(s);
  3629. }
  3630. }
  3631. assert((put_bits_count(&s->pb)&7) == 0);
  3632. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  3633. if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
  3634. int r= put_bits_count(&s->pb)/8 + s->picture_number + s->codec_id + s->mb_x + s->mb_y;
  3635. int d= 100 / s->avctx->error_rate;
  3636. if(r % d == 0){
  3637. current_packet_size=0;
  3638. #ifndef ALT_BITSTREAM_WRITER
  3639. s->pb.buf_ptr= s->ptr_lastgob;
  3640. #endif
  3641. assert(pbBufPtr(&s->pb) == s->ptr_lastgob);
  3642. }
  3643. }
  3644. if (s->avctx->rtp_callback)
  3645. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, 0);
  3646. switch(s->codec_id){
  3647. case CODEC_ID_MPEG4:
  3648. ff_mpeg4_encode_video_packet_header(s);
  3649. ff_mpeg4_clean_buffers(s);
  3650. break;
  3651. case CODEC_ID_MPEG1VIDEO:
  3652. case CODEC_ID_MPEG2VIDEO:
  3653. ff_mpeg1_encode_slice_header(s);
  3654. ff_mpeg1_clean_buffers(s);
  3655. break;
  3656. case CODEC_ID_H263:
  3657. case CODEC_ID_H263P:
  3658. h263_encode_gob_header(s, mb_y);
  3659. break;
  3660. }
  3661. if(s->flags&CODEC_FLAG_PASS1){
  3662. int bits= put_bits_count(&s->pb);
  3663. s->misc_bits+= bits - s->last_bits;
  3664. s->last_bits= bits;
  3665. }
  3666. s->ptr_lastgob += current_packet_size;
  3667. s->first_slice_line=1;
  3668. s->resync_mb_x=mb_x;
  3669. s->resync_mb_y=mb_y;
  3670. }
  3671. }
  3672. #endif
  3673. if( (s->resync_mb_x == s->mb_x)
  3674. && s->resync_mb_y+1 == s->mb_y){
  3675. s->first_slice_line=0;
  3676. }
  3677. s->mb_skiped=0;
  3678. s->dquant=0; //only for QP_RD
  3679. if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible or CODEC_FLAG_QP_RD
  3680. int next_block=0;
  3681. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  3682. copy_context_before_encode(&backup_s, s, -1);
  3683. backup_s.pb= s->pb;
  3684. best_s.data_partitioning= s->data_partitioning;
  3685. best_s.partitioned_frame= s->partitioned_frame;
  3686. if(s->data_partitioning){
  3687. backup_s.pb2= s->pb2;
  3688. backup_s.tex_pb= s->tex_pb;
  3689. }
  3690. if(mb_type&CANDIDATE_MB_TYPE_INTER){
  3691. s->mv_dir = MV_DIR_FORWARD;
  3692. s->mv_type = MV_TYPE_16X16;
  3693. s->mb_intra= 0;
  3694. s->mv[0][0][0] = s->p_mv_table[xy][0];
  3695. s->mv[0][0][1] = s->p_mv_table[xy][1];
  3696. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
  3697. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3698. }
  3699. if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
  3700. s->mv_dir = MV_DIR_FORWARD;
  3701. s->mv_type = MV_TYPE_FIELD;
  3702. s->mb_intra= 0;
  3703. for(i=0; i<2; i++){
  3704. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  3705. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  3706. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  3707. }
  3708. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
  3709. &dmin, &next_block, 0, 0);
  3710. }
  3711. if(mb_type&CANDIDATE_MB_TYPE_SKIPED){
  3712. s->mv_dir = MV_DIR_FORWARD;
  3713. s->mv_type = MV_TYPE_16X16;
  3714. s->mb_intra= 0;
  3715. s->mv[0][0][0] = 0;
  3716. s->mv[0][0][1] = 0;
  3717. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPED, pb, pb2, tex_pb,
  3718. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3719. }
  3720. if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
  3721. s->mv_dir = MV_DIR_FORWARD;
  3722. s->mv_type = MV_TYPE_8X8;
  3723. s->mb_intra= 0;
  3724. for(i=0; i<4; i++){
  3725. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  3726. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  3727. }
  3728. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
  3729. &dmin, &next_block, 0, 0);
  3730. }
  3731. if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
  3732. s->mv_dir = MV_DIR_FORWARD;
  3733. s->mv_type = MV_TYPE_16X16;
  3734. s->mb_intra= 0;
  3735. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3736. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3737. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
  3738. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3739. }
  3740. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
  3741. s->mv_dir = MV_DIR_BACKWARD;
  3742. s->mv_type = MV_TYPE_16X16;
  3743. s->mb_intra= 0;
  3744. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3745. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3746. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  3747. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  3748. }
  3749. if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
  3750. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3751. s->mv_type = MV_TYPE_16X16;
  3752. s->mb_intra= 0;
  3753. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3754. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3755. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3756. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3757. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
  3758. &dmin, &next_block, 0, 0);
  3759. }
  3760. if(mb_type&CANDIDATE_MB_TYPE_DIRECT){
  3761. int mx= s->b_direct_mv_table[xy][0];
  3762. int my= s->b_direct_mv_table[xy][1];
  3763. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  3764. s->mb_intra= 0;
  3765. #ifdef CONFIG_RISKY
  3766. ff_mpeg4_set_direct_mv(s, mx, my);
  3767. #endif
  3768. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  3769. &dmin, &next_block, mx, my);
  3770. }
  3771. if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
  3772. s->mv_dir = MV_DIR_FORWARD;
  3773. s->mv_type = MV_TYPE_FIELD;
  3774. s->mb_intra= 0;
  3775. for(i=0; i<2; i++){
  3776. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  3777. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  3778. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  3779. }
  3780. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
  3781. &dmin, &next_block, 0, 0);
  3782. }
  3783. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
  3784. s->mv_dir = MV_DIR_BACKWARD;
  3785. s->mv_type = MV_TYPE_FIELD;
  3786. s->mb_intra= 0;
  3787. for(i=0; i<2; i++){
  3788. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  3789. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  3790. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  3791. }
  3792. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
  3793. &dmin, &next_block, 0, 0);
  3794. }
  3795. if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
  3796. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3797. s->mv_type = MV_TYPE_FIELD;
  3798. s->mb_intra= 0;
  3799. for(dir=0; dir<2; dir++){
  3800. for(i=0; i<2; i++){
  3801. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  3802. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  3803. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  3804. }
  3805. }
  3806. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
  3807. &dmin, &next_block, 0, 0);
  3808. }
  3809. if(mb_type&CANDIDATE_MB_TYPE_INTRA){
  3810. s->mv_dir = 0;
  3811. s->mv_type = MV_TYPE_16X16;
  3812. s->mb_intra= 1;
  3813. s->mv[0][0][0] = 0;
  3814. s->mv[0][0][1] = 0;
  3815. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
  3816. &dmin, &next_block, 0, 0);
  3817. if(s->h263_pred || s->h263_aic){
  3818. if(best_s.mb_intra)
  3819. s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  3820. else
  3821. ff_clean_intra_table_entries(s); //old mode?
  3822. }
  3823. }
  3824. if(s->flags & CODEC_FLAG_QP_RD){
  3825. if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){
  3826. const int last_qp= backup_s.qscale;
  3827. int dquant, dir, qp, dc[6];
  3828. DCTELEM ac[6][16];
  3829. const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
  3830. assert(backup_s.dquant == 0);
  3831. //FIXME intra
  3832. s->mv_dir= best_s.mv_dir;
  3833. s->mv_type = MV_TYPE_16X16;
  3834. s->mb_intra= best_s.mb_intra;
  3835. s->mv[0][0][0] = best_s.mv[0][0][0];
  3836. s->mv[0][0][1] = best_s.mv[0][0][1];
  3837. s->mv[1][0][0] = best_s.mv[1][0][0];
  3838. s->mv[1][0][1] = best_s.mv[1][0][1];
  3839. dir= s->pict_type == B_TYPE ? 2 : 1;
  3840. if(last_qp + dir > s->avctx->qmax) dir= -dir;
  3841. for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){
  3842. qp= last_qp + dquant;
  3843. if(qp < s->avctx->qmin || qp > s->avctx->qmax)
  3844. break;
  3845. backup_s.dquant= dquant;
  3846. if(s->mb_intra && s->dc_val[0]){
  3847. for(i=0; i<6; i++){
  3848. dc[i]= s->dc_val[0][ s->block_index[i] ];
  3849. memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
  3850. }
  3851. }
  3852. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  3853. &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
  3854. if(best_s.qscale != qp){
  3855. if(s->mb_intra && s->dc_val[0]){
  3856. for(i=0; i<6; i++){
  3857. s->dc_val[0][ s->block_index[i] ]= dc[i];
  3858. memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
  3859. }
  3860. }
  3861. if(dir > 0 && dquant==dir){
  3862. dquant= 0;
  3863. dir= -dir;
  3864. }else
  3865. break;
  3866. }
  3867. }
  3868. qp= best_s.qscale;
  3869. s->current_picture.qscale_table[xy]= qp;
  3870. }
  3871. }
  3872. copy_context_after_encode(s, &best_s, -1);
  3873. pb_bits_count= put_bits_count(&s->pb);
  3874. flush_put_bits(&s->pb);
  3875. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  3876. s->pb= backup_s.pb;
  3877. if(s->data_partitioning){
  3878. pb2_bits_count= put_bits_count(&s->pb2);
  3879. flush_put_bits(&s->pb2);
  3880. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  3881. s->pb2= backup_s.pb2;
  3882. tex_pb_bits_count= put_bits_count(&s->tex_pb);
  3883. flush_put_bits(&s->tex_pb);
  3884. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  3885. s->tex_pb= backup_s.tex_pb;
  3886. }
  3887. s->last_bits= put_bits_count(&s->pb);
  3888. #ifdef CONFIG_RISKY
  3889. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  3890. ff_h263_update_motion_val(s);
  3891. #endif
  3892. if(next_block==0){ //FIXME 16 vs linesize16
  3893. s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
  3894. s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
  3895. s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
  3896. }
  3897. if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  3898. MPV_decode_mb(s, s->block);
  3899. } else {
  3900. int motion_x, motion_y;
  3901. s->mv_type=MV_TYPE_16X16;
  3902. // only one MB-Type possible
  3903. switch(mb_type){
  3904. case CANDIDATE_MB_TYPE_INTRA:
  3905. s->mv_dir = 0;
  3906. s->mb_intra= 1;
  3907. motion_x= s->mv[0][0][0] = 0;
  3908. motion_y= s->mv[0][0][1] = 0;
  3909. break;
  3910. case CANDIDATE_MB_TYPE_INTER:
  3911. s->mv_dir = MV_DIR_FORWARD;
  3912. s->mb_intra= 0;
  3913. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  3914. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  3915. break;
  3916. case CANDIDATE_MB_TYPE_INTER_I:
  3917. s->mv_dir = MV_DIR_FORWARD;
  3918. s->mv_type = MV_TYPE_FIELD;
  3919. s->mb_intra= 0;
  3920. for(i=0; i<2; i++){
  3921. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  3922. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  3923. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  3924. }
  3925. motion_x = motion_y = 0;
  3926. break;
  3927. case CANDIDATE_MB_TYPE_INTER4V:
  3928. s->mv_dir = MV_DIR_FORWARD;
  3929. s->mv_type = MV_TYPE_8X8;
  3930. s->mb_intra= 0;
  3931. for(i=0; i<4; i++){
  3932. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  3933. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  3934. }
  3935. motion_x= motion_y= 0;
  3936. break;
  3937. case CANDIDATE_MB_TYPE_DIRECT:
  3938. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  3939. s->mb_intra= 0;
  3940. motion_x=s->b_direct_mv_table[xy][0];
  3941. motion_y=s->b_direct_mv_table[xy][1];
  3942. #ifdef CONFIG_RISKY
  3943. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  3944. #endif
  3945. break;
  3946. case CANDIDATE_MB_TYPE_BIDIR:
  3947. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3948. s->mb_intra= 0;
  3949. motion_x=0;
  3950. motion_y=0;
  3951. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3952. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3953. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3954. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3955. break;
  3956. case CANDIDATE_MB_TYPE_BACKWARD:
  3957. s->mv_dir = MV_DIR_BACKWARD;
  3958. s->mb_intra= 0;
  3959. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3960. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3961. break;
  3962. case CANDIDATE_MB_TYPE_FORWARD:
  3963. s->mv_dir = MV_DIR_FORWARD;
  3964. s->mb_intra= 0;
  3965. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3966. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3967. // printf(" %d %d ", motion_x, motion_y);
  3968. break;
  3969. case CANDIDATE_MB_TYPE_FORWARD_I:
  3970. s->mv_dir = MV_DIR_FORWARD;
  3971. s->mv_type = MV_TYPE_FIELD;
  3972. s->mb_intra= 0;
  3973. for(i=0; i<2; i++){
  3974. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  3975. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  3976. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  3977. }
  3978. motion_x=motion_y=0;
  3979. break;
  3980. case CANDIDATE_MB_TYPE_BACKWARD_I:
  3981. s->mv_dir = MV_DIR_BACKWARD;
  3982. s->mv_type = MV_TYPE_FIELD;
  3983. s->mb_intra= 0;
  3984. for(i=0; i<2; i++){
  3985. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  3986. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  3987. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  3988. }
  3989. motion_x=motion_y=0;
  3990. break;
  3991. case CANDIDATE_MB_TYPE_BIDIR_I:
  3992. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3993. s->mv_type = MV_TYPE_FIELD;
  3994. s->mb_intra= 0;
  3995. for(dir=0; dir<2; dir++){
  3996. for(i=0; i<2; i++){
  3997. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  3998. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  3999. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  4000. }
  4001. }
  4002. motion_x=motion_y=0;
  4003. break;
  4004. default:
  4005. motion_x=motion_y=0; //gcc warning fix
  4006. av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
  4007. }
  4008. encode_mb(s, motion_x, motion_y);
  4009. // RAL: Update last macrobloc type
  4010. s->last_mv_dir = s->mv_dir;
  4011. #ifdef CONFIG_RISKY
  4012. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  4013. ff_h263_update_motion_val(s);
  4014. #endif
  4015. MPV_decode_mb(s, s->block);
  4016. }
  4017. /* clean the MV table in IPS frames for direct mode in B frames */
  4018. if(s->mb_intra /* && I,P,S_TYPE */){
  4019. s->p_mv_table[xy][0]=0;
  4020. s->p_mv_table[xy][1]=0;
  4021. }
  4022. if(s->flags&CODEC_FLAG_PSNR){
  4023. int w= 16;
  4024. int h= 16;
  4025. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  4026. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  4027. s->current_picture_ptr->error[0] += sse(
  4028. s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  4029. s->dest[0], w, h, s->linesize);
  4030. s->current_picture_ptr->error[1] += sse(
  4031. s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  4032. s->dest[1], w>>1, h>>1, s->uvlinesize);
  4033. s->current_picture_ptr->error[2] += sse(
  4034. s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  4035. s->dest[2], w>>1, h>>1, s->uvlinesize);
  4036. }
  4037. if(s->loop_filter)
  4038. ff_h263_loop_filter(s);
  4039. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
  4040. }
  4041. }
  4042. #ifdef CONFIG_RISKY
  4043. //not beautifull here but we must write it before flushing so it has to be here
  4044. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  4045. msmpeg4_encode_ext_header(s);
  4046. #endif
  4047. write_slice_end(s);
  4048. /* Send the last GOB if RTP */
  4049. if (s->avctx->rtp_callback) {
  4050. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  4051. /* Call the RTP callback to send the last GOB */
  4052. emms_c();
  4053. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, 0);
  4054. }
  4055. return 0;
  4056. }
  4057. #define MERGE(field) dst->field += src->field; src->field=0
  4058. static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
  4059. MERGE(me.scene_change_score);
  4060. MERGE(me.mc_mb_var_sum_temp);
  4061. MERGE(me.mb_var_sum_temp);
  4062. }
  4063. static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
  4064. int i;
  4065. MERGE(dct_count[0]); //note, the other dct vars are not part of the context
  4066. MERGE(dct_count[1]);
  4067. MERGE(mv_bits);
  4068. MERGE(i_tex_bits);
  4069. MERGE(p_tex_bits);
  4070. MERGE(i_count);
  4071. MERGE(f_count);
  4072. MERGE(b_count);
  4073. MERGE(skip_count);
  4074. MERGE(misc_bits);
  4075. MERGE(error_count);
  4076. MERGE(padding_bug_score);
  4077. if(dst->avctx->noise_reduction){
  4078. for(i=0; i<64; i++){
  4079. MERGE(dct_error_sum[0][i]);
  4080. MERGE(dct_error_sum[1][i]);
  4081. }
  4082. }
  4083. assert(put_bits_count(&src->pb) % 8 ==0);
  4084. assert(put_bits_count(&dst->pb) % 8 ==0);
  4085. ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
  4086. flush_put_bits(&dst->pb);
  4087. }
  4088. static void encode_picture(MpegEncContext *s, int picture_number)
  4089. {
  4090. int i;
  4091. int bits;
  4092. s->picture_number = picture_number;
  4093. /* Reset the average MB variance */
  4094. s->me.mb_var_sum_temp =
  4095. s->me.mc_mb_var_sum_temp = 0;
  4096. #ifdef CONFIG_RISKY
  4097. /* we need to initialize some time vars before we can encode b-frames */
  4098. // RAL: Condition added for MPEG1VIDEO
  4099. if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
  4100. ff_set_mpeg4_time(s, s->picture_number); //FIXME rename and use has_b_frames or similar
  4101. #endif
  4102. s->me.scene_change_score=0;
  4103. // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration
  4104. if(s->pict_type==I_TYPE){
  4105. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  4106. else s->no_rounding=0;
  4107. }else if(s->pict_type!=B_TYPE){
  4108. if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
  4109. s->no_rounding ^= 1;
  4110. }
  4111. s->mb_intra=0; //for the rate distoration & bit compare functions
  4112. for(i=1; i<s->avctx->thread_count; i++){
  4113. ff_update_duplicate_context(s->thread_context[i], s);
  4114. }
  4115. ff_init_me(s);
  4116. /* Estimate motion for every MB */
  4117. if(s->pict_type != I_TYPE){
  4118. if(s->pict_type != B_TYPE && s->avctx->me_threshold==0){
  4119. if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
  4120. s->avctx->execute(s->avctx, pre_estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  4121. }
  4122. }
  4123. s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  4124. }else /* if(s->pict_type == I_TYPE) */{
  4125. /* I-Frame */
  4126. for(i=0; i<s->mb_stride*s->mb_height; i++)
  4127. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  4128. if(!s->fixed_qscale){
  4129. /* finding spatial complexity for I-frame rate control */
  4130. s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  4131. }
  4132. }
  4133. for(i=1; i<s->avctx->thread_count; i++){
  4134. merge_context_after_me(s, s->thread_context[i]);
  4135. }
  4136. s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
  4137. s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
  4138. emms_c();
  4139. if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){
  4140. s->pict_type= I_TYPE;
  4141. for(i=0; i<s->mb_stride*s->mb_height; i++)
  4142. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  4143. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  4144. }
  4145. if(!s->umvplus){
  4146. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
  4147. s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
  4148. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  4149. int a,b;
  4150. a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
  4151. b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
  4152. s->f_code= FFMAX(s->f_code, FFMAX(a,b));
  4153. }
  4154. ff_fix_long_p_mvs(s);
  4155. ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
  4156. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  4157. int j;
  4158. for(i=0; i<2; i++){
  4159. for(j=0; j<2; j++)
  4160. ff_fix_long_mvs(s, s->p_field_select_table[i], j,
  4161. s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
  4162. }
  4163. }
  4164. }
  4165. if(s->pict_type==B_TYPE){
  4166. int a, b;
  4167. a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
  4168. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  4169. s->f_code = FFMAX(a, b);
  4170. a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
  4171. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  4172. s->b_code = FFMAX(a, b);
  4173. ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
  4174. ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
  4175. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  4176. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  4177. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  4178. int dir, j;
  4179. for(dir=0; dir<2; dir++){
  4180. for(i=0; i<2; i++){
  4181. for(j=0; j<2; j++){
  4182. int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
  4183. : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
  4184. ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
  4185. s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
  4186. }
  4187. }
  4188. }
  4189. }
  4190. }
  4191. }
  4192. if (!s->fixed_qscale)
  4193. s->current_picture.quality = ff_rate_estimate_qscale(s); //FIXME pic_ptr
  4194. if(s->adaptive_quant){
  4195. #ifdef CONFIG_RISKY
  4196. switch(s->codec_id){
  4197. case CODEC_ID_MPEG4:
  4198. ff_clean_mpeg4_qscales(s);
  4199. break;
  4200. case CODEC_ID_H263:
  4201. case CODEC_ID_H263P:
  4202. case CODEC_ID_FLV1:
  4203. ff_clean_h263_qscales(s);
  4204. break;
  4205. }
  4206. #endif
  4207. s->lambda= s->lambda_table[0];
  4208. //FIXME broken
  4209. }else
  4210. s->lambda= s->current_picture.quality;
  4211. //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
  4212. update_qscale(s);
  4213. if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
  4214. s->qscale= 3; //reduce cliping problems
  4215. if (s->out_format == FMT_MJPEG) {
  4216. /* for mjpeg, we do include qscale in the matrix */
  4217. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  4218. for(i=1;i<64;i++){
  4219. int j= s->dsp.idct_permutation[i];
  4220. s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  4221. }
  4222. convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  4223. s->intra_matrix, s->intra_quant_bias, 8, 8);
  4224. s->qscale= 8;
  4225. }
  4226. //FIXME var duplication
  4227. s->current_picture_ptr->key_frame=
  4228. s->current_picture.key_frame= s->pict_type == I_TYPE; //FIXME pic_ptr
  4229. s->current_picture_ptr->pict_type=
  4230. s->current_picture.pict_type= s->pict_type;
  4231. if(s->current_picture.key_frame)
  4232. s->picture_in_gop_number=0;
  4233. s->last_bits= put_bits_count(&s->pb);
  4234. switch(s->out_format) {
  4235. case FMT_MJPEG:
  4236. mjpeg_picture_header(s);
  4237. break;
  4238. #ifdef CONFIG_RISKY
  4239. case FMT_H263:
  4240. if (s->codec_id == CODEC_ID_WMV2)
  4241. ff_wmv2_encode_picture_header(s, picture_number);
  4242. else if (s->h263_msmpeg4)
  4243. msmpeg4_encode_picture_header(s, picture_number);
  4244. else if (s->h263_pred)
  4245. mpeg4_encode_picture_header(s, picture_number);
  4246. else if (s->codec_id == CODEC_ID_RV10)
  4247. rv10_encode_picture_header(s, picture_number);
  4248. else if (s->codec_id == CODEC_ID_FLV1)
  4249. ff_flv_encode_picture_header(s, picture_number);
  4250. else
  4251. h263_encode_picture_header(s, picture_number);
  4252. break;
  4253. #endif
  4254. case FMT_MPEG1:
  4255. mpeg1_encode_picture_header(s, picture_number);
  4256. break;
  4257. case FMT_H264:
  4258. break;
  4259. default:
  4260. assert(0);
  4261. }
  4262. bits= put_bits_count(&s->pb);
  4263. s->header_bits= bits - s->last_bits;
  4264. for(i=1; i<s->avctx->thread_count; i++){
  4265. update_duplicate_context_after_me(s->thread_context[i], s);
  4266. }
  4267. s->avctx->execute(s->avctx, encode_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  4268. for(i=1; i<s->avctx->thread_count; i++){
  4269. merge_context_after_encode(s, s->thread_context[i]);
  4270. }
  4271. emms_c();
  4272. }
  4273. #endif //CONFIG_ENCODERS
  4274. static void denoise_dct_c(MpegEncContext *s, DCTELEM *block){
  4275. const int intra= s->mb_intra;
  4276. int i;
  4277. s->dct_count[intra]++;
  4278. for(i=0; i<64; i++){
  4279. int level= block[i];
  4280. if(level){
  4281. if(level>0){
  4282. s->dct_error_sum[intra][i] += level;
  4283. level -= s->dct_offset[intra][i];
  4284. if(level<0) level=0;
  4285. }else{
  4286. s->dct_error_sum[intra][i] -= level;
  4287. level += s->dct_offset[intra][i];
  4288. if(level>0) level=0;
  4289. }
  4290. block[i]= level;
  4291. }
  4292. }
  4293. }
  4294. #ifdef CONFIG_ENCODERS
  4295. static int dct_quantize_trellis_c(MpegEncContext *s,
  4296. DCTELEM *block, int n,
  4297. int qscale, int *overflow){
  4298. const int *qmat;
  4299. const uint8_t *scantable= s->intra_scantable.scantable;
  4300. const uint8_t *perm_scantable= s->intra_scantable.permutated;
  4301. int max=0;
  4302. unsigned int threshold1, threshold2;
  4303. int bias=0;
  4304. int run_tab[65];
  4305. int level_tab[65];
  4306. int score_tab[65];
  4307. int survivor[65];
  4308. int survivor_count;
  4309. int last_run=0;
  4310. int last_level=0;
  4311. int last_score= 0;
  4312. int last_i;
  4313. int coeff[2][64];
  4314. int coeff_count[64];
  4315. int qmul, qadd, start_i, last_non_zero, i, dc;
  4316. const int esc_length= s->ac_esc_length;
  4317. uint8_t * length;
  4318. uint8_t * last_length;
  4319. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  4320. s->dsp.fdct (block);
  4321. if(s->dct_error_sum)
  4322. s->denoise_dct(s, block);
  4323. qmul= qscale*16;
  4324. qadd= ((qscale-1)|1)*8;
  4325. if (s->mb_intra) {
  4326. int q;
  4327. if (!s->h263_aic) {
  4328. if (n < 4)
  4329. q = s->y_dc_scale;
  4330. else
  4331. q = s->c_dc_scale;
  4332. q = q << 3;
  4333. } else{
  4334. /* For AIC we skip quant/dequant of INTRADC */
  4335. q = 1 << 3;
  4336. qadd=0;
  4337. }
  4338. /* note: block[0] is assumed to be positive */
  4339. block[0] = (block[0] + (q >> 1)) / q;
  4340. start_i = 1;
  4341. last_non_zero = 0;
  4342. qmat = s->q_intra_matrix[qscale];
  4343. if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  4344. bias= 1<<(QMAT_SHIFT-1);
  4345. length = s->intra_ac_vlc_length;
  4346. last_length= s->intra_ac_vlc_last_length;
  4347. } else {
  4348. start_i = 0;
  4349. last_non_zero = -1;
  4350. qmat = s->q_inter_matrix[qscale];
  4351. length = s->inter_ac_vlc_length;
  4352. last_length= s->inter_ac_vlc_last_length;
  4353. }
  4354. last_i= start_i;
  4355. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  4356. threshold2= (threshold1<<1);
  4357. for(i=63; i>=start_i; i--) {
  4358. const int j = scantable[i];
  4359. int level = block[j] * qmat[j];
  4360. if(((unsigned)(level+threshold1))>threshold2){
  4361. last_non_zero = i;
  4362. break;
  4363. }
  4364. }
  4365. for(i=start_i; i<=last_non_zero; i++) {
  4366. const int j = scantable[i];
  4367. int level = block[j] * qmat[j];
  4368. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  4369. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  4370. if(((unsigned)(level+threshold1))>threshold2){
  4371. if(level>0){
  4372. level= (bias + level)>>QMAT_SHIFT;
  4373. coeff[0][i]= level;
  4374. coeff[1][i]= level-1;
  4375. // coeff[2][k]= level-2;
  4376. }else{
  4377. level= (bias - level)>>QMAT_SHIFT;
  4378. coeff[0][i]= -level;
  4379. coeff[1][i]= -level+1;
  4380. // coeff[2][k]= -level+2;
  4381. }
  4382. coeff_count[i]= FFMIN(level, 2);
  4383. assert(coeff_count[i]);
  4384. max |=level;
  4385. }else{
  4386. coeff[0][i]= (level>>31)|1;
  4387. coeff_count[i]= 1;
  4388. }
  4389. }
  4390. *overflow= s->max_qcoeff < max; //overflow might have happend
  4391. if(last_non_zero < start_i){
  4392. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  4393. return last_non_zero;
  4394. }
  4395. score_tab[start_i]= 0;
  4396. survivor[0]= start_i;
  4397. survivor_count= 1;
  4398. for(i=start_i; i<=last_non_zero; i++){
  4399. int level_index, j;
  4400. const int dct_coeff= ABS(block[ scantable[i] ]);
  4401. const int zero_distoration= dct_coeff*dct_coeff;
  4402. int best_score=256*256*256*120;
  4403. for(level_index=0; level_index < coeff_count[i]; level_index++){
  4404. int distoration;
  4405. int level= coeff[level_index][i];
  4406. const int alevel= ABS(level);
  4407. int unquant_coeff;
  4408. assert(level);
  4409. if(s->out_format == FMT_H263){
  4410. unquant_coeff= alevel*qmul + qadd;
  4411. }else{ //MPEG1
  4412. j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
  4413. if(s->mb_intra){
  4414. unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
  4415. unquant_coeff = (unquant_coeff - 1) | 1;
  4416. }else{
  4417. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  4418. unquant_coeff = (unquant_coeff - 1) | 1;
  4419. }
  4420. unquant_coeff<<= 3;
  4421. }
  4422. distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distoration;
  4423. level+=64;
  4424. if((level&(~127)) == 0){
  4425. for(j=survivor_count-1; j>=0; j--){
  4426. int run= i - survivor[j];
  4427. int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  4428. score += score_tab[i-run];
  4429. if(score < best_score){
  4430. best_score= score;
  4431. run_tab[i+1]= run;
  4432. level_tab[i+1]= level-64;
  4433. }
  4434. }
  4435. if(s->out_format == FMT_H263){
  4436. for(j=survivor_count-1; j>=0; j--){
  4437. int run= i - survivor[j];
  4438. int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  4439. score += score_tab[i-run];
  4440. if(score < last_score){
  4441. last_score= score;
  4442. last_run= run;
  4443. last_level= level-64;
  4444. last_i= i+1;
  4445. }
  4446. }
  4447. }
  4448. }else{
  4449. distoration += esc_length*lambda;
  4450. for(j=survivor_count-1; j>=0; j--){
  4451. int run= i - survivor[j];
  4452. int score= distoration + score_tab[i-run];
  4453. if(score < best_score){
  4454. best_score= score;
  4455. run_tab[i+1]= run;
  4456. level_tab[i+1]= level-64;
  4457. }
  4458. }
  4459. if(s->out_format == FMT_H263){
  4460. for(j=survivor_count-1; j>=0; j--){
  4461. int run= i - survivor[j];
  4462. int score= distoration + score_tab[i-run];
  4463. if(score < last_score){
  4464. last_score= score;
  4465. last_run= run;
  4466. last_level= level-64;
  4467. last_i= i+1;
  4468. }
  4469. }
  4470. }
  4471. }
  4472. }
  4473. score_tab[i+1]= best_score;
  4474. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  4475. if(last_non_zero <= 27){
  4476. for(; survivor_count; survivor_count--){
  4477. if(score_tab[ survivor[survivor_count-1] ] <= best_score)
  4478. break;
  4479. }
  4480. }else{
  4481. for(; survivor_count; survivor_count--){
  4482. if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
  4483. break;
  4484. }
  4485. }
  4486. survivor[ survivor_count++ ]= i+1;
  4487. }
  4488. if(s->out_format != FMT_H263){
  4489. last_score= 256*256*256*120;
  4490. for(i= survivor[0]; i<=last_non_zero + 1; i++){
  4491. int score= score_tab[i];
  4492. if(i) score += lambda*2; //FIXME exacter?
  4493. if(score < last_score){
  4494. last_score= score;
  4495. last_i= i;
  4496. last_level= level_tab[i];
  4497. last_run= run_tab[i];
  4498. }
  4499. }
  4500. }
  4501. s->coded_score[n] = last_score;
  4502. dc= ABS(block[0]);
  4503. last_non_zero= last_i - 1;
  4504. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  4505. if(last_non_zero < start_i)
  4506. return last_non_zero;
  4507. if(last_non_zero == 0 && start_i == 0){
  4508. int best_level= 0;
  4509. int best_score= dc * dc;
  4510. for(i=0; i<coeff_count[0]; i++){
  4511. int level= coeff[i][0];
  4512. int alevel= ABS(level);
  4513. int unquant_coeff, score, distortion;
  4514. if(s->out_format == FMT_H263){
  4515. unquant_coeff= (alevel*qmul + qadd)>>3;
  4516. }else{ //MPEG1
  4517. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
  4518. unquant_coeff = (unquant_coeff - 1) | 1;
  4519. }
  4520. unquant_coeff = (unquant_coeff + 4) >> 3;
  4521. unquant_coeff<<= 3 + 3;
  4522. distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
  4523. level+=64;
  4524. if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
  4525. else score= distortion + esc_length*lambda;
  4526. if(score < best_score){
  4527. best_score= score;
  4528. best_level= level - 64;
  4529. }
  4530. }
  4531. block[0]= best_level;
  4532. s->coded_score[n] = best_score - dc*dc;
  4533. if(best_level == 0) return -1;
  4534. else return last_non_zero;
  4535. }
  4536. i= last_i;
  4537. assert(last_level);
  4538. block[ perm_scantable[last_non_zero] ]= last_level;
  4539. i -= last_run + 1;
  4540. for(; i>start_i; i -= run_tab[i] + 1){
  4541. block[ perm_scantable[i-1] ]= level_tab[i];
  4542. }
  4543. return last_non_zero;
  4544. }
  4545. //#define REFINE_STATS 1
  4546. static int16_t basis[64][64];
  4547. static void build_basis(uint8_t *perm){
  4548. int i, j, x, y;
  4549. emms_c();
  4550. for(i=0; i<8; i++){
  4551. for(j=0; j<8; j++){
  4552. for(y=0; y<8; y++){
  4553. for(x=0; x<8; x++){
  4554. double s= 0.25*(1<<BASIS_SHIFT);
  4555. int index= 8*i + j;
  4556. int perm_index= perm[index];
  4557. if(i==0) s*= sqrt(0.5);
  4558. if(j==0) s*= sqrt(0.5);
  4559. basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
  4560. }
  4561. }
  4562. }
  4563. }
  4564. }
  4565. static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
  4566. DCTELEM *block, int16_t *weight, DCTELEM *orig,
  4567. int n, int qscale){
  4568. int16_t rem[64];
  4569. DCTELEM d1[64];
  4570. const int *qmat;
  4571. const uint8_t *scantable= s->intra_scantable.scantable;
  4572. const uint8_t *perm_scantable= s->intra_scantable.permutated;
  4573. // unsigned int threshold1, threshold2;
  4574. // int bias=0;
  4575. int run_tab[65];
  4576. int prev_run=0;
  4577. int prev_level=0;
  4578. int qmul, qadd, start_i, last_non_zero, i, dc;
  4579. uint8_t * length;
  4580. uint8_t * last_length;
  4581. int lambda;
  4582. int rle_index, run, q, sum;
  4583. #ifdef REFINE_STATS
  4584. static int count=0;
  4585. static int after_last=0;
  4586. static int to_zero=0;
  4587. static int from_zero=0;
  4588. static int raise=0;
  4589. static int lower=0;
  4590. static int messed_sign=0;
  4591. #endif
  4592. if(basis[0][0] == 0)
  4593. build_basis(s->dsp.idct_permutation);
  4594. qmul= qscale*2;
  4595. qadd= (qscale-1)|1;
  4596. if (s->mb_intra) {
  4597. if (!s->h263_aic) {
  4598. if (n < 4)
  4599. q = s->y_dc_scale;
  4600. else
  4601. q = s->c_dc_scale;
  4602. } else{
  4603. /* For AIC we skip quant/dequant of INTRADC */
  4604. q = 1;
  4605. qadd=0;
  4606. }
  4607. q <<= RECON_SHIFT-3;
  4608. /* note: block[0] is assumed to be positive */
  4609. dc= block[0]*q;
  4610. // block[0] = (block[0] + (q >> 1)) / q;
  4611. start_i = 1;
  4612. qmat = s->q_intra_matrix[qscale];
  4613. // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  4614. // bias= 1<<(QMAT_SHIFT-1);
  4615. length = s->intra_ac_vlc_length;
  4616. last_length= s->intra_ac_vlc_last_length;
  4617. } else {
  4618. dc= 0;
  4619. start_i = 0;
  4620. qmat = s->q_inter_matrix[qscale];
  4621. length = s->inter_ac_vlc_length;
  4622. last_length= s->inter_ac_vlc_last_length;
  4623. }
  4624. last_non_zero = s->block_last_index[n];
  4625. #ifdef REFINE_STATS
  4626. {START_TIMER
  4627. #endif
  4628. dc += (1<<(RECON_SHIFT-1));
  4629. for(i=0; i<64; i++){
  4630. rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly insteadof copying to rem[]
  4631. }
  4632. #ifdef REFINE_STATS
  4633. STOP_TIMER("memset rem[]")}
  4634. #endif
  4635. sum=0;
  4636. for(i=0; i<64; i++){
  4637. int one= 36;
  4638. int qns=4;
  4639. int w;
  4640. w= ABS(weight[i]) + qns*one;
  4641. w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
  4642. weight[i] = w;
  4643. // w=weight[i] = (63*qns + (w/2)) / w;
  4644. assert(w>0);
  4645. assert(w<(1<<6));
  4646. sum += w*w;
  4647. }
  4648. lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
  4649. #ifdef REFINE_STATS
  4650. {START_TIMER
  4651. #endif
  4652. run=0;
  4653. rle_index=0;
  4654. for(i=start_i; i<=last_non_zero; i++){
  4655. int j= perm_scantable[i];
  4656. const int level= block[j];
  4657. int coeff;
  4658. if(level){
  4659. if(level<0) coeff= qmul*level - qadd;
  4660. else coeff= qmul*level + qadd;
  4661. run_tab[rle_index++]=run;
  4662. run=0;
  4663. s->dsp.add_8x8basis(rem, basis[j], coeff);
  4664. }else{
  4665. run++;
  4666. }
  4667. }
  4668. #ifdef REFINE_STATS
  4669. if(last_non_zero>0){
  4670. STOP_TIMER("init rem[]")
  4671. }
  4672. }
  4673. {START_TIMER
  4674. #endif
  4675. for(;;){
  4676. int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
  4677. int best_coeff=0;
  4678. int best_change=0;
  4679. int run2, best_unquant_change=0, analyze_gradient;
  4680. #ifdef REFINE_STATS
  4681. {START_TIMER
  4682. #endif
  4683. analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
  4684. if(analyze_gradient){
  4685. #ifdef REFINE_STATS
  4686. {START_TIMER
  4687. #endif
  4688. for(i=0; i<64; i++){
  4689. int w= weight[i];
  4690. d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
  4691. }
  4692. #ifdef REFINE_STATS
  4693. STOP_TIMER("rem*w*w")}
  4694. {START_TIMER
  4695. #endif
  4696. s->dsp.fdct(d1);
  4697. #ifdef REFINE_STATS
  4698. STOP_TIMER("dct")}
  4699. #endif
  4700. }
  4701. if(start_i){
  4702. const int level= block[0];
  4703. int change, old_coeff;
  4704. assert(s->mb_intra);
  4705. old_coeff= q*level;
  4706. for(change=-1; change<=1; change+=2){
  4707. int new_level= level + change;
  4708. int score, new_coeff;
  4709. new_coeff= q*new_level;
  4710. if(new_coeff >= 2048 || new_coeff < 0)
  4711. continue;
  4712. score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
  4713. if(score<best_score){
  4714. best_score= score;
  4715. best_coeff= 0;
  4716. best_change= change;
  4717. best_unquant_change= new_coeff - old_coeff;
  4718. }
  4719. }
  4720. }
  4721. run=0;
  4722. rle_index=0;
  4723. run2= run_tab[rle_index++];
  4724. prev_level=0;
  4725. prev_run=0;
  4726. for(i=start_i; i<64; i++){
  4727. int j= perm_scantable[i];
  4728. const int level= block[j];
  4729. int change, old_coeff;
  4730. if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
  4731. break;
  4732. if(level){
  4733. if(level<0) old_coeff= qmul*level - qadd;
  4734. else old_coeff= qmul*level + qadd;
  4735. run2= run_tab[rle_index++]; //FIXME ! maybe after last
  4736. }else{
  4737. old_coeff=0;
  4738. run2--;
  4739. assert(run2>=0 || i >= last_non_zero );
  4740. }
  4741. for(change=-1; change<=1; change+=2){
  4742. int new_level= level + change;
  4743. int score, new_coeff, unquant_change;
  4744. score=0;
  4745. if(s->avctx->quantizer_noise_shaping < 2 && ABS(new_level) > ABS(level))
  4746. continue;
  4747. if(new_level){
  4748. if(new_level<0) new_coeff= qmul*new_level - qadd;
  4749. else new_coeff= qmul*new_level + qadd;
  4750. if(new_coeff >= 2048 || new_coeff <= -2048)
  4751. continue;
  4752. //FIXME check for overflow
  4753. if(level){
  4754. if(level < 63 && level > -63){
  4755. if(i < last_non_zero)
  4756. score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
  4757. - length[UNI_AC_ENC_INDEX(run, level+64)];
  4758. else
  4759. score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
  4760. - last_length[UNI_AC_ENC_INDEX(run, level+64)];
  4761. }
  4762. }else{
  4763. assert(ABS(new_level)==1);
  4764. if(analyze_gradient){
  4765. int g= d1[ scantable[i] ];
  4766. if(g && (g^new_level) >= 0)
  4767. continue;
  4768. }
  4769. if(i < last_non_zero){
  4770. int next_i= i + run2 + 1;
  4771. int next_level= block[ perm_scantable[next_i] ] + 64;
  4772. if(next_level&(~127))
  4773. next_level= 0;
  4774. if(next_i < last_non_zero)
  4775. score += length[UNI_AC_ENC_INDEX(run, 65)]
  4776. + length[UNI_AC_ENC_INDEX(run2, next_level)]
  4777. - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  4778. else
  4779. score += length[UNI_AC_ENC_INDEX(run, 65)]
  4780. + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  4781. - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  4782. }else{
  4783. score += last_length[UNI_AC_ENC_INDEX(run, 65)];
  4784. if(prev_level){
  4785. score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  4786. - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  4787. }
  4788. }
  4789. }
  4790. }else{
  4791. new_coeff=0;
  4792. assert(ABS(level)==1);
  4793. if(i < last_non_zero){
  4794. int next_i= i + run2 + 1;
  4795. int next_level= block[ perm_scantable[next_i] ] + 64;
  4796. if(next_level&(~127))
  4797. next_level= 0;
  4798. if(next_i < last_non_zero)
  4799. score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  4800. - length[UNI_AC_ENC_INDEX(run2, next_level)]
  4801. - length[UNI_AC_ENC_INDEX(run, 65)];
  4802. else
  4803. score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  4804. - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  4805. - length[UNI_AC_ENC_INDEX(run, 65)];
  4806. }else{
  4807. score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
  4808. if(prev_level){
  4809. score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  4810. - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  4811. }
  4812. }
  4813. }
  4814. score *= lambda;
  4815. unquant_change= new_coeff - old_coeff;
  4816. assert((score < 100*lambda && score > -100*lambda) || lambda==0);
  4817. score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
  4818. if(score<best_score){
  4819. best_score= score;
  4820. best_coeff= i;
  4821. best_change= change;
  4822. best_unquant_change= unquant_change;
  4823. }
  4824. }
  4825. if(level){
  4826. prev_level= level + 64;
  4827. if(prev_level&(~127))
  4828. prev_level= 0;
  4829. prev_run= run;
  4830. run=0;
  4831. }else{
  4832. run++;
  4833. }
  4834. }
  4835. #ifdef REFINE_STATS
  4836. STOP_TIMER("iterative step")}
  4837. #endif
  4838. if(best_change){
  4839. int j= perm_scantable[ best_coeff ];
  4840. block[j] += best_change;
  4841. if(best_coeff > last_non_zero){
  4842. last_non_zero= best_coeff;
  4843. assert(block[j]);
  4844. #ifdef REFINE_STATS
  4845. after_last++;
  4846. #endif
  4847. }else{
  4848. #ifdef REFINE_STATS
  4849. if(block[j]){
  4850. if(block[j] - best_change){
  4851. if(ABS(block[j]) > ABS(block[j] - best_change)){
  4852. raise++;
  4853. }else{
  4854. lower++;
  4855. }
  4856. }else{
  4857. from_zero++;
  4858. }
  4859. }else{
  4860. to_zero++;
  4861. }
  4862. #endif
  4863. for(; last_non_zero>=start_i; last_non_zero--){
  4864. if(block[perm_scantable[last_non_zero]])
  4865. break;
  4866. }
  4867. }
  4868. #ifdef REFINE_STATS
  4869. count++;
  4870. if(256*256*256*64 % count == 0){
  4871. printf("after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);
  4872. }
  4873. #endif
  4874. run=0;
  4875. rle_index=0;
  4876. for(i=start_i; i<=last_non_zero; i++){
  4877. int j= perm_scantable[i];
  4878. const int level= block[j];
  4879. if(level){
  4880. run_tab[rle_index++]=run;
  4881. run=0;
  4882. }else{
  4883. run++;
  4884. }
  4885. }
  4886. s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
  4887. }else{
  4888. break;
  4889. }
  4890. }
  4891. #ifdef REFINE_STATS
  4892. if(last_non_zero>0){
  4893. STOP_TIMER("iterative search")
  4894. }
  4895. }
  4896. #endif
  4897. return last_non_zero;
  4898. }
  4899. static int dct_quantize_c(MpegEncContext *s,
  4900. DCTELEM *block, int n,
  4901. int qscale, int *overflow)
  4902. {
  4903. int i, j, level, last_non_zero, q, start_i;
  4904. const int *qmat;
  4905. const uint8_t *scantable= s->intra_scantable.scantable;
  4906. int bias;
  4907. int max=0;
  4908. unsigned int threshold1, threshold2;
  4909. s->dsp.fdct (block);
  4910. if(s->dct_error_sum)
  4911. s->denoise_dct(s, block);
  4912. if (s->mb_intra) {
  4913. if (!s->h263_aic) {
  4914. if (n < 4)
  4915. q = s->y_dc_scale;
  4916. else
  4917. q = s->c_dc_scale;
  4918. q = q << 3;
  4919. } else
  4920. /* For AIC we skip quant/dequant of INTRADC */
  4921. q = 1 << 3;
  4922. /* note: block[0] is assumed to be positive */
  4923. block[0] = (block[0] + (q >> 1)) / q;
  4924. start_i = 1;
  4925. last_non_zero = 0;
  4926. qmat = s->q_intra_matrix[qscale];
  4927. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  4928. } else {
  4929. start_i = 0;
  4930. last_non_zero = -1;
  4931. qmat = s->q_inter_matrix[qscale];
  4932. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  4933. }
  4934. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  4935. threshold2= (threshold1<<1);
  4936. for(i=63;i>=start_i;i--) {
  4937. j = scantable[i];
  4938. level = block[j] * qmat[j];
  4939. if(((unsigned)(level+threshold1))>threshold2){
  4940. last_non_zero = i;
  4941. break;
  4942. }else{
  4943. block[j]=0;
  4944. }
  4945. }
  4946. for(i=start_i; i<=last_non_zero; i++) {
  4947. j = scantable[i];
  4948. level = block[j] * qmat[j];
  4949. // if( bias+level >= (1<<QMAT_SHIFT)
  4950. // || bias-level >= (1<<QMAT_SHIFT)){
  4951. if(((unsigned)(level+threshold1))>threshold2){
  4952. if(level>0){
  4953. level= (bias + level)>>QMAT_SHIFT;
  4954. block[j]= level;
  4955. }else{
  4956. level= (bias - level)>>QMAT_SHIFT;
  4957. block[j]= -level;
  4958. }
  4959. max |=level;
  4960. }else{
  4961. block[j]=0;
  4962. }
  4963. }
  4964. *overflow= s->max_qcoeff < max; //overflow might have happend
  4965. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  4966. if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
  4967. ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
  4968. return last_non_zero;
  4969. }
  4970. #endif //CONFIG_ENCODERS
  4971. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  4972. DCTELEM *block, int n, int qscale)
  4973. {
  4974. int i, level, nCoeffs;
  4975. const uint16_t *quant_matrix;
  4976. nCoeffs= s->block_last_index[n];
  4977. if (n < 4)
  4978. block[0] = block[0] * s->y_dc_scale;
  4979. else
  4980. block[0] = block[0] * s->c_dc_scale;
  4981. /* XXX: only mpeg1 */
  4982. quant_matrix = s->intra_matrix;
  4983. for(i=1;i<=nCoeffs;i++) {
  4984. int j= s->intra_scantable.permutated[i];
  4985. level = block[j];
  4986. if (level) {
  4987. if (level < 0) {
  4988. level = -level;
  4989. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4990. level = (level - 1) | 1;
  4991. level = -level;
  4992. } else {
  4993. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4994. level = (level - 1) | 1;
  4995. }
  4996. block[j] = level;
  4997. }
  4998. }
  4999. }
  5000. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  5001. DCTELEM *block, int n, int qscale)
  5002. {
  5003. int i, level, nCoeffs;
  5004. const uint16_t *quant_matrix;
  5005. nCoeffs= s->block_last_index[n];
  5006. quant_matrix = s->inter_matrix;
  5007. for(i=0; i<=nCoeffs; i++) {
  5008. int j= s->intra_scantable.permutated[i];
  5009. level = block[j];
  5010. if (level) {
  5011. if (level < 0) {
  5012. level = -level;
  5013. level = (((level << 1) + 1) * qscale *
  5014. ((int) (quant_matrix[j]))) >> 4;
  5015. level = (level - 1) | 1;
  5016. level = -level;
  5017. } else {
  5018. level = (((level << 1) + 1) * qscale *
  5019. ((int) (quant_matrix[j]))) >> 4;
  5020. level = (level - 1) | 1;
  5021. }
  5022. block[j] = level;
  5023. }
  5024. }
  5025. }
  5026. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  5027. DCTELEM *block, int n, int qscale)
  5028. {
  5029. int i, level, nCoeffs;
  5030. const uint16_t *quant_matrix;
  5031. if(s->alternate_scan) nCoeffs= 63;
  5032. else nCoeffs= s->block_last_index[n];
  5033. if (n < 4)
  5034. block[0] = block[0] * s->y_dc_scale;
  5035. else
  5036. block[0] = block[0] * s->c_dc_scale;
  5037. quant_matrix = s->intra_matrix;
  5038. for(i=1;i<=nCoeffs;i++) {
  5039. int j= s->intra_scantable.permutated[i];
  5040. level = block[j];
  5041. if (level) {
  5042. if (level < 0) {
  5043. level = -level;
  5044. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  5045. level = -level;
  5046. } else {
  5047. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  5048. }
  5049. block[j] = level;
  5050. }
  5051. }
  5052. }
  5053. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  5054. DCTELEM *block, int n, int qscale)
  5055. {
  5056. int i, level, nCoeffs;
  5057. const uint16_t *quant_matrix;
  5058. int sum=-1;
  5059. if(s->alternate_scan) nCoeffs= 63;
  5060. else nCoeffs= s->block_last_index[n];
  5061. quant_matrix = s->inter_matrix;
  5062. for(i=0; i<=nCoeffs; i++) {
  5063. int j= s->intra_scantable.permutated[i];
  5064. level = block[j];
  5065. if (level) {
  5066. if (level < 0) {
  5067. level = -level;
  5068. level = (((level << 1) + 1) * qscale *
  5069. ((int) (quant_matrix[j]))) >> 4;
  5070. level = -level;
  5071. } else {
  5072. level = (((level << 1) + 1) * qscale *
  5073. ((int) (quant_matrix[j]))) >> 4;
  5074. }
  5075. block[j] = level;
  5076. sum+=level;
  5077. }
  5078. }
  5079. block[63]^=sum&1;
  5080. }
  5081. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  5082. DCTELEM *block, int n, int qscale)
  5083. {
  5084. int i, level, qmul, qadd;
  5085. int nCoeffs;
  5086. assert(s->block_last_index[n]>=0);
  5087. qmul = qscale << 1;
  5088. if (!s->h263_aic) {
  5089. if (n < 4)
  5090. block[0] = block[0] * s->y_dc_scale;
  5091. else
  5092. block[0] = block[0] * s->c_dc_scale;
  5093. qadd = (qscale - 1) | 1;
  5094. }else{
  5095. qadd = 0;
  5096. }
  5097. if(s->ac_pred)
  5098. nCoeffs=63;
  5099. else
  5100. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  5101. for(i=1; i<=nCoeffs; i++) {
  5102. level = block[i];
  5103. if (level) {
  5104. if (level < 0) {
  5105. level = level * qmul - qadd;
  5106. } else {
  5107. level = level * qmul + qadd;
  5108. }
  5109. block[i] = level;
  5110. }
  5111. }
  5112. }
  5113. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  5114. DCTELEM *block, int n, int qscale)
  5115. {
  5116. int i, level, qmul, qadd;
  5117. int nCoeffs;
  5118. assert(s->block_last_index[n]>=0);
  5119. qadd = (qscale - 1) | 1;
  5120. qmul = qscale << 1;
  5121. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  5122. for(i=0; i<=nCoeffs; i++) {
  5123. level = block[i];
  5124. if (level) {
  5125. if (level < 0) {
  5126. level = level * qmul - qadd;
  5127. } else {
  5128. level = level * qmul + qadd;
  5129. }
  5130. block[i] = level;
  5131. }
  5132. }
  5133. }
  5134. static void dct_unquantize_h261_intra_c(MpegEncContext *s,
  5135. DCTELEM *block, int n, int qscale)
  5136. {
  5137. int i, level, even;
  5138. int nCoeffs;
  5139. assert(s->block_last_index[n]>=0);
  5140. if (n < 4)
  5141. block[0] = block[0] * s->y_dc_scale;
  5142. else
  5143. block[0] = block[0] * s->c_dc_scale;
  5144. even = (qscale & 1)^1;
  5145. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  5146. for(i=1; i<=nCoeffs; i++){
  5147. level = block[i];
  5148. if (level){
  5149. if (level < 0){
  5150. level = qscale * ((level << 1) - 1) + even;
  5151. }else{
  5152. level = qscale * ((level << 1) + 1) - even;
  5153. }
  5154. }
  5155. block[i] = level;
  5156. }
  5157. }
  5158. static void dct_unquantize_h261_inter_c(MpegEncContext *s,
  5159. DCTELEM *block, int n, int qscale)
  5160. {
  5161. int i, level, even;
  5162. int nCoeffs;
  5163. assert(s->block_last_index[n]>=0);
  5164. even = (qscale & 1)^1;
  5165. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  5166. for(i=0; i<=nCoeffs; i++){
  5167. level = block[i];
  5168. if (level){
  5169. if (level < 0){
  5170. level = qscale * ((level << 1) - 1) + even;
  5171. }else{
  5172. level = qscale * ((level << 1) + 1) - even;
  5173. }
  5174. }
  5175. block[i] = level;
  5176. }
  5177. }
  5178. static const AVOption mpeg4_options[] =
  5179. {
  5180. AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
  5181. AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
  5182. "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
  5183. bit_rate_tolerance, 4, 240000000, 8000),
  5184. AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
  5185. AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
  5186. AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
  5187. rc_eq, "tex^qComp,option1,options2", 0),
  5188. AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
  5189. rc_min_rate, 4, 24000000, 0),
  5190. AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
  5191. rc_max_rate, 4, 24000000, 0),
  5192. AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
  5193. rc_buffer_aggressivity, 4, 24000000, 0),
  5194. AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
  5195. rc_initial_cplx, 0., 9999999., 0),
  5196. AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
  5197. i_quant_factor, 0., 0., 0),
  5198. AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
  5199. i_quant_factor, -999999., 999999., 0),
  5200. AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
  5201. dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
  5202. AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
  5203. lumi_masking, 0., 999999., 0),
  5204. AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
  5205. temporal_cplx_masking, 0., 999999., 0),
  5206. AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
  5207. spatial_cplx_masking, 0., 999999., 0),
  5208. AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
  5209. p_masking, 0., 999999., 0),
  5210. AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
  5211. dark_masking, 0., 999999., 0),
  5212. AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
  5213. idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
  5214. AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
  5215. mb_qmin, 0, 8, 0),
  5216. AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
  5217. mb_qmin, 0, 8, 0),
  5218. AVOPTION_CODEC_INT("me_cmp", "ME compare function",
  5219. me_cmp, 0, 24000000, 0),
  5220. AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
  5221. me_sub_cmp, 0, 24000000, 0),
  5222. AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
  5223. dia_size, 0, 24000000, 0),
  5224. AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
  5225. last_predictor_count, 0, 24000000, 0),
  5226. AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
  5227. pre_me, 0, 24000000, 0),
  5228. AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
  5229. me_pre_cmp, 0, 24000000, 0),
  5230. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  5231. me_range, 0, 24000000, 0),
  5232. AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
  5233. pre_dia_size, 0, 24000000, 0),
  5234. AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
  5235. me_subpel_quality, 0, 24000000, 0),
  5236. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  5237. me_range, 0, 24000000, 0),
  5238. AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
  5239. flags, CODEC_FLAG_PSNR, 0),
  5240. AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
  5241. rc_override),
  5242. AVOPTION_SUB(avoptions_common),
  5243. AVOPTION_END()
  5244. };
  5245. #ifdef CONFIG_ENCODERS
  5246. #ifdef CONFIG_RISKY
  5247. AVCodec h263_encoder = {
  5248. "h263",
  5249. CODEC_TYPE_VIDEO,
  5250. CODEC_ID_H263,
  5251. sizeof(MpegEncContext),
  5252. MPV_encode_init,
  5253. MPV_encode_picture,
  5254. MPV_encode_end,
  5255. };
  5256. AVCodec h263p_encoder = {
  5257. "h263p",
  5258. CODEC_TYPE_VIDEO,
  5259. CODEC_ID_H263P,
  5260. sizeof(MpegEncContext),
  5261. MPV_encode_init,
  5262. MPV_encode_picture,
  5263. MPV_encode_end,
  5264. };
  5265. AVCodec flv_encoder = {
  5266. "flv",
  5267. CODEC_TYPE_VIDEO,
  5268. CODEC_ID_FLV1,
  5269. sizeof(MpegEncContext),
  5270. MPV_encode_init,
  5271. MPV_encode_picture,
  5272. MPV_encode_end,
  5273. };
  5274. AVCodec rv10_encoder = {
  5275. "rv10",
  5276. CODEC_TYPE_VIDEO,
  5277. CODEC_ID_RV10,
  5278. sizeof(MpegEncContext),
  5279. MPV_encode_init,
  5280. MPV_encode_picture,
  5281. MPV_encode_end,
  5282. };
  5283. AVCodec mpeg4_encoder = {
  5284. "mpeg4",
  5285. CODEC_TYPE_VIDEO,
  5286. CODEC_ID_MPEG4,
  5287. sizeof(MpegEncContext),
  5288. MPV_encode_init,
  5289. MPV_encode_picture,
  5290. MPV_encode_end,
  5291. .options = mpeg4_options,
  5292. .capabilities= CODEC_CAP_DELAY,
  5293. };
  5294. AVCodec msmpeg4v1_encoder = {
  5295. "msmpeg4v1",
  5296. CODEC_TYPE_VIDEO,
  5297. CODEC_ID_MSMPEG4V1,
  5298. sizeof(MpegEncContext),
  5299. MPV_encode_init,
  5300. MPV_encode_picture,
  5301. MPV_encode_end,
  5302. .options = mpeg4_options,
  5303. };
  5304. AVCodec msmpeg4v2_encoder = {
  5305. "msmpeg4v2",
  5306. CODEC_TYPE_VIDEO,
  5307. CODEC_ID_MSMPEG4V2,
  5308. sizeof(MpegEncContext),
  5309. MPV_encode_init,
  5310. MPV_encode_picture,
  5311. MPV_encode_end,
  5312. .options = mpeg4_options,
  5313. };
  5314. AVCodec msmpeg4v3_encoder = {
  5315. "msmpeg4",
  5316. CODEC_TYPE_VIDEO,
  5317. CODEC_ID_MSMPEG4V3,
  5318. sizeof(MpegEncContext),
  5319. MPV_encode_init,
  5320. MPV_encode_picture,
  5321. MPV_encode_end,
  5322. .options = mpeg4_options,
  5323. };
  5324. AVCodec wmv1_encoder = {
  5325. "wmv1",
  5326. CODEC_TYPE_VIDEO,
  5327. CODEC_ID_WMV1,
  5328. sizeof(MpegEncContext),
  5329. MPV_encode_init,
  5330. MPV_encode_picture,
  5331. MPV_encode_end,
  5332. .options = mpeg4_options,
  5333. };
  5334. #endif
  5335. AVCodec mjpeg_encoder = {
  5336. "mjpeg",
  5337. CODEC_TYPE_VIDEO,
  5338. CODEC_ID_MJPEG,
  5339. sizeof(MpegEncContext),
  5340. MPV_encode_init,
  5341. MPV_encode_picture,
  5342. MPV_encode_end,
  5343. };
  5344. #endif //CONFIG_ENCODERS