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.

2654 lines
91KB

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