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.

2637 lines
101KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "libavutil/intmath.h"
  29. #include "libavutil/imgutils.h"
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "internal.h"
  33. #include "mpegvideo.h"
  34. #include "mpegvideo_common.h"
  35. #include "mjpegenc.h"
  36. #include "msmpeg4.h"
  37. #include "faandct.h"
  38. #include "xvmc_internal.h"
  39. #include "thread.h"
  40. #include <limits.h>
  41. //#undef NDEBUG
  42. //#include <assert.h>
  43. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  44. DCTELEM *block, int n, int qscale);
  45. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  46. DCTELEM *block, int n, int qscale);
  47. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  48. DCTELEM *block, int n, int qscale);
  49. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  50. DCTELEM *block, int n, int qscale);
  51. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  52. DCTELEM *block, int n, int qscale);
  53. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  54. DCTELEM *block, int n, int qscale);
  55. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  56. DCTELEM *block, int n, int qscale);
  57. /* enable all paranoid tests for rounding, overflows, etc... */
  58. //#define PARANOID
  59. //#define DEBUG
  60. static const uint8_t ff_default_chroma_qscale_table[32]={
  61. // 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
  62. 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
  63. };
  64. const uint8_t ff_mpeg1_dc_scale_table[128]={
  65. // 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
  66. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  67. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  68. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  69. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  70. };
  71. static const uint8_t mpeg2_dc_scale_table1[128]={
  72. // 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
  73. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  74. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  75. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  76. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  77. };
  78. static const uint8_t mpeg2_dc_scale_table2[128]={
  79. // 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
  80. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  81. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  82. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  83. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  84. };
  85. static const uint8_t mpeg2_dc_scale_table3[128]={
  86. // 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
  87. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  88. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  90. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  91. };
  92. const uint8_t * const ff_mpeg2_dc_scale_table[4]={
  93. ff_mpeg1_dc_scale_table,
  94. mpeg2_dc_scale_table1,
  95. mpeg2_dc_scale_table2,
  96. mpeg2_dc_scale_table3,
  97. };
  98. const enum PixelFormat ff_pixfmt_list_420[] = {
  99. PIX_FMT_YUV420P,
  100. PIX_FMT_NONE
  101. };
  102. const enum PixelFormat ff_hwaccel_pixfmt_list_420[] = {
  103. PIX_FMT_DXVA2_VLD,
  104. PIX_FMT_VAAPI_VLD,
  105. PIX_FMT_YUV420P,
  106. PIX_FMT_NONE
  107. };
  108. const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){
  109. int i;
  110. assert(p<=end);
  111. if(p>=end)
  112. return end;
  113. for(i=0; i<3; i++){
  114. uint32_t tmp= *state << 8;
  115. *state= tmp + *(p++);
  116. if(tmp == 0x100 || p==end)
  117. return p;
  118. }
  119. while(p<end){
  120. if (p[-1] > 1 ) p+= 3;
  121. else if(p[-2] ) p+= 2;
  122. else if(p[-3]|(p[-1]-1)) p++;
  123. else{
  124. p++;
  125. break;
  126. }
  127. }
  128. p= FFMIN(p, end)-4;
  129. *state= AV_RB32(p);
  130. return p+4;
  131. }
  132. /* init common dct for both encoder and decoder */
  133. av_cold int ff_dct_common_init(MpegEncContext *s)
  134. {
  135. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  136. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  137. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  138. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  139. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  140. if(s->flags & CODEC_FLAG_BITEXACT)
  141. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  142. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  143. #if HAVE_MMX
  144. MPV_common_init_mmx(s);
  145. #elif ARCH_ALPHA
  146. MPV_common_init_axp(s);
  147. #elif CONFIG_MLIB
  148. MPV_common_init_mlib(s);
  149. #elif HAVE_MMI
  150. MPV_common_init_mmi(s);
  151. #elif ARCH_ARM
  152. MPV_common_init_arm(s);
  153. #elif HAVE_ALTIVEC
  154. MPV_common_init_altivec(s);
  155. #elif ARCH_BFIN
  156. MPV_common_init_bfin(s);
  157. #endif
  158. /* load & permutate scantables
  159. note: only wmv uses different ones
  160. */
  161. if(s->alternate_scan){
  162. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  163. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  164. }else{
  165. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  166. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  167. }
  168. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  169. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  170. return 0;
  171. }
  172. void ff_copy_picture(Picture *dst, Picture *src){
  173. *dst = *src;
  174. dst->type= FF_BUFFER_TYPE_COPY;
  175. }
  176. /**
  177. * Release a frame buffer
  178. */
  179. static void free_frame_buffer(MpegEncContext *s, Picture *pic)
  180. {
  181. ff_thread_release_buffer(s->avctx, (AVFrame*)pic);
  182. av_freep(&pic->hwaccel_picture_private);
  183. }
  184. /**
  185. * Allocate a frame buffer
  186. */
  187. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  188. {
  189. int r;
  190. if (s->avctx->hwaccel) {
  191. assert(!pic->hwaccel_picture_private);
  192. if (s->avctx->hwaccel->priv_data_size) {
  193. pic->hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
  194. if (!pic->hwaccel_picture_private) {
  195. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  196. return -1;
  197. }
  198. }
  199. }
  200. r = ff_thread_get_buffer(s->avctx, (AVFrame*)pic);
  201. if (r<0 || !pic->age || !pic->type || !pic->data[0]) {
  202. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  203. av_freep(&pic->hwaccel_picture_private);
  204. return -1;
  205. }
  206. if (s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])) {
  207. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  208. free_frame_buffer(s, pic);
  209. return -1;
  210. }
  211. if (pic->linesize[1] != pic->linesize[2]) {
  212. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
  213. free_frame_buffer(s, pic);
  214. return -1;
  215. }
  216. return 0;
  217. }
  218. /**
  219. * allocates a Picture
  220. * The pixels are allocated/set by calling get_buffer() if shared=0
  221. */
  222. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  223. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11
  224. const int mb_array_size= s->mb_stride*s->mb_height;
  225. const int b8_array_size= s->b8_stride*s->mb_height*2;
  226. const int b4_array_size= s->b4_stride*s->mb_height*4;
  227. int i;
  228. int r= -1;
  229. if(shared){
  230. assert(pic->data[0]);
  231. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  232. pic->type= FF_BUFFER_TYPE_SHARED;
  233. }else{
  234. assert(!pic->data[0]);
  235. if (alloc_frame_buffer(s, pic) < 0)
  236. return -1;
  237. s->linesize = pic->linesize[0];
  238. s->uvlinesize= pic->linesize[1];
  239. }
  240. if(pic->qscale_table==NULL){
  241. if (s->encoding) {
  242. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_var , mb_array_size * sizeof(int16_t) , fail)
  243. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mc_mb_var, mb_array_size * sizeof(int16_t) , fail)
  244. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_mean , mb_array_size * sizeof(int8_t ) , fail)
  245. }
  246. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2, fail) //the +2 is for the slice end check
  247. FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table , mb_array_size * sizeof(uint8_t) , fail)
  248. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t), fail)
  249. pic->mb_type= pic->mb_type_base + 2*s->mb_stride+1;
  250. if(s->out_format == FMT_H264){
  251. for(i=0; i<2; i++){
  252. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t), fail)
  253. pic->motion_val[i]= pic->motion_val_base[i]+4;
  254. FF_ALLOCZ_OR_GOTO(s->avctx, pic->ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
  255. }
  256. pic->motion_subsample_log2= 2;
  257. }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
  258. for(i=0; i<2; i++){
  259. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t), fail)
  260. pic->motion_val[i]= pic->motion_val_base[i]+4;
  261. FF_ALLOCZ_OR_GOTO(s->avctx, pic->ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
  262. }
  263. pic->motion_subsample_log2= 3;
  264. }
  265. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  266. FF_ALLOCZ_OR_GOTO(s->avctx, pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6, fail)
  267. }
  268. pic->qstride= s->mb_stride;
  269. FF_ALLOCZ_OR_GOTO(s->avctx, pic->pan_scan , 1 * sizeof(AVPanScan), fail)
  270. }
  271. /* It might be nicer if the application would keep track of these
  272. * but it would require an API change. */
  273. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  274. s->prev_pict_types[0]= s->dropable ? AV_PICTURE_TYPE_B : s->pict_type;
  275. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == AV_PICTURE_TYPE_B)
  276. pic->age= INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2 and it is a bit tricky to skip them anyway.
  277. pic->owner2 = NULL;
  278. return 0;
  279. fail: //for the FF_ALLOCZ_OR_GOTO macro
  280. if(r>=0)
  281. free_frame_buffer(s, pic);
  282. return -1;
  283. }
  284. /**
  285. * deallocates a picture
  286. */
  287. static void free_picture(MpegEncContext *s, Picture *pic){
  288. int i;
  289. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  290. free_frame_buffer(s, pic);
  291. }
  292. av_freep(&pic->mb_var);
  293. av_freep(&pic->mc_mb_var);
  294. av_freep(&pic->mb_mean);
  295. av_freep(&pic->mbskip_table);
  296. av_freep(&pic->qscale_table);
  297. av_freep(&pic->mb_type_base);
  298. av_freep(&pic->dct_coeff);
  299. av_freep(&pic->pan_scan);
  300. pic->mb_type= NULL;
  301. for(i=0; i<2; i++){
  302. av_freep(&pic->motion_val_base[i]);
  303. av_freep(&pic->ref_index[i]);
  304. }
  305. if(pic->type == FF_BUFFER_TYPE_SHARED){
  306. for(i=0; i<4; i++){
  307. pic->base[i]=
  308. pic->data[i]= NULL;
  309. }
  310. pic->type= 0;
  311. }
  312. }
  313. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
  314. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  315. int c_size = s->mb_stride * (s->mb_height + 1);
  316. int yc_size = y_size + 2 * c_size;
  317. int i;
  318. // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
  319. FF_ALLOCZ_OR_GOTO(s->avctx, s->allocated_edge_emu_buffer, (s->width+64)*2*21*2, fail); //(width + edge + align)*interlaced*MBsize*tolerance
  320. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
  321. //FIXME should be linesize instead of s->width*2 but that is not known before get_buffer()
  322. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t), fail)
  323. s->me.temp= s->me.scratchpad;
  324. s->rd_scratchpad= s->me.scratchpad;
  325. s->b_scratchpad= s->me.scratchpad;
  326. s->obmc_scratchpad= s->me.scratchpad + 16;
  327. if (s->encoding) {
  328. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map , ME_MAP_SIZE*sizeof(uint32_t), fail)
  329. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t), fail)
  330. if(s->avctx->noise_reduction){
  331. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum, 2 * 64 * sizeof(int), fail)
  332. }
  333. }
  334. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64*12*2 * sizeof(DCTELEM), fail)
  335. s->block= s->blocks[0];
  336. for(i=0;i<12;i++){
  337. s->pblocks[i] = &s->block[i];
  338. }
  339. if (s->out_format == FMT_H263) {
  340. /* ac values */
  341. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base, yc_size * sizeof(int16_t) * 16, fail);
  342. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  343. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  344. s->ac_val[2] = s->ac_val[1] + c_size;
  345. }
  346. return 0;
  347. fail:
  348. return -1; //free() through MPV_common_end()
  349. }
  350. static void free_duplicate_context(MpegEncContext *s){
  351. if(s==NULL) return;
  352. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  353. av_freep(&s->me.scratchpad);
  354. s->me.temp=
  355. s->rd_scratchpad=
  356. s->b_scratchpad=
  357. s->obmc_scratchpad= NULL;
  358. av_freep(&s->dct_error_sum);
  359. av_freep(&s->me.map);
  360. av_freep(&s->me.score_map);
  361. av_freep(&s->blocks);
  362. av_freep(&s->ac_val_base);
  363. s->block= NULL;
  364. }
  365. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
  366. #define COPY(a) bak->a= src->a
  367. COPY(allocated_edge_emu_buffer);
  368. COPY(edge_emu_buffer);
  369. COPY(me.scratchpad);
  370. COPY(me.temp);
  371. COPY(rd_scratchpad);
  372. COPY(b_scratchpad);
  373. COPY(obmc_scratchpad);
  374. COPY(me.map);
  375. COPY(me.score_map);
  376. COPY(blocks);
  377. COPY(block);
  378. COPY(start_mb_y);
  379. COPY(end_mb_y);
  380. COPY(me.map_generation);
  381. COPY(pb);
  382. COPY(dct_error_sum);
  383. COPY(dct_count[0]);
  384. COPY(dct_count[1]);
  385. COPY(ac_val_base);
  386. COPY(ac_val[0]);
  387. COPY(ac_val[1]);
  388. COPY(ac_val[2]);
  389. #undef COPY
  390. }
  391. void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
  392. MpegEncContext bak;
  393. int i;
  394. //FIXME copy only needed parts
  395. //START_TIMER
  396. backup_duplicate_context(&bak, dst);
  397. memcpy(dst, src, sizeof(MpegEncContext));
  398. backup_duplicate_context(dst, &bak);
  399. for(i=0;i<12;i++){
  400. dst->pblocks[i] = &dst->block[i];
  401. }
  402. //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  403. }
  404. int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  405. {
  406. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  407. if(dst == src || !s1->context_initialized) return 0;
  408. //FIXME can parameters change on I-frames? in that case dst may need a reinit
  409. if(!s->context_initialized){
  410. memcpy(s, s1, sizeof(MpegEncContext));
  411. s->avctx = dst;
  412. s->picture_range_start += MAX_PICTURE_COUNT;
  413. s->picture_range_end += MAX_PICTURE_COUNT;
  414. s->bitstream_buffer = NULL;
  415. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  416. MPV_common_init(s);
  417. }
  418. s->avctx->coded_height = s1->avctx->coded_height;
  419. s->avctx->coded_width = s1->avctx->coded_width;
  420. s->avctx->width = s1->avctx->width;
  421. s->avctx->height = s1->avctx->height;
  422. s->coded_picture_number = s1->coded_picture_number;
  423. s->picture_number = s1->picture_number;
  424. s->input_picture_number = s1->input_picture_number;
  425. memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
  426. memcpy(&s->last_picture, &s1->last_picture, (char*)&s1->last_picture_ptr - (char*)&s1->last_picture);
  427. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  428. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  429. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  430. memcpy(s->prev_pict_types, s1->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  431. //Error/bug resilience
  432. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  433. s->workaround_bugs = s1->workaround_bugs;
  434. //MPEG4 timing info
  435. memcpy(&s->time_increment_bits, &s1->time_increment_bits, (char*)&s1->shape - (char*)&s1->time_increment_bits);
  436. //B-frame info
  437. s->max_b_frames = s1->max_b_frames;
  438. s->low_delay = s1->low_delay;
  439. s->dropable = s1->dropable;
  440. //DivX handling (doesn't work)
  441. s->divx_packed = s1->divx_packed;
  442. if(s1->bitstream_buffer){
  443. if (s1->bitstream_buffer_size + FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  444. av_fast_malloc(&s->bitstream_buffer, &s->allocated_bitstream_buffer_size, s1->allocated_bitstream_buffer_size);
  445. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  446. memcpy(s->bitstream_buffer, s1->bitstream_buffer, s1->bitstream_buffer_size);
  447. memset(s->bitstream_buffer+s->bitstream_buffer_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  448. }
  449. //MPEG2/interlacing info
  450. memcpy(&s->progressive_sequence, &s1->progressive_sequence, (char*)&s1->rtp_mode - (char*)&s1->progressive_sequence);
  451. if(!s1->first_field){
  452. s->last_pict_type= s1->pict_type;
  453. if (s1->current_picture_ptr) s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->quality;
  454. if(s1->pict_type!=FF_B_TYPE){
  455. s->last_non_b_pict_type= s1->pict_type;
  456. }
  457. }
  458. return 0;
  459. }
  460. /**
  461. * sets the given MpegEncContext to common defaults (same for encoding and decoding).
  462. * the changed fields will not depend upon the prior state of the MpegEncContext.
  463. */
  464. void MPV_common_defaults(MpegEncContext *s){
  465. s->y_dc_scale_table=
  466. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  467. s->chroma_qscale_table= ff_default_chroma_qscale_table;
  468. s->progressive_frame= 1;
  469. s->progressive_sequence= 1;
  470. s->picture_structure= PICT_FRAME;
  471. s->coded_picture_number = 0;
  472. s->picture_number = 0;
  473. s->input_picture_number = 0;
  474. s->picture_in_gop_number = 0;
  475. s->f_code = 1;
  476. s->b_code = 1;
  477. s->picture_range_start = 0;
  478. s->picture_range_end = MAX_PICTURE_COUNT;
  479. }
  480. /**
  481. * sets the given MpegEncContext to defaults for decoding.
  482. * the changed fields will not depend upon the prior state of the MpegEncContext.
  483. */
  484. void MPV_decode_defaults(MpegEncContext *s){
  485. MPV_common_defaults(s);
  486. }
  487. /**
  488. * init common structure for both encoder and decoder.
  489. * this assumes that some variables like width/height are already set
  490. */
  491. av_cold int MPV_common_init(MpegEncContext *s)
  492. {
  493. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads;
  494. if(s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  495. s->mb_height = (s->height + 31) / 32 * 2;
  496. else if (s->codec_id != CODEC_ID_H264)
  497. s->mb_height = (s->height + 15) / 16;
  498. if(s->avctx->pix_fmt == PIX_FMT_NONE){
  499. av_log(s->avctx, AV_LOG_ERROR, "decoding to PIX_FMT_NONE is not supported.\n");
  500. return -1;
  501. }
  502. if((s->encoding || (s->avctx->active_thread_type & FF_THREAD_SLICE)) &&
  503. (s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height))){
  504. av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
  505. return -1;
  506. }
  507. if((s->width || s->height) && av_image_check_size(s->width, s->height, 0, s->avctx))
  508. return -1;
  509. dsputil_init(&s->dsp, s->avctx);
  510. ff_dct_common_init(s);
  511. s->flags= s->avctx->flags;
  512. s->flags2= s->avctx->flags2;
  513. if (s->width && s->height) {
  514. s->mb_width = (s->width + 15) / 16;
  515. s->mb_stride = s->mb_width + 1;
  516. s->b8_stride = s->mb_width*2 + 1;
  517. s->b4_stride = s->mb_width*4 + 1;
  518. mb_array_size= s->mb_height * s->mb_stride;
  519. mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
  520. /* set chroma shifts */
  521. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
  522. &(s->chroma_y_shift) );
  523. /* set default edge pos, will be overriden in decode_header if needed */
  524. s->h_edge_pos= s->mb_width*16;
  525. s->v_edge_pos= s->mb_height*16;
  526. s->mb_num = s->mb_width * s->mb_height;
  527. s->block_wrap[0]=
  528. s->block_wrap[1]=
  529. s->block_wrap[2]=
  530. s->block_wrap[3]= s->b8_stride;
  531. s->block_wrap[4]=
  532. s->block_wrap[5]= s->mb_stride;
  533. y_size = s->b8_stride * (2 * s->mb_height + 1);
  534. c_size = s->mb_stride * (s->mb_height + 1);
  535. yc_size = y_size + 2 * c_size;
  536. /* convert fourcc to upper case */
  537. s->codec_tag = ff_toupper4(s->avctx->codec_tag);
  538. s->stream_codec_tag = ff_toupper4(s->avctx->stream_codec_tag);
  539. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  540. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num+1)*sizeof(int), fail) //error ressilience code looks cleaner with this
  541. for(y=0; y<s->mb_height; y++){
  542. for(x=0; x<s->mb_width; x++){
  543. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  544. }
  545. }
  546. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  547. if (s->encoding) {
  548. /* Allocate MV tables */
  549. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  550. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  551. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  552. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  553. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  554. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  555. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  556. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  557. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  558. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  559. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  560. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  561. if(s->msmpeg4_version){
  562. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int), fail);
  563. }
  564. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  565. /* Allocate MB type table */
  566. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type , mb_array_size * sizeof(uint16_t), fail) //needed for encoding
  567. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  568. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix , 64*32 * sizeof(int), fail)
  569. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix , 64*32 * sizeof(int), fail)
  570. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  571. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  572. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  573. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  574. if(s->avctx->noise_reduction){
  575. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail)
  576. }
  577. }
  578. }
  579. s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
  580. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, s->picture_count * sizeof(Picture), fail)
  581. for(i = 0; i < s->picture_count; i++) {
  582. avcodec_get_frame_defaults((AVFrame *)&s->picture[i]);
  583. }
  584. if (s->width && s->height) {
  585. FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table, mb_array_size*sizeof(uint8_t), fail)
  586. if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
  587. /* interlaced direct mode decoding tables */
  588. for(i=0; i<2; i++){
  589. int j, k;
  590. for(j=0; j<2; j++){
  591. for(k=0; k<2; k++){
  592. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_mv_table_base[i][j][k], mv_table_size * 2 * sizeof(int16_t), fail)
  593. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  594. }
  595. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  596. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  597. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]+ s->mb_stride + 1;
  598. }
  599. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  600. }
  601. }
  602. if (s->out_format == FMT_H263) {
  603. /* cbp values */
  604. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  605. s->coded_block= s->coded_block_base + s->b8_stride + 1;
  606. /* cbp, ac_pred, pred_dir */
  607. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail)
  608. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail)
  609. }
  610. if (s->h263_pred || s->h263_plus || !s->encoding) {
  611. /* dc values */
  612. //MN: we need these for error resilience of intra-frames
  613. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  614. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  615. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  616. s->dc_val[2] = s->dc_val[1] + c_size;
  617. for(i=0;i<yc_size;i++)
  618. s->dc_val_base[i] = 1024;
  619. }
  620. /* which mb is a intra block */
  621. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  622. memset(s->mbintra_table, 1, mb_array_size);
  623. /* init macroblock skip table */
  624. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size+2, fail);
  625. //Note the +1 is for a quicker mpeg4 slice_end detection
  626. FF_ALLOCZ_OR_GOTO(s->avctx, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE, fail);
  627. s->parse_context.state= -1;
  628. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  629. s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  630. s->visualization_buffer[1] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  631. s->visualization_buffer[2] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  632. }
  633. }
  634. s->context_initialized = 1;
  635. s->thread_context[0]= s;
  636. if (s->width && s->height) {
  637. if (s->encoding || (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  638. threads = s->avctx->thread_count;
  639. for(i=1; i<threads; i++){
  640. s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
  641. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  642. }
  643. for(i=0; i<threads; i++){
  644. if(init_duplicate_context(s->thread_context[i], s) < 0)
  645. goto fail;
  646. s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
  647. s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
  648. }
  649. } else {
  650. if(init_duplicate_context(s, s) < 0) goto fail;
  651. s->start_mb_y = 0;
  652. s->end_mb_y = s->mb_height;
  653. }
  654. }
  655. return 0;
  656. fail:
  657. MPV_common_end(s);
  658. return -1;
  659. }
  660. /* init common structure for both encoder and decoder */
  661. void MPV_common_end(MpegEncContext *s)
  662. {
  663. int i, j, k;
  664. if (s->encoding || (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  665. for(i=0; i<s->avctx->thread_count; i++){
  666. free_duplicate_context(s->thread_context[i]);
  667. }
  668. for(i=1; i<s->avctx->thread_count; i++){
  669. av_freep(&s->thread_context[i]);
  670. }
  671. } else free_duplicate_context(s);
  672. av_freep(&s->parse_context.buffer);
  673. s->parse_context.buffer_size=0;
  674. av_freep(&s->mb_type);
  675. av_freep(&s->p_mv_table_base);
  676. av_freep(&s->b_forw_mv_table_base);
  677. av_freep(&s->b_back_mv_table_base);
  678. av_freep(&s->b_bidir_forw_mv_table_base);
  679. av_freep(&s->b_bidir_back_mv_table_base);
  680. av_freep(&s->b_direct_mv_table_base);
  681. s->p_mv_table= NULL;
  682. s->b_forw_mv_table= NULL;
  683. s->b_back_mv_table= NULL;
  684. s->b_bidir_forw_mv_table= NULL;
  685. s->b_bidir_back_mv_table= NULL;
  686. s->b_direct_mv_table= NULL;
  687. for(i=0; i<2; i++){
  688. for(j=0; j<2; j++){
  689. for(k=0; k<2; k++){
  690. av_freep(&s->b_field_mv_table_base[i][j][k]);
  691. s->b_field_mv_table[i][j][k]=NULL;
  692. }
  693. av_freep(&s->b_field_select_table[i][j]);
  694. av_freep(&s->p_field_mv_table_base[i][j]);
  695. s->p_field_mv_table[i][j]=NULL;
  696. }
  697. av_freep(&s->p_field_select_table[i]);
  698. }
  699. av_freep(&s->dc_val_base);
  700. av_freep(&s->coded_block_base);
  701. av_freep(&s->mbintra_table);
  702. av_freep(&s->cbp_table);
  703. av_freep(&s->pred_dir_table);
  704. av_freep(&s->mbskip_table);
  705. av_freep(&s->prev_pict_types);
  706. av_freep(&s->bitstream_buffer);
  707. s->allocated_bitstream_buffer_size=0;
  708. av_freep(&s->avctx->stats_out);
  709. av_freep(&s->ac_stats);
  710. av_freep(&s->error_status_table);
  711. av_freep(&s->mb_index2xy);
  712. av_freep(&s->lambda_table);
  713. av_freep(&s->q_intra_matrix);
  714. av_freep(&s->q_inter_matrix);
  715. av_freep(&s->q_intra_matrix16);
  716. av_freep(&s->q_inter_matrix16);
  717. av_freep(&s->input_picture);
  718. av_freep(&s->reordered_input_picture);
  719. av_freep(&s->dct_offset);
  720. if(s->picture && !s->avctx->is_copy){
  721. for(i=0; i<s->picture_count; i++){
  722. free_picture(s, &s->picture[i]);
  723. }
  724. }
  725. av_freep(&s->picture);
  726. s->context_initialized = 0;
  727. s->last_picture_ptr=
  728. s->next_picture_ptr=
  729. s->current_picture_ptr= NULL;
  730. s->linesize= s->uvlinesize= 0;
  731. for(i=0; i<3; i++)
  732. av_freep(&s->visualization_buffer[i]);
  733. if(!(s->avctx->active_thread_type&FF_THREAD_FRAME))
  734. avcodec_default_free_buffers(s->avctx);
  735. }
  736. void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
  737. {
  738. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  739. uint8_t index_run[MAX_RUN+1];
  740. int last, run, level, start, end, i;
  741. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  742. if(static_store && rl->max_level[0])
  743. return;
  744. /* compute max_level[], max_run[] and index_run[] */
  745. for(last=0;last<2;last++) {
  746. if (last == 0) {
  747. start = 0;
  748. end = rl->last;
  749. } else {
  750. start = rl->last;
  751. end = rl->n;
  752. }
  753. memset(max_level, 0, MAX_RUN + 1);
  754. memset(max_run, 0, MAX_LEVEL + 1);
  755. memset(index_run, rl->n, MAX_RUN + 1);
  756. for(i=start;i<end;i++) {
  757. run = rl->table_run[i];
  758. level = rl->table_level[i];
  759. if (index_run[run] == rl->n)
  760. index_run[run] = i;
  761. if (level > max_level[run])
  762. max_level[run] = level;
  763. if (run > max_run[level])
  764. max_run[level] = run;
  765. }
  766. if(static_store)
  767. rl->max_level[last] = static_store[last];
  768. else
  769. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  770. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  771. if(static_store)
  772. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  773. else
  774. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  775. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  776. if(static_store)
  777. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  778. else
  779. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  780. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  781. }
  782. }
  783. void init_vlc_rl(RLTable *rl)
  784. {
  785. int i, q;
  786. for(q=0; q<32; q++){
  787. int qmul= q*2;
  788. int qadd= (q-1)|1;
  789. if(q==0){
  790. qmul=1;
  791. qadd=0;
  792. }
  793. for(i=0; i<rl->vlc.table_size; i++){
  794. int code= rl->vlc.table[i][0];
  795. int len = rl->vlc.table[i][1];
  796. int level, run;
  797. if(len==0){ // illegal code
  798. run= 66;
  799. level= MAX_LEVEL;
  800. }else if(len<0){ //more bits needed
  801. run= 0;
  802. level= code;
  803. }else{
  804. if(code==rl->n){ //esc
  805. run= 66;
  806. level= 0;
  807. }else{
  808. run= rl->table_run [code] + 1;
  809. level= rl->table_level[code] * qmul + qadd;
  810. if(code >= rl->last) run+=192;
  811. }
  812. }
  813. rl->rl_vlc[q][i].len= len;
  814. rl->rl_vlc[q][i].level= level;
  815. rl->rl_vlc[q][i].run= run;
  816. }
  817. }
  818. }
  819. void ff_release_unused_pictures(MpegEncContext *s, int remove_current)
  820. {
  821. int i;
  822. /* release non reference frames */
  823. for(i=0; i<s->picture_count; i++){
  824. if(s->picture[i].data[0] && !s->picture[i].reference
  825. && (!s->picture[i].owner2 || s->picture[i].owner2 == s)
  826. && (remove_current || &s->picture[i] != s->current_picture_ptr)
  827. /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  828. free_frame_buffer(s, &s->picture[i]);
  829. }
  830. }
  831. }
  832. int ff_find_unused_picture(MpegEncContext *s, int shared){
  833. int i;
  834. if(shared){
  835. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  836. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
  837. }
  838. }else{
  839. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  840. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
  841. }
  842. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  843. if(s->picture[i].data[0]==NULL) return i;
  844. }
  845. }
  846. av_log(s->avctx, AV_LOG_FATAL, "Internal error, picture buffer overflow\n");
  847. /* We could return -1, but the codec would crash trying to draw into a
  848. * non-existing frame anyway. This is safer than waiting for a random crash.
  849. * Also the return of this is never useful, an encoder must only allocate
  850. * as much as allowed in the specification. This has no relationship to how
  851. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  852. * enough for such valid streams).
  853. * Plus, a decoder has to check stream validity and remove frames if too
  854. * many reference frames are around. Waiting for "OOM" is not correct at
  855. * all. Similarly, missing reference frames have to be replaced by
  856. * interpolated/MC frames, anything else is a bug in the codec ...
  857. */
  858. abort();
  859. return -1;
  860. }
  861. static void update_noise_reduction(MpegEncContext *s){
  862. int intra, i;
  863. for(intra=0; intra<2; intra++){
  864. if(s->dct_count[intra] > (1<<16)){
  865. for(i=0; i<64; i++){
  866. s->dct_error_sum[intra][i] >>=1;
  867. }
  868. s->dct_count[intra] >>= 1;
  869. }
  870. for(i=0; i<64; i++){
  871. 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);
  872. }
  873. }
  874. }
  875. /**
  876. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  877. */
  878. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  879. {
  880. int i;
  881. Picture *pic;
  882. s->mb_skipped = 0;
  883. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  884. /* mark&release old frames */
  885. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
  886. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  887. free_frame_buffer(s, s->last_picture_ptr);
  888. /* release forgotten pictures */
  889. /* if(mpeg124/h263) */
  890. if(!s->encoding){
  891. for(i=0; i<s->picture_count; i++){
  892. if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
  893. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  894. free_frame_buffer(s, &s->picture[i]);
  895. }
  896. }
  897. }
  898. }
  899. }
  900. if(!s->encoding){
  901. ff_release_unused_pictures(s, 1);
  902. if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
  903. pic= s->current_picture_ptr; //we already have a unused image (maybe it was set before reading the header)
  904. else{
  905. i= ff_find_unused_picture(s, 0);
  906. pic= &s->picture[i];
  907. }
  908. pic->reference= 0;
  909. if (!s->dropable){
  910. if (s->codec_id == CODEC_ID_H264)
  911. pic->reference = s->picture_structure;
  912. else if (s->pict_type != AV_PICTURE_TYPE_B)
  913. pic->reference = 3;
  914. }
  915. pic->coded_picture_number= s->coded_picture_number++;
  916. if(ff_alloc_picture(s, pic, 0) < 0)
  917. return -1;
  918. s->current_picture_ptr= pic;
  919. //FIXME use only the vars from current_pic
  920. s->current_picture_ptr->top_field_first= s->top_field_first;
  921. if(s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO) {
  922. if(s->picture_structure != PICT_FRAME)
  923. s->current_picture_ptr->top_field_first= (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  924. }
  925. s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
  926. s->current_picture_ptr->field_picture= s->picture_structure != PICT_FRAME;
  927. }
  928. s->current_picture_ptr->pict_type= s->pict_type;
  929. // if(s->flags && CODEC_FLAG_QSCALE)
  930. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  931. s->current_picture_ptr->key_frame= s->pict_type == AV_PICTURE_TYPE_I;
  932. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  933. if (s->pict_type != AV_PICTURE_TYPE_B) {
  934. s->last_picture_ptr= s->next_picture_ptr;
  935. if(!s->dropable)
  936. s->next_picture_ptr= s->current_picture_ptr;
  937. }
  938. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  939. s->last_picture_ptr ? s->last_picture_ptr->data[0] : NULL,
  940. s->next_picture_ptr ? s->next_picture_ptr->data[0] : NULL,
  941. s->current_picture_ptr ? s->current_picture_ptr->data[0] : NULL,
  942. s->pict_type, s->dropable);*/
  943. if(s->codec_id != CODEC_ID_H264){
  944. if((s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL) &&
  945. (s->pict_type!=AV_PICTURE_TYPE_I || s->picture_structure != PICT_FRAME)){
  946. if (s->pict_type != AV_PICTURE_TYPE_I)
  947. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  948. else if (s->picture_structure != PICT_FRAME)
  949. av_log(avctx, AV_LOG_INFO, "allocate dummy last picture for field based first keyframe\n");
  950. /* Allocate a dummy frame */
  951. i= ff_find_unused_picture(s, 0);
  952. s->last_picture_ptr= &s->picture[i];
  953. if(ff_alloc_picture(s, s->last_picture_ptr, 0) < 0)
  954. return -1;
  955. ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 0);
  956. ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 1);
  957. }
  958. if((s->next_picture_ptr==NULL || s->next_picture_ptr->data[0]==NULL) && s->pict_type==AV_PICTURE_TYPE_B){
  959. /* Allocate a dummy frame */
  960. i= ff_find_unused_picture(s, 0);
  961. s->next_picture_ptr= &s->picture[i];
  962. if(ff_alloc_picture(s, s->next_picture_ptr, 0) < 0)
  963. return -1;
  964. ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 0);
  965. ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 1);
  966. }
  967. }
  968. if(s->last_picture_ptr) ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  969. if(s->next_picture_ptr) ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  970. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  971. if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
  972. int i;
  973. for(i=0; i<4; i++){
  974. if(s->picture_structure == PICT_BOTTOM_FIELD){
  975. s->current_picture.data[i] += s->current_picture.linesize[i];
  976. }
  977. s->current_picture.linesize[i] *= 2;
  978. s->last_picture.linesize[i] *=2;
  979. s->next_picture.linesize[i] *=2;
  980. }
  981. }
  982. s->error_recognition= avctx->error_recognition;
  983. /* set dequantizer, we can't do it during init as it might change for mpeg4
  984. and we can't do it in the header decode as init is not called for mpeg4 there yet */
  985. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  986. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  987. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  988. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  989. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  990. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  991. }else{
  992. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  993. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  994. }
  995. if(s->dct_error_sum){
  996. assert(s->avctx->noise_reduction && s->encoding);
  997. update_noise_reduction(s);
  998. }
  999. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1000. return ff_xvmc_field_start(s, avctx);
  1001. return 0;
  1002. }
  1003. /* generic function for encode/decode called after a frame has been coded/decoded */
  1004. void MPV_frame_end(MpegEncContext *s)
  1005. {
  1006. int i;
  1007. /* redraw edges for the frame if decoding didn't complete */
  1008. //just to make sure that all data is rendered.
  1009. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1010. ff_xvmc_field_end(s);
  1011. }else if((s->error_count || s->encoding)
  1012. && !s->avctx->hwaccel
  1013. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1014. && s->unrestricted_mv
  1015. && s->current_picture.reference
  1016. && !s->intra_only
  1017. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  1018. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  1019. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  1020. s->dsp.draw_edges(s->current_picture.data[0], s->linesize ,
  1021. s->h_edge_pos , s->v_edge_pos,
  1022. EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
  1023. s->dsp.draw_edges(s->current_picture.data[1], s->uvlinesize,
  1024. s->h_edge_pos>>hshift, s->v_edge_pos>>vshift,
  1025. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, EDGE_TOP | EDGE_BOTTOM);
  1026. s->dsp.draw_edges(s->current_picture.data[2], s->uvlinesize,
  1027. s->h_edge_pos>>hshift, s->v_edge_pos>>vshift,
  1028. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, EDGE_TOP | EDGE_BOTTOM);
  1029. }
  1030. emms_c();
  1031. s->last_pict_type = s->pict_type;
  1032. s->last_lambda_for[s->pict_type]= s->current_picture_ptr->quality;
  1033. if(s->pict_type!=AV_PICTURE_TYPE_B){
  1034. s->last_non_b_pict_type= s->pict_type;
  1035. }
  1036. #if 0
  1037. /* copy back current_picture variables */
  1038. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1039. if(s->picture[i].data[0] == s->current_picture.data[0]){
  1040. s->picture[i]= s->current_picture;
  1041. break;
  1042. }
  1043. }
  1044. assert(i<MAX_PICTURE_COUNT);
  1045. #endif
  1046. if(s->encoding){
  1047. /* release non-reference frames */
  1048. for(i=0; i<s->picture_count; i++){
  1049. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  1050. free_frame_buffer(s, &s->picture[i]);
  1051. }
  1052. }
  1053. }
  1054. // clear copies, to avoid confusion
  1055. #if 0
  1056. memset(&s->last_picture, 0, sizeof(Picture));
  1057. memset(&s->next_picture, 0, sizeof(Picture));
  1058. memset(&s->current_picture, 0, sizeof(Picture));
  1059. #endif
  1060. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  1061. if (s->codec_id != CODEC_ID_H264 && s->current_picture.reference) {
  1062. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_height-1, 0);
  1063. }
  1064. }
  1065. /**
  1066. * draws an line from (ex, ey) -> (sx, sy).
  1067. * @param w width of the image
  1068. * @param h height of the image
  1069. * @param stride stride/linesize of the image
  1070. * @param color color of the arrow
  1071. */
  1072. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1073. int x, y, fr, f;
  1074. sx= av_clip(sx, 0, w-1);
  1075. sy= av_clip(sy, 0, h-1);
  1076. ex= av_clip(ex, 0, w-1);
  1077. ey= av_clip(ey, 0, h-1);
  1078. buf[sy*stride + sx]+= color;
  1079. if(FFABS(ex - sx) > FFABS(ey - sy)){
  1080. if(sx > ex){
  1081. FFSWAP(int, sx, ex);
  1082. FFSWAP(int, sy, ey);
  1083. }
  1084. buf+= sx + sy*stride;
  1085. ex-= sx;
  1086. f= ((ey-sy)<<16)/ex;
  1087. for(x= 0; x <= ex; x++){
  1088. y = (x*f)>>16;
  1089. fr= (x*f)&0xFFFF;
  1090. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  1091. buf[(y+1)*stride + x]+= (color* fr )>>16;
  1092. }
  1093. }else{
  1094. if(sy > ey){
  1095. FFSWAP(int, sx, ex);
  1096. FFSWAP(int, sy, ey);
  1097. }
  1098. buf+= sx + sy*stride;
  1099. ey-= sy;
  1100. if(ey) f= ((ex-sx)<<16)/ey;
  1101. else f= 0;
  1102. for(y= 0; y <= ey; y++){
  1103. x = (y*f)>>16;
  1104. fr= (y*f)&0xFFFF;
  1105. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;
  1106. buf[y*stride + x+1]+= (color* fr )>>16;
  1107. }
  1108. }
  1109. }
  1110. /**
  1111. * draws an arrow from (ex, ey) -> (sx, sy).
  1112. * @param w width of the image
  1113. * @param h height of the image
  1114. * @param stride stride/linesize of the image
  1115. * @param color color of the arrow
  1116. */
  1117. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1118. int dx,dy;
  1119. sx= av_clip(sx, -100, w+100);
  1120. sy= av_clip(sy, -100, h+100);
  1121. ex= av_clip(ex, -100, w+100);
  1122. ey= av_clip(ey, -100, h+100);
  1123. dx= ex - sx;
  1124. dy= ey - sy;
  1125. if(dx*dx + dy*dy > 3*3){
  1126. int rx= dx + dy;
  1127. int ry= -dx + dy;
  1128. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  1129. //FIXME subpixel accuracy
  1130. rx= ROUNDED_DIV(rx*3<<4, length);
  1131. ry= ROUNDED_DIV(ry*3<<4, length);
  1132. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1133. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1134. }
  1135. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1136. }
  1137. /**
  1138. * prints debuging info for the given picture.
  1139. */
  1140. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  1141. if(s->avctx->hwaccel || !pict || !pict->mb_type) return;
  1142. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  1143. int x,y;
  1144. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1145. switch (pict->pict_type) {
  1146. case AV_PICTURE_TYPE_I: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
  1147. case AV_PICTURE_TYPE_P: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
  1148. case AV_PICTURE_TYPE_B: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
  1149. case AV_PICTURE_TYPE_S: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
  1150. case AV_PICTURE_TYPE_SI: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
  1151. case AV_PICTURE_TYPE_SP: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
  1152. }
  1153. for(y=0; y<s->mb_height; y++){
  1154. for(x=0; x<s->mb_width; x++){
  1155. if(s->avctx->debug&FF_DEBUG_SKIP){
  1156. int count= s->mbskip_table[x + y*s->mb_stride];
  1157. if(count>9) count=9;
  1158. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1159. }
  1160. if(s->avctx->debug&FF_DEBUG_QP){
  1161. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1162. }
  1163. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1164. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1165. //Type & MV direction
  1166. if(IS_PCM(mb_type))
  1167. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1168. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1169. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1170. else if(IS_INTRA4x4(mb_type))
  1171. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1172. else if(IS_INTRA16x16(mb_type))
  1173. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1174. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1175. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1176. else if(IS_DIRECT(mb_type))
  1177. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1178. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1179. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1180. else if(IS_GMC(mb_type))
  1181. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1182. else if(IS_SKIP(mb_type))
  1183. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1184. else if(!USES_LIST(mb_type, 1))
  1185. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1186. else if(!USES_LIST(mb_type, 0))
  1187. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1188. else{
  1189. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1190. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1191. }
  1192. //segmentation
  1193. if(IS_8X8(mb_type))
  1194. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1195. else if(IS_16X8(mb_type))
  1196. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1197. else if(IS_8X16(mb_type))
  1198. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1199. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1200. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1201. else
  1202. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1203. if(IS_INTERLACED(mb_type))
  1204. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1205. else
  1206. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1207. }
  1208. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1209. }
  1210. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1211. }
  1212. }
  1213. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1214. const int shift= 1 + s->quarter_sample;
  1215. int mb_y;
  1216. uint8_t *ptr;
  1217. int i;
  1218. int h_chroma_shift, v_chroma_shift, block_height;
  1219. const int width = s->avctx->width;
  1220. const int height= s->avctx->height;
  1221. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1222. const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1223. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1224. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1225. for(i=0; i<3; i++){
  1226. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1227. pict->data[i]= s->visualization_buffer[i];
  1228. }
  1229. pict->type= FF_BUFFER_TYPE_COPY;
  1230. ptr= pict->data[0];
  1231. block_height = 16>>v_chroma_shift;
  1232. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1233. int mb_x;
  1234. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1235. const int mb_index= mb_x + mb_y*s->mb_stride;
  1236. if((s->avctx->debug_mv) && pict->motion_val){
  1237. int type;
  1238. for(type=0; type<3; type++){
  1239. int direction = 0;
  1240. switch (type) {
  1241. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=AV_PICTURE_TYPE_P))
  1242. continue;
  1243. direction = 0;
  1244. break;
  1245. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=AV_PICTURE_TYPE_B))
  1246. continue;
  1247. direction = 0;
  1248. break;
  1249. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=AV_PICTURE_TYPE_B))
  1250. continue;
  1251. direction = 1;
  1252. break;
  1253. }
  1254. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1255. continue;
  1256. if(IS_8X8(pict->mb_type[mb_index])){
  1257. int i;
  1258. for(i=0; i<4; i++){
  1259. int sx= mb_x*16 + 4 + 8*(i&1);
  1260. int sy= mb_y*16 + 4 + 8*(i>>1);
  1261. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1262. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1263. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1264. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1265. }
  1266. }else if(IS_16X8(pict->mb_type[mb_index])){
  1267. int i;
  1268. for(i=0; i<2; i++){
  1269. int sx=mb_x*16 + 8;
  1270. int sy=mb_y*16 + 4 + 8*i;
  1271. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
  1272. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1273. int my=(pict->motion_val[direction][xy][1]>>shift);
  1274. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1275. my*=2;
  1276. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1277. }
  1278. }else if(IS_8X16(pict->mb_type[mb_index])){
  1279. int i;
  1280. for(i=0; i<2; i++){
  1281. int sx=mb_x*16 + 4 + 8*i;
  1282. int sy=mb_y*16 + 8;
  1283. int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
  1284. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1285. int my=(pict->motion_val[direction][xy][1]>>shift);
  1286. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1287. my*=2;
  1288. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1289. }
  1290. }else{
  1291. int sx= mb_x*16 + 8;
  1292. int sy= mb_y*16 + 8;
  1293. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1294. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1295. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1296. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1297. }
  1298. }
  1299. }
  1300. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1301. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1302. int y;
  1303. for(y=0; y<block_height; y++){
  1304. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= c;
  1305. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= c;
  1306. }
  1307. }
  1308. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1309. int mb_type= pict->mb_type[mb_index];
  1310. uint64_t u,v;
  1311. int y;
  1312. #define COLOR(theta, r)\
  1313. u= (int)(128 + r*cos(theta*3.141592/180));\
  1314. v= (int)(128 + r*sin(theta*3.141592/180));
  1315. u=v=128;
  1316. if(IS_PCM(mb_type)){
  1317. COLOR(120,48)
  1318. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1319. COLOR(30,48)
  1320. }else if(IS_INTRA4x4(mb_type)){
  1321. COLOR(90,48)
  1322. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1323. // COLOR(120,48)
  1324. }else if(IS_DIRECT(mb_type)){
  1325. COLOR(150,48)
  1326. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1327. COLOR(170,48)
  1328. }else if(IS_GMC(mb_type)){
  1329. COLOR(190,48)
  1330. }else if(IS_SKIP(mb_type)){
  1331. // COLOR(180,48)
  1332. }else if(!USES_LIST(mb_type, 1)){
  1333. COLOR(240,48)
  1334. }else if(!USES_LIST(mb_type, 0)){
  1335. COLOR(0,48)
  1336. }else{
  1337. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1338. COLOR(300,48)
  1339. }
  1340. u*= 0x0101010101010101ULL;
  1341. v*= 0x0101010101010101ULL;
  1342. for(y=0; y<block_height; y++){
  1343. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= u;
  1344. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= v;
  1345. }
  1346. //segmentation
  1347. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1348. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1349. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1350. }
  1351. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1352. for(y=0; y<16; y++)
  1353. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1354. }
  1355. if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
  1356. int dm= 1 << (mv_sample_log2-2);
  1357. for(i=0; i<4; i++){
  1358. int sx= mb_x*16 + 8*(i&1);
  1359. int sy= mb_y*16 + 8*(i>>1);
  1360. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1361. //FIXME bidir
  1362. int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
  1363. if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
  1364. for(y=0; y<8; y++)
  1365. pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
  1366. if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
  1367. *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
  1368. }
  1369. }
  1370. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1371. // hmm
  1372. }
  1373. }
  1374. s->mbskip_table[mb_index]=0;
  1375. }
  1376. }
  1377. }
  1378. }
  1379. static inline int hpel_motion_lowres(MpegEncContext *s,
  1380. uint8_t *dest, uint8_t *src,
  1381. int field_based, int field_select,
  1382. int src_x, int src_y,
  1383. int width, int height, int stride,
  1384. int h_edge_pos, int v_edge_pos,
  1385. int w, int h, h264_chroma_mc_func *pix_op,
  1386. int motion_x, int motion_y)
  1387. {
  1388. const int lowres= s->avctx->lowres;
  1389. const int op_index= FFMIN(lowres, 2);
  1390. const int s_mask= (2<<lowres)-1;
  1391. int emu=0;
  1392. int sx, sy;
  1393. if(s->quarter_sample){
  1394. motion_x/=2;
  1395. motion_y/=2;
  1396. }
  1397. sx= motion_x & s_mask;
  1398. sy= motion_y & s_mask;
  1399. src_x += motion_x >> (lowres+1);
  1400. src_y += motion_y >> (lowres+1);
  1401. src += src_y * stride + src_x;
  1402. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  1403. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1404. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  1405. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1406. src= s->edge_emu_buffer;
  1407. emu=1;
  1408. }
  1409. sx= (sx << 2) >> lowres;
  1410. sy= (sy << 2) >> lowres;
  1411. if(field_select)
  1412. src += s->linesize;
  1413. pix_op[op_index](dest, src, stride, h, sx, sy);
  1414. return emu;
  1415. }
  1416. /* apply one mpeg motion vector to the three components */
  1417. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1418. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1419. int field_based, int bottom_field, int field_select,
  1420. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  1421. int motion_x, int motion_y, int h, int mb_y)
  1422. {
  1423. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1424. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  1425. const int lowres= s->avctx->lowres;
  1426. const int op_index= FFMIN(lowres, 2);
  1427. const int block_s= 8>>lowres;
  1428. const int s_mask= (2<<lowres)-1;
  1429. const int h_edge_pos = s->h_edge_pos >> lowres;
  1430. const int v_edge_pos = s->v_edge_pos >> lowres;
  1431. linesize = s->current_picture.linesize[0] << field_based;
  1432. uvlinesize = s->current_picture.linesize[1] << field_based;
  1433. if(s->quarter_sample){ //FIXME obviously not perfect but qpel will not work in lowres anyway
  1434. motion_x/=2;
  1435. motion_y/=2;
  1436. }
  1437. if(field_based){
  1438. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  1439. }
  1440. sx= motion_x & s_mask;
  1441. sy= motion_y & s_mask;
  1442. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  1443. src_y =( mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  1444. if (s->out_format == FMT_H263) {
  1445. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  1446. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  1447. uvsrc_x = src_x>>1;
  1448. uvsrc_y = src_y>>1;
  1449. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  1450. mx = motion_x / 4;
  1451. my = motion_y / 4;
  1452. uvsx = (2*mx) & s_mask;
  1453. uvsy = (2*my) & s_mask;
  1454. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  1455. uvsrc_y = mb_y*block_s + (my >> lowres);
  1456. } else {
  1457. mx = motion_x / 2;
  1458. my = motion_y / 2;
  1459. uvsx = mx & s_mask;
  1460. uvsy = my & s_mask;
  1461. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1462. uvsrc_y =( mb_y*block_s>>field_based) + (my >> (lowres+1));
  1463. }
  1464. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1465. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1466. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1467. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  1468. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1469. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  1470. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1471. ptr_y = s->edge_emu_buffer;
  1472. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1473. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  1474. s->dsp.emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  1475. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1476. s->dsp.emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  1477. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1478. ptr_cb= uvbuf;
  1479. ptr_cr= uvbuf+16;
  1480. }
  1481. }
  1482. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  1483. dest_y += s->linesize;
  1484. dest_cb+= s->uvlinesize;
  1485. dest_cr+= s->uvlinesize;
  1486. }
  1487. if(field_select){
  1488. ptr_y += s->linesize;
  1489. ptr_cb+= s->uvlinesize;
  1490. ptr_cr+= s->uvlinesize;
  1491. }
  1492. sx= (sx << 2) >> lowres;
  1493. sy= (sy << 2) >> lowres;
  1494. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  1495. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1496. uvsx= (uvsx << 2) >> lowres;
  1497. uvsy= (uvsy << 2) >> lowres;
  1498. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1499. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1500. }
  1501. //FIXME h261 lowres loop filter
  1502. }
  1503. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1504. uint8_t *dest_cb, uint8_t *dest_cr,
  1505. uint8_t **ref_picture,
  1506. h264_chroma_mc_func *pix_op,
  1507. int mx, int my){
  1508. const int lowres= s->avctx->lowres;
  1509. const int op_index= FFMIN(lowres, 2);
  1510. const int block_s= 8>>lowres;
  1511. const int s_mask= (2<<lowres)-1;
  1512. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  1513. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  1514. int emu=0, src_x, src_y, offset, sx, sy;
  1515. uint8_t *ptr;
  1516. if(s->quarter_sample){
  1517. mx/=2;
  1518. my/=2;
  1519. }
  1520. /* In case of 8X8, we construct a single chroma motion vector
  1521. with a special rounding */
  1522. mx= ff_h263_round_chroma(mx);
  1523. my= ff_h263_round_chroma(my);
  1524. sx= mx & s_mask;
  1525. sy= my & s_mask;
  1526. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  1527. src_y = s->mb_y*block_s + (my >> (lowres+1));
  1528. offset = src_y * s->uvlinesize + src_x;
  1529. ptr = ref_picture[1] + offset;
  1530. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1531. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  1532. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  1533. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1534. ptr= s->edge_emu_buffer;
  1535. emu=1;
  1536. }
  1537. }
  1538. sx= (sx << 2) >> lowres;
  1539. sy= (sy << 2) >> lowres;
  1540. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1541. ptr = ref_picture[2] + offset;
  1542. if(emu){
  1543. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1544. ptr= s->edge_emu_buffer;
  1545. }
  1546. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1547. }
  1548. /**
  1549. * motion compensation of a single macroblock
  1550. * @param s context
  1551. * @param dest_y luma destination pointer
  1552. * @param dest_cb chroma cb/u destination pointer
  1553. * @param dest_cr chroma cr/v destination pointer
  1554. * @param dir direction (0->forward, 1->backward)
  1555. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1556. * @param pix_op halfpel motion compensation function (average or put normally)
  1557. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1558. */
  1559. static inline void MPV_motion_lowres(MpegEncContext *s,
  1560. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1561. int dir, uint8_t **ref_picture,
  1562. h264_chroma_mc_func *pix_op)
  1563. {
  1564. int mx, my;
  1565. int mb_x, mb_y, i;
  1566. const int lowres= s->avctx->lowres;
  1567. const int block_s= 8>>lowres;
  1568. mb_x = s->mb_x;
  1569. mb_y = s->mb_y;
  1570. switch(s->mv_type) {
  1571. case MV_TYPE_16X16:
  1572. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1573. 0, 0, 0,
  1574. ref_picture, pix_op,
  1575. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y);
  1576. break;
  1577. case MV_TYPE_8X8:
  1578. mx = 0;
  1579. my = 0;
  1580. for(i=0;i<4;i++) {
  1581. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  1582. ref_picture[0], 0, 0,
  1583. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  1584. s->width, s->height, s->linesize,
  1585. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1586. block_s, block_s, pix_op,
  1587. s->mv[dir][i][0], s->mv[dir][i][1]);
  1588. mx += s->mv[dir][i][0];
  1589. my += s->mv[dir][i][1];
  1590. }
  1591. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  1592. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  1593. break;
  1594. case MV_TYPE_FIELD:
  1595. if (s->picture_structure == PICT_FRAME) {
  1596. /* top field */
  1597. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1598. 1, 0, s->field_select[dir][0],
  1599. ref_picture, pix_op,
  1600. s->mv[dir][0][0], s->mv[dir][0][1], block_s, mb_y);
  1601. /* bottom field */
  1602. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1603. 1, 1, s->field_select[dir][1],
  1604. ref_picture, pix_op,
  1605. s->mv[dir][1][0], s->mv[dir][1][1], block_s, mb_y);
  1606. } else {
  1607. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
  1608. ref_picture= s->current_picture_ptr->data;
  1609. }
  1610. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1611. 0, 0, s->field_select[dir][0],
  1612. ref_picture, pix_op,
  1613. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y>>1);
  1614. }
  1615. break;
  1616. case MV_TYPE_16X8:
  1617. for(i=0; i<2; i++){
  1618. uint8_t ** ref2picture;
  1619. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
  1620. ref2picture= ref_picture;
  1621. }else{
  1622. ref2picture= s->current_picture_ptr->data;
  1623. }
  1624. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1625. 0, 0, s->field_select[dir][i],
  1626. ref2picture, pix_op,
  1627. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s, mb_y>>1);
  1628. dest_y += 2*block_s*s->linesize;
  1629. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1630. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1631. }
  1632. break;
  1633. case MV_TYPE_DMV:
  1634. if(s->picture_structure == PICT_FRAME){
  1635. for(i=0; i<2; i++){
  1636. int j;
  1637. for(j=0; j<2; j++){
  1638. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1639. 1, j, j^i,
  1640. ref_picture, pix_op,
  1641. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s, mb_y);
  1642. }
  1643. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1644. }
  1645. }else{
  1646. for(i=0; i<2; i++){
  1647. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1648. 0, 0, s->picture_structure != i+1,
  1649. ref_picture, pix_op,
  1650. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s, mb_y>>1);
  1651. // after put we make avg of the same block
  1652. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1653. //opposite parity is always in the same frame if this is second field
  1654. if(!s->first_field){
  1655. ref_picture = s->current_picture_ptr->data;
  1656. }
  1657. }
  1658. }
  1659. break;
  1660. default: assert(0);
  1661. }
  1662. }
  1663. /**
  1664. * find the lowest MB row referenced in the MVs
  1665. */
  1666. int MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1667. {
  1668. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1669. int my, off, i, mvs;
  1670. if (s->picture_structure != PICT_FRAME) goto unhandled;
  1671. switch (s->mv_type) {
  1672. case MV_TYPE_16X16:
  1673. mvs = 1;
  1674. break;
  1675. case MV_TYPE_16X8:
  1676. mvs = 2;
  1677. break;
  1678. case MV_TYPE_8X8:
  1679. mvs = 4;
  1680. break;
  1681. default:
  1682. goto unhandled;
  1683. }
  1684. for (i = 0; i < mvs; i++) {
  1685. my = s->mv[dir][i][1]<<qpel_shift;
  1686. my_max = FFMAX(my_max, my);
  1687. my_min = FFMIN(my_min, my);
  1688. }
  1689. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1690. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1691. unhandled:
  1692. return s->mb_height-1;
  1693. }
  1694. /* put block[] to dest[] */
  1695. static inline void put_dct(MpegEncContext *s,
  1696. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1697. {
  1698. s->dct_unquantize_intra(s, block, i, qscale);
  1699. s->dsp.idct_put (dest, line_size, block);
  1700. }
  1701. /* add block[] to dest[] */
  1702. static inline void add_dct(MpegEncContext *s,
  1703. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1704. {
  1705. if (s->block_last_index[i] >= 0) {
  1706. s->dsp.idct_add (dest, line_size, block);
  1707. }
  1708. }
  1709. static inline void add_dequant_dct(MpegEncContext *s,
  1710. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1711. {
  1712. if (s->block_last_index[i] >= 0) {
  1713. s->dct_unquantize_inter(s, block, i, qscale);
  1714. s->dsp.idct_add (dest, line_size, block);
  1715. }
  1716. }
  1717. /**
  1718. * cleans dc, ac, coded_block for the current non intra MB
  1719. */
  1720. void ff_clean_intra_table_entries(MpegEncContext *s)
  1721. {
  1722. int wrap = s->b8_stride;
  1723. int xy = s->block_index[0];
  1724. s->dc_val[0][xy ] =
  1725. s->dc_val[0][xy + 1 ] =
  1726. s->dc_val[0][xy + wrap] =
  1727. s->dc_val[0][xy + 1 + wrap] = 1024;
  1728. /* ac pred */
  1729. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1730. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1731. if (s->msmpeg4_version>=3) {
  1732. s->coded_block[xy ] =
  1733. s->coded_block[xy + 1 ] =
  1734. s->coded_block[xy + wrap] =
  1735. s->coded_block[xy + 1 + wrap] = 0;
  1736. }
  1737. /* chroma */
  1738. wrap = s->mb_stride;
  1739. xy = s->mb_x + s->mb_y * wrap;
  1740. s->dc_val[1][xy] =
  1741. s->dc_val[2][xy] = 1024;
  1742. /* ac pred */
  1743. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1744. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1745. s->mbintra_table[xy]= 0;
  1746. }
  1747. /* generic function called after a macroblock has been parsed by the
  1748. decoder or after it has been encoded by the encoder.
  1749. Important variables used:
  1750. s->mb_intra : true if intra macroblock
  1751. s->mv_dir : motion vector direction
  1752. s->mv_type : motion vector type
  1753. s->mv : motion vector
  1754. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1755. */
  1756. static av_always_inline
  1757. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  1758. int lowres_flag, int is_mpeg12)
  1759. {
  1760. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1761. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1762. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1763. return;
  1764. }
  1765. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1766. /* save DCT coefficients */
  1767. int i,j;
  1768. DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
  1769. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1770. for(i=0; i<6; i++){
  1771. for(j=0; j<64; j++){
  1772. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1773. av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
  1774. }
  1775. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1776. }
  1777. }
  1778. s->current_picture.qscale_table[mb_xy]= s->qscale;
  1779. /* update DC predictors for P macroblocks */
  1780. if (!s->mb_intra) {
  1781. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1782. if(s->mbintra_table[mb_xy])
  1783. ff_clean_intra_table_entries(s);
  1784. } else {
  1785. s->last_dc[0] =
  1786. s->last_dc[1] =
  1787. s->last_dc[2] = 128 << s->intra_dc_precision;
  1788. }
  1789. }
  1790. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1791. s->mbintra_table[mb_xy]=1;
  1792. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  1793. uint8_t *dest_y, *dest_cb, *dest_cr;
  1794. int dct_linesize, dct_offset;
  1795. op_pixels_func (*op_pix)[4];
  1796. qpel_mc_func (*op_qpix)[16];
  1797. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1798. const int uvlinesize= s->current_picture.linesize[1];
  1799. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1800. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1801. /* avoid copy if macroblock skipped in last frame too */
  1802. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1803. if(!s->encoding){
  1804. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1805. const int age= s->current_picture.age;
  1806. assert(age);
  1807. if (s->mb_skipped) {
  1808. s->mb_skipped= 0;
  1809. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1810. (*mbskip_ptr) ++; /* indicate that this time we skipped it */
  1811. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1812. /* if previous was skipped too, then nothing to do ! */
  1813. if (*mbskip_ptr >= age && s->current_picture.reference){
  1814. return;
  1815. }
  1816. } else if(!s->current_picture.reference){
  1817. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1818. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1819. } else{
  1820. *mbskip_ptr = 0; /* not skipped */
  1821. }
  1822. }
  1823. dct_linesize = linesize << s->interlaced_dct;
  1824. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  1825. if(readable){
  1826. dest_y= s->dest[0];
  1827. dest_cb= s->dest[1];
  1828. dest_cr= s->dest[2];
  1829. }else{
  1830. dest_y = s->b_scratchpad;
  1831. dest_cb= s->b_scratchpad+16*linesize;
  1832. dest_cr= s->b_scratchpad+32*linesize;
  1833. }
  1834. if (!s->mb_intra) {
  1835. /* motion handling */
  1836. /* decoding or more than one mb_type (MC was already done otherwise) */
  1837. if(!s->encoding){
  1838. if(HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1839. if (s->mv_dir & MV_DIR_FORWARD) {
  1840. ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0);
  1841. }
  1842. if (s->mv_dir & MV_DIR_BACKWARD) {
  1843. ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0);
  1844. }
  1845. }
  1846. if(lowres_flag){
  1847. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  1848. if (s->mv_dir & MV_DIR_FORWARD) {
  1849. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix);
  1850. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  1851. }
  1852. if (s->mv_dir & MV_DIR_BACKWARD) {
  1853. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
  1854. }
  1855. }else{
  1856. op_qpix= s->me.qpel_put;
  1857. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1858. op_pix = s->dsp.put_pixels_tab;
  1859. }else{
  1860. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1861. }
  1862. if (s->mv_dir & MV_DIR_FORWARD) {
  1863. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1864. op_pix = s->dsp.avg_pixels_tab;
  1865. op_qpix= s->me.qpel_avg;
  1866. }
  1867. if (s->mv_dir & MV_DIR_BACKWARD) {
  1868. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1869. }
  1870. }
  1871. }
  1872. /* skip dequant / idct if we are really late ;) */
  1873. if(s->avctx->skip_idct){
  1874. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1875. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1876. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1877. goto skip_idct;
  1878. }
  1879. /* add dct residue */
  1880. if(s->encoding || !( s->msmpeg4_version || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  1881. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1882. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1883. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1884. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1885. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1886. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1887. if (s->chroma_y_shift){
  1888. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1889. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1890. }else{
  1891. dct_linesize >>= 1;
  1892. dct_offset >>=1;
  1893. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1894. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1895. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1896. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1897. }
  1898. }
  1899. } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
  1900. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1901. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1902. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1903. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1904. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1905. if(s->chroma_y_shift){//Chroma420
  1906. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1907. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1908. }else{
  1909. //chroma422
  1910. dct_linesize = uvlinesize << s->interlaced_dct;
  1911. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1912. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1913. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1914. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1915. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1916. if(!s->chroma_x_shift){//Chroma444
  1917. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1918. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1919. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1920. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1921. }
  1922. }
  1923. }//fi gray
  1924. }
  1925. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1926. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1927. }
  1928. } else {
  1929. /* dct only in intra block */
  1930. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  1931. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1932. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1933. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1934. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1935. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1936. if(s->chroma_y_shift){
  1937. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1938. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1939. }else{
  1940. dct_offset >>=1;
  1941. dct_linesize >>=1;
  1942. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1943. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1944. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1945. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1946. }
  1947. }
  1948. }else{
  1949. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1950. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1951. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1952. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1953. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1954. if(s->chroma_y_shift){
  1955. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1956. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1957. }else{
  1958. dct_linesize = uvlinesize << s->interlaced_dct;
  1959. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1960. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1961. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1962. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1963. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1964. if(!s->chroma_x_shift){//Chroma444
  1965. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1966. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1967. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1968. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1969. }
  1970. }
  1971. }//gray
  1972. }
  1973. }
  1974. skip_idct:
  1975. if(!readable){
  1976. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1977. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1978. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1979. }
  1980. }
  1981. }
  1982. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  1983. #if !CONFIG_SMALL
  1984. if(s->out_format == FMT_MPEG1) {
  1985. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  1986. else MPV_decode_mb_internal(s, block, 0, 1);
  1987. } else
  1988. #endif
  1989. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  1990. else MPV_decode_mb_internal(s, block, 0, 0);
  1991. }
  1992. /**
  1993. *
  1994. * @param h is the normal height, this will be reduced automatically if needed for the last row
  1995. */
  1996. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  1997. const int field_pic= s->picture_structure != PICT_FRAME;
  1998. if(field_pic){
  1999. h <<= 1;
  2000. y <<= 1;
  2001. }
  2002. if (!s->avctx->hwaccel
  2003. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2004. && s->unrestricted_mv
  2005. && s->current_picture.reference
  2006. && !s->intra_only
  2007. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  2008. int sides = 0, edge_h;
  2009. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  2010. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  2011. if (y==0) sides |= EDGE_TOP;
  2012. if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
  2013. edge_h= FFMIN(h, s->v_edge_pos - y);
  2014. s->dsp.draw_edges(s->current_picture_ptr->data[0] + y *s->linesize , s->linesize,
  2015. s->h_edge_pos , edge_h , EDGE_WIDTH , EDGE_WIDTH , sides);
  2016. s->dsp.draw_edges(s->current_picture_ptr->data[1] + (y>>vshift)*s->uvlinesize, s->uvlinesize,
  2017. s->h_edge_pos>>hshift, edge_h>>hshift, EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2018. s->dsp.draw_edges(s->current_picture_ptr->data[2] + (y>>vshift)*s->uvlinesize, s->uvlinesize,
  2019. s->h_edge_pos>>hshift, edge_h>>hshift, EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2020. }
  2021. h= FFMIN(h, s->avctx->height - y);
  2022. if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2023. if (s->avctx->draw_horiz_band) {
  2024. AVFrame *src;
  2025. int offset[4];
  2026. if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2027. src= (AVFrame*)s->current_picture_ptr;
  2028. else if(s->last_picture_ptr)
  2029. src= (AVFrame*)s->last_picture_ptr;
  2030. else
  2031. return;
  2032. if(s->pict_type==AV_PICTURE_TYPE_B && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2033. offset[0]=
  2034. offset[1]=
  2035. offset[2]=
  2036. offset[3]= 0;
  2037. }else{
  2038. offset[0]= y * s->linesize;
  2039. offset[1]=
  2040. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  2041. offset[3]= 0;
  2042. }
  2043. emms_c();
  2044. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2045. y, s->picture_structure, h);
  2046. }
  2047. }
  2048. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2049. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  2050. const int uvlinesize= s->current_picture.linesize[1];
  2051. const int mb_size= 4 - s->avctx->lowres;
  2052. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2053. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2054. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2055. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2056. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2057. s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2058. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2059. s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << mb_size);
  2060. s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2061. s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2062. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2063. {
  2064. if(s->picture_structure==PICT_FRAME){
  2065. s->dest[0] += s->mb_y * linesize << mb_size;
  2066. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2067. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2068. }else{
  2069. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2070. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2071. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2072. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2073. }
  2074. }
  2075. }
  2076. void ff_mpeg_flush(AVCodecContext *avctx){
  2077. int i;
  2078. MpegEncContext *s = avctx->priv_data;
  2079. if(s==NULL || s->picture==NULL)
  2080. return;
  2081. for(i=0; i<s->picture_count; i++){
  2082. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  2083. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  2084. free_frame_buffer(s, &s->picture[i]);
  2085. }
  2086. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2087. s->mb_x= s->mb_y= 0;
  2088. s->closed_gop= 0;
  2089. s->parse_context.state= -1;
  2090. s->parse_context.frame_start_found= 0;
  2091. s->parse_context.overread= 0;
  2092. s->parse_context.overread_index= 0;
  2093. s->parse_context.index= 0;
  2094. s->parse_context.last_index= 0;
  2095. s->bitstream_buffer_size=0;
  2096. s->pp_time=0;
  2097. }
  2098. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2099. DCTELEM *block, int n, int qscale)
  2100. {
  2101. int i, level, nCoeffs;
  2102. const uint16_t *quant_matrix;
  2103. nCoeffs= s->block_last_index[n];
  2104. if (n < 4)
  2105. block[0] = block[0] * s->y_dc_scale;
  2106. else
  2107. block[0] = block[0] * s->c_dc_scale;
  2108. /* XXX: only mpeg1 */
  2109. quant_matrix = s->intra_matrix;
  2110. for(i=1;i<=nCoeffs;i++) {
  2111. int j= s->intra_scantable.permutated[i];
  2112. level = block[j];
  2113. if (level) {
  2114. if (level < 0) {
  2115. level = -level;
  2116. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2117. level = (level - 1) | 1;
  2118. level = -level;
  2119. } else {
  2120. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2121. level = (level - 1) | 1;
  2122. }
  2123. block[j] = level;
  2124. }
  2125. }
  2126. }
  2127. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2128. DCTELEM *block, int n, int qscale)
  2129. {
  2130. int i, level, nCoeffs;
  2131. const uint16_t *quant_matrix;
  2132. nCoeffs= s->block_last_index[n];
  2133. quant_matrix = s->inter_matrix;
  2134. for(i=0; i<=nCoeffs; i++) {
  2135. int j= s->intra_scantable.permutated[i];
  2136. level = block[j];
  2137. if (level) {
  2138. if (level < 0) {
  2139. level = -level;
  2140. level = (((level << 1) + 1) * qscale *
  2141. ((int) (quant_matrix[j]))) >> 4;
  2142. level = (level - 1) | 1;
  2143. level = -level;
  2144. } else {
  2145. level = (((level << 1) + 1) * qscale *
  2146. ((int) (quant_matrix[j]))) >> 4;
  2147. level = (level - 1) | 1;
  2148. }
  2149. block[j] = level;
  2150. }
  2151. }
  2152. }
  2153. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2154. DCTELEM *block, int n, int qscale)
  2155. {
  2156. int i, level, nCoeffs;
  2157. const uint16_t *quant_matrix;
  2158. if(s->alternate_scan) nCoeffs= 63;
  2159. else nCoeffs= s->block_last_index[n];
  2160. if (n < 4)
  2161. block[0] = block[0] * s->y_dc_scale;
  2162. else
  2163. block[0] = block[0] * s->c_dc_scale;
  2164. quant_matrix = s->intra_matrix;
  2165. for(i=1;i<=nCoeffs;i++) {
  2166. int j= s->intra_scantable.permutated[i];
  2167. level = block[j];
  2168. if (level) {
  2169. if (level < 0) {
  2170. level = -level;
  2171. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2172. level = -level;
  2173. } else {
  2174. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2175. }
  2176. block[j] = level;
  2177. }
  2178. }
  2179. }
  2180. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2181. DCTELEM *block, int n, int qscale)
  2182. {
  2183. int i, level, nCoeffs;
  2184. const uint16_t *quant_matrix;
  2185. int sum=-1;
  2186. if(s->alternate_scan) nCoeffs= 63;
  2187. else nCoeffs= s->block_last_index[n];
  2188. if (n < 4)
  2189. block[0] = block[0] * s->y_dc_scale;
  2190. else
  2191. block[0] = block[0] * s->c_dc_scale;
  2192. quant_matrix = s->intra_matrix;
  2193. for(i=1;i<=nCoeffs;i++) {
  2194. int j= s->intra_scantable.permutated[i];
  2195. level = block[j];
  2196. if (level) {
  2197. if (level < 0) {
  2198. level = -level;
  2199. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2200. level = -level;
  2201. } else {
  2202. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2203. }
  2204. block[j] = level;
  2205. sum+=level;
  2206. }
  2207. }
  2208. block[63]^=sum&1;
  2209. }
  2210. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2211. DCTELEM *block, int n, int qscale)
  2212. {
  2213. int i, level, nCoeffs;
  2214. const uint16_t *quant_matrix;
  2215. int sum=-1;
  2216. if(s->alternate_scan) nCoeffs= 63;
  2217. else nCoeffs= s->block_last_index[n];
  2218. quant_matrix = s->inter_matrix;
  2219. for(i=0; i<=nCoeffs; i++) {
  2220. int j= s->intra_scantable.permutated[i];
  2221. level = block[j];
  2222. if (level) {
  2223. if (level < 0) {
  2224. level = -level;
  2225. level = (((level << 1) + 1) * qscale *
  2226. ((int) (quant_matrix[j]))) >> 4;
  2227. level = -level;
  2228. } else {
  2229. level = (((level << 1) + 1) * qscale *
  2230. ((int) (quant_matrix[j]))) >> 4;
  2231. }
  2232. block[j] = level;
  2233. sum+=level;
  2234. }
  2235. }
  2236. block[63]^=sum&1;
  2237. }
  2238. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2239. DCTELEM *block, int n, int qscale)
  2240. {
  2241. int i, level, qmul, qadd;
  2242. int nCoeffs;
  2243. assert(s->block_last_index[n]>=0);
  2244. qmul = qscale << 1;
  2245. if (!s->h263_aic) {
  2246. if (n < 4)
  2247. block[0] = block[0] * s->y_dc_scale;
  2248. else
  2249. block[0] = block[0] * s->c_dc_scale;
  2250. qadd = (qscale - 1) | 1;
  2251. }else{
  2252. qadd = 0;
  2253. }
  2254. if(s->ac_pred)
  2255. nCoeffs=63;
  2256. else
  2257. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2258. for(i=1; i<=nCoeffs; i++) {
  2259. level = block[i];
  2260. if (level) {
  2261. if (level < 0) {
  2262. level = level * qmul - qadd;
  2263. } else {
  2264. level = level * qmul + qadd;
  2265. }
  2266. block[i] = level;
  2267. }
  2268. }
  2269. }
  2270. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2271. DCTELEM *block, int n, int qscale)
  2272. {
  2273. int i, level, qmul, qadd;
  2274. int nCoeffs;
  2275. assert(s->block_last_index[n]>=0);
  2276. qadd = (qscale - 1) | 1;
  2277. qmul = qscale << 1;
  2278. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2279. for(i=0; i<=nCoeffs; i++) {
  2280. level = block[i];
  2281. if (level) {
  2282. if (level < 0) {
  2283. level = level * qmul - qadd;
  2284. } else {
  2285. level = level * qmul + qadd;
  2286. }
  2287. block[i] = level;
  2288. }
  2289. }
  2290. }
  2291. /**
  2292. * set qscale and update qscale dependent variables.
  2293. */
  2294. void ff_set_qscale(MpegEncContext * s, int qscale)
  2295. {
  2296. if (qscale < 1)
  2297. qscale = 1;
  2298. else if (qscale > 31)
  2299. qscale = 31;
  2300. s->qscale = qscale;
  2301. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2302. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2303. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2304. }
  2305. void MPV_report_decode_progress(MpegEncContext *s)
  2306. {
  2307. if (s->pict_type != FF_B_TYPE && !s->partitioned_frame && !s->error_occurred)
  2308. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0);
  2309. }