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.

5957 lines
211KB

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