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.

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