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.

5403 lines
193KB

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