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.

2658 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. s->input_picture_number = s1->input_picture_number;
  593. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  594. ff_mpeg_unref_picture(s, &s->picture[i]);
  595. if (s1->picture[i].f.buf[0] &&
  596. (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
  597. return ret;
  598. }
  599. #define UPDATE_PICTURE(pic)\
  600. do {\
  601. ff_mpeg_unref_picture(s, &s->pic);\
  602. if (s1->pic.f.buf[0])\
  603. ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
  604. else\
  605. ret = update_picture_tables(&s->pic, &s1->pic);\
  606. if (ret < 0)\
  607. return ret;\
  608. } while (0)
  609. UPDATE_PICTURE(current_picture);
  610. UPDATE_PICTURE(last_picture);
  611. UPDATE_PICTURE(next_picture);
  612. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  613. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  614. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  615. // Error/bug resilience
  616. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  617. s->workaround_bugs = s1->workaround_bugs;
  618. // MPEG4 timing info
  619. memcpy(&s->last_time_base, &s1->last_time_base,
  620. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  621. (char *) &s1->last_time_base);
  622. // B-frame info
  623. s->max_b_frames = s1->max_b_frames;
  624. s->low_delay = s1->low_delay;
  625. s->droppable = s1->droppable;
  626. // DivX handling (doesn't work)
  627. s->divx_packed = s1->divx_packed;
  628. if (s1->bitstream_buffer) {
  629. if (s1->bitstream_buffer_size +
  630. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  631. av_fast_malloc(&s->bitstream_buffer,
  632. &s->allocated_bitstream_buffer_size,
  633. s1->allocated_bitstream_buffer_size);
  634. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  635. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  636. s1->bitstream_buffer_size);
  637. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  638. FF_INPUT_BUFFER_PADDING_SIZE);
  639. }
  640. // linesize dependend scratch buffer allocation
  641. if (!s->edge_emu_buffer)
  642. if (s1->linesize) {
  643. if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
  644. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  645. "scratch buffers.\n");
  646. return AVERROR(ENOMEM);
  647. }
  648. } else {
  649. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  650. "be allocated due to unknown size.\n");
  651. return AVERROR_BUG;
  652. }
  653. // MPEG2/interlacing info
  654. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  655. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  656. if (!s1->first_field) {
  657. s->last_pict_type = s1->pict_type;
  658. if (s1->current_picture_ptr)
  659. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  660. if (s1->pict_type != AV_PICTURE_TYPE_B) {
  661. s->last_non_b_pict_type = s1->pict_type;
  662. }
  663. }
  664. return 0;
  665. }
  666. /**
  667. * Set the given MpegEncContext to common defaults
  668. * (same for encoding and decoding).
  669. * The changed fields will not depend upon the
  670. * prior state of the MpegEncContext.
  671. */
  672. void ff_MPV_common_defaults(MpegEncContext *s)
  673. {
  674. s->y_dc_scale_table =
  675. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  676. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  677. s->progressive_frame = 1;
  678. s->progressive_sequence = 1;
  679. s->picture_structure = PICT_FRAME;
  680. s->coded_picture_number = 0;
  681. s->picture_number = 0;
  682. s->input_picture_number = 0;
  683. s->picture_in_gop_number = 0;
  684. s->f_code = 1;
  685. s->b_code = 1;
  686. s->slice_context_count = 1;
  687. }
  688. /**
  689. * Set the given MpegEncContext to defaults for decoding.
  690. * the changed fields will not depend upon
  691. * the prior state of the MpegEncContext.
  692. */
  693. void ff_MPV_decode_defaults(MpegEncContext *s)
  694. {
  695. ff_MPV_common_defaults(s);
  696. }
  697. static int init_er(MpegEncContext *s)
  698. {
  699. ERContext *er = &s->er;
  700. int mb_array_size = s->mb_height * s->mb_stride;
  701. int i;
  702. er->avctx = s->avctx;
  703. er->dsp = &s->dsp;
  704. er->mb_index2xy = s->mb_index2xy;
  705. er->mb_num = s->mb_num;
  706. er->mb_width = s->mb_width;
  707. er->mb_height = s->mb_height;
  708. er->mb_stride = s->mb_stride;
  709. er->b8_stride = s->b8_stride;
  710. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  711. er->error_status_table = av_mallocz(mb_array_size);
  712. if (!er->er_temp_buffer || !er->error_status_table)
  713. goto fail;
  714. er->mbskip_table = s->mbskip_table;
  715. er->mbintra_table = s->mbintra_table;
  716. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  717. er->dc_val[i] = s->dc_val[i];
  718. er->decode_mb = mpeg_er_decode_mb;
  719. er->opaque = s;
  720. return 0;
  721. fail:
  722. av_freep(&er->er_temp_buffer);
  723. av_freep(&er->error_status_table);
  724. return AVERROR(ENOMEM);
  725. }
  726. /**
  727. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  728. */
  729. static int init_context_frame(MpegEncContext *s)
  730. {
  731. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  732. s->mb_width = (s->width + 15) / 16;
  733. s->mb_stride = s->mb_width + 1;
  734. s->b8_stride = s->mb_width * 2 + 1;
  735. s->b4_stride = s->mb_width * 4 + 1;
  736. mb_array_size = s->mb_height * s->mb_stride;
  737. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  738. /* set default edge pos, will be overriden
  739. * in decode_header if needed */
  740. s->h_edge_pos = s->mb_width * 16;
  741. s->v_edge_pos = s->mb_height * 16;
  742. s->mb_num = s->mb_width * s->mb_height;
  743. s->block_wrap[0] =
  744. s->block_wrap[1] =
  745. s->block_wrap[2] =
  746. s->block_wrap[3] = s->b8_stride;
  747. s->block_wrap[4] =
  748. s->block_wrap[5] = s->mb_stride;
  749. y_size = s->b8_stride * (2 * s->mb_height + 1);
  750. c_size = s->mb_stride * (s->mb_height + 1);
  751. yc_size = y_size + 2 * c_size;
  752. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  753. fail); // error ressilience code looks cleaner with this
  754. for (y = 0; y < s->mb_height; y++)
  755. for (x = 0; x < s->mb_width; x++)
  756. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  757. s->mb_index2xy[s->mb_height * s->mb_width] =
  758. (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  759. if (s->encoding) {
  760. /* Allocate MV tables */
  761. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
  762. mv_table_size * 2 * sizeof(int16_t), fail);
  763. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
  764. mv_table_size * 2 * sizeof(int16_t), fail);
  765. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
  766. mv_table_size * 2 * sizeof(int16_t), fail);
  767. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
  768. mv_table_size * 2 * sizeof(int16_t), fail);
  769. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
  770. mv_table_size * 2 * sizeof(int16_t), fail);
  771. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
  772. mv_table_size * 2 * sizeof(int16_t), fail);
  773. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  774. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  775. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  776. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
  777. s->mb_stride + 1;
  778. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
  779. s->mb_stride + 1;
  780. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  781. /* Allocate MB type table */
  782. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
  783. sizeof(uint16_t), fail); // needed for encoding
  784. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
  785. sizeof(int), fail);
  786. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  787. mb_array_size * sizeof(float), fail);
  788. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  789. mb_array_size * sizeof(float), fail);
  790. }
  791. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  792. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  793. /* interlaced direct mode decoding tables */
  794. for (i = 0; i < 2; i++) {
  795. int j, k;
  796. for (j = 0; j < 2; j++) {
  797. for (k = 0; k < 2; k++) {
  798. FF_ALLOCZ_OR_GOTO(s->avctx,
  799. s->b_field_mv_table_base[i][j][k],
  800. mv_table_size * 2 * sizeof(int16_t),
  801. fail);
  802. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  803. s->mb_stride + 1;
  804. }
  805. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
  806. mb_array_size * 2 * sizeof(uint8_t), fail);
  807. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
  808. mv_table_size * 2 * sizeof(int16_t), fail);
  809. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
  810. + s->mb_stride + 1;
  811. }
  812. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
  813. mb_array_size * 2 * sizeof(uint8_t), fail);
  814. }
  815. }
  816. if (s->out_format == FMT_H263) {
  817. /* cbp values */
  818. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  819. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  820. /* cbp, ac_pred, pred_dir */
  821. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
  822. mb_array_size * sizeof(uint8_t), fail);
  823. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
  824. mb_array_size * sizeof(uint8_t), fail);
  825. }
  826. if (s->h263_pred || s->h263_plus || !s->encoding) {
  827. /* dc values */
  828. // MN: we need these for error resilience of intra-frames
  829. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
  830. yc_size * sizeof(int16_t), fail);
  831. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  832. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  833. s->dc_val[2] = s->dc_val[1] + c_size;
  834. for (i = 0; i < yc_size; i++)
  835. s->dc_val_base[i] = 1024;
  836. }
  837. /* which mb is a intra block */
  838. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  839. memset(s->mbintra_table, 1, mb_array_size);
  840. /* init macroblock skip table */
  841. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  842. // Note the + 1 is for a quicker mpeg4 slice_end detection
  843. return init_er(s);
  844. fail:
  845. return AVERROR(ENOMEM);
  846. }
  847. /**
  848. * init common structure for both encoder and decoder.
  849. * this assumes that some variables like width/height are already set
  850. */
  851. av_cold int ff_MPV_common_init(MpegEncContext *s)
  852. {
  853. int i;
  854. int nb_slices = (HAVE_THREADS &&
  855. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  856. s->avctx->thread_count : 1;
  857. if (s->encoding && s->avctx->slices)
  858. nb_slices = s->avctx->slices;
  859. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  860. s->mb_height = (s->height + 31) / 32 * 2;
  861. else
  862. s->mb_height = (s->height + 15) / 16;
  863. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  864. av_log(s->avctx, AV_LOG_ERROR,
  865. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  866. return -1;
  867. }
  868. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  869. int max_slices;
  870. if (s->mb_height)
  871. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  872. else
  873. max_slices = MAX_THREADS;
  874. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  875. " reducing to %d\n", nb_slices, max_slices);
  876. nb_slices = max_slices;
  877. }
  878. if ((s->width || s->height) &&
  879. av_image_check_size(s->width, s->height, 0, s->avctx))
  880. return -1;
  881. ff_dct_common_init(s);
  882. s->flags = s->avctx->flags;
  883. s->flags2 = s->avctx->flags2;
  884. /* set chroma shifts */
  885. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  886. &s->chroma_x_shift,
  887. &s->chroma_y_shift);
  888. /* convert fourcc to upper case */
  889. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  890. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  891. if (s->width && s->height) {
  892. s->avctx->coded_frame = &s->current_picture.f;
  893. if (s->encoding) {
  894. if (s->msmpeg4_version) {
  895. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
  896. 2 * 2 * (MAX_LEVEL + 1) *
  897. (MAX_RUN + 1) * 2 * sizeof(int), fail);
  898. }
  899. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  900. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,
  901. 64 * 32 * sizeof(int), fail);
  902. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,
  903. 64 * 32 * sizeof(int), fail);
  904. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,
  905. 64 * 32 * 2 * sizeof(uint16_t), fail);
  906. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,
  907. 64 * 32 * 2 * sizeof(uint16_t), fail);
  908. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
  909. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  910. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
  911. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  912. if (s->avctx->noise_reduction) {
  913. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
  914. 2 * 64 * sizeof(uint16_t), fail);
  915. }
  916. }
  917. }
  918. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  919. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  920. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  921. avcodec_get_frame_defaults(&s->picture[i].f);
  922. }
  923. memset(&s->next_picture, 0, sizeof(s->next_picture));
  924. memset(&s->last_picture, 0, sizeof(s->last_picture));
  925. memset(&s->current_picture, 0, sizeof(s->current_picture));
  926. avcodec_get_frame_defaults(&s->next_picture.f);
  927. avcodec_get_frame_defaults(&s->last_picture.f);
  928. avcodec_get_frame_defaults(&s->current_picture.f);
  929. if (s->width && s->height) {
  930. if (init_context_frame(s))
  931. goto fail;
  932. s->parse_context.state = -1;
  933. }
  934. s->context_initialized = 1;
  935. s->thread_context[0] = s;
  936. if (s->width && s->height) {
  937. if (nb_slices > 1) {
  938. for (i = 1; i < nb_slices; i++) {
  939. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  940. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  941. }
  942. for (i = 0; i < nb_slices; i++) {
  943. if (init_duplicate_context(s->thread_context[i]) < 0)
  944. goto fail;
  945. s->thread_context[i]->start_mb_y =
  946. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  947. s->thread_context[i]->end_mb_y =
  948. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  949. }
  950. } else {
  951. if (init_duplicate_context(s) < 0)
  952. goto fail;
  953. s->start_mb_y = 0;
  954. s->end_mb_y = s->mb_height;
  955. }
  956. s->slice_context_count = nb_slices;
  957. }
  958. return 0;
  959. fail:
  960. ff_MPV_common_end(s);
  961. return -1;
  962. }
  963. /**
  964. * Frees and resets MpegEncContext fields depending on the resolution.
  965. * Is used during resolution changes to avoid a full reinitialization of the
  966. * codec.
  967. */
  968. static int free_context_frame(MpegEncContext *s)
  969. {
  970. int i, j, k;
  971. av_freep(&s->mb_type);
  972. av_freep(&s->p_mv_table_base);
  973. av_freep(&s->b_forw_mv_table_base);
  974. av_freep(&s->b_back_mv_table_base);
  975. av_freep(&s->b_bidir_forw_mv_table_base);
  976. av_freep(&s->b_bidir_back_mv_table_base);
  977. av_freep(&s->b_direct_mv_table_base);
  978. s->p_mv_table = NULL;
  979. s->b_forw_mv_table = NULL;
  980. s->b_back_mv_table = NULL;
  981. s->b_bidir_forw_mv_table = NULL;
  982. s->b_bidir_back_mv_table = NULL;
  983. s->b_direct_mv_table = NULL;
  984. for (i = 0; i < 2; i++) {
  985. for (j = 0; j < 2; j++) {
  986. for (k = 0; k < 2; k++) {
  987. av_freep(&s->b_field_mv_table_base[i][j][k]);
  988. s->b_field_mv_table[i][j][k] = NULL;
  989. }
  990. av_freep(&s->b_field_select_table[i][j]);
  991. av_freep(&s->p_field_mv_table_base[i][j]);
  992. s->p_field_mv_table[i][j] = NULL;
  993. }
  994. av_freep(&s->p_field_select_table[i]);
  995. }
  996. av_freep(&s->dc_val_base);
  997. av_freep(&s->coded_block_base);
  998. av_freep(&s->mbintra_table);
  999. av_freep(&s->cbp_table);
  1000. av_freep(&s->pred_dir_table);
  1001. av_freep(&s->mbskip_table);
  1002. av_freep(&s->er.error_status_table);
  1003. av_freep(&s->er.er_temp_buffer);
  1004. av_freep(&s->mb_index2xy);
  1005. av_freep(&s->lambda_table);
  1006. av_freep(&s->cplx_tab);
  1007. av_freep(&s->bits_tab);
  1008. s->linesize = s->uvlinesize = 0;
  1009. return 0;
  1010. }
  1011. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1012. {
  1013. int i, err = 0;
  1014. if (s->slice_context_count > 1) {
  1015. for (i = 0; i < s->slice_context_count; i++) {
  1016. free_duplicate_context(s->thread_context[i]);
  1017. }
  1018. for (i = 1; i < s->slice_context_count; i++) {
  1019. av_freep(&s->thread_context[i]);
  1020. }
  1021. } else
  1022. free_duplicate_context(s);
  1023. if ((err = free_context_frame(s)) < 0)
  1024. return err;
  1025. if (s->picture)
  1026. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1027. s->picture[i].needs_realloc = 1;
  1028. }
  1029. s->last_picture_ptr =
  1030. s->next_picture_ptr =
  1031. s->current_picture_ptr = NULL;
  1032. // init
  1033. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1034. s->mb_height = (s->height + 31) / 32 * 2;
  1035. else
  1036. s->mb_height = (s->height + 15) / 16;
  1037. if ((s->width || s->height) &&
  1038. av_image_check_size(s->width, s->height, 0, s->avctx))
  1039. return AVERROR_INVALIDDATA;
  1040. if ((err = init_context_frame(s)))
  1041. goto fail;
  1042. s->thread_context[0] = s;
  1043. if (s->width && s->height) {
  1044. int nb_slices = s->slice_context_count;
  1045. if (nb_slices > 1) {
  1046. for (i = 1; i < nb_slices; i++) {
  1047. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1048. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1049. }
  1050. for (i = 0; i < nb_slices; i++) {
  1051. if (init_duplicate_context(s->thread_context[i]) < 0)
  1052. goto fail;
  1053. s->thread_context[i]->start_mb_y =
  1054. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1055. s->thread_context[i]->end_mb_y =
  1056. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1057. }
  1058. } else {
  1059. if (init_duplicate_context(s) < 0)
  1060. goto fail;
  1061. s->start_mb_y = 0;
  1062. s->end_mb_y = s->mb_height;
  1063. }
  1064. s->slice_context_count = nb_slices;
  1065. }
  1066. return 0;
  1067. fail:
  1068. ff_MPV_common_end(s);
  1069. return err;
  1070. }
  1071. /* init common structure for both encoder and decoder */
  1072. void ff_MPV_common_end(MpegEncContext *s)
  1073. {
  1074. int i;
  1075. if (s->slice_context_count > 1) {
  1076. for (i = 0; i < s->slice_context_count; i++) {
  1077. free_duplicate_context(s->thread_context[i]);
  1078. }
  1079. for (i = 1; i < s->slice_context_count; i++) {
  1080. av_freep(&s->thread_context[i]);
  1081. }
  1082. s->slice_context_count = 1;
  1083. } else free_duplicate_context(s);
  1084. av_freep(&s->parse_context.buffer);
  1085. s->parse_context.buffer_size = 0;
  1086. av_freep(&s->bitstream_buffer);
  1087. s->allocated_bitstream_buffer_size = 0;
  1088. av_freep(&s->avctx->stats_out);
  1089. av_freep(&s->ac_stats);
  1090. av_freep(&s->q_intra_matrix);
  1091. av_freep(&s->q_inter_matrix);
  1092. av_freep(&s->q_intra_matrix16);
  1093. av_freep(&s->q_inter_matrix16);
  1094. av_freep(&s->input_picture);
  1095. av_freep(&s->reordered_input_picture);
  1096. av_freep(&s->dct_offset);
  1097. if (s->picture) {
  1098. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1099. free_picture_tables(&s->picture[i]);
  1100. ff_mpeg_unref_picture(s, &s->picture[i]);
  1101. }
  1102. }
  1103. av_freep(&s->picture);
  1104. free_picture_tables(&s->last_picture);
  1105. ff_mpeg_unref_picture(s, &s->last_picture);
  1106. free_picture_tables(&s->current_picture);
  1107. ff_mpeg_unref_picture(s, &s->current_picture);
  1108. free_picture_tables(&s->next_picture);
  1109. ff_mpeg_unref_picture(s, &s->next_picture);
  1110. free_picture_tables(&s->new_picture);
  1111. ff_mpeg_unref_picture(s, &s->new_picture);
  1112. free_context_frame(s);
  1113. s->context_initialized = 0;
  1114. s->last_picture_ptr =
  1115. s->next_picture_ptr =
  1116. s->current_picture_ptr = NULL;
  1117. s->linesize = s->uvlinesize = 0;
  1118. }
  1119. av_cold void ff_init_rl(RLTable *rl,
  1120. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1121. {
  1122. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1123. uint8_t index_run[MAX_RUN + 1];
  1124. int last, run, level, start, end, i;
  1125. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1126. if (static_store && rl->max_level[0])
  1127. return;
  1128. /* compute max_level[], max_run[] and index_run[] */
  1129. for (last = 0; last < 2; last++) {
  1130. if (last == 0) {
  1131. start = 0;
  1132. end = rl->last;
  1133. } else {
  1134. start = rl->last;
  1135. end = rl->n;
  1136. }
  1137. memset(max_level, 0, MAX_RUN + 1);
  1138. memset(max_run, 0, MAX_LEVEL + 1);
  1139. memset(index_run, rl->n, MAX_RUN + 1);
  1140. for (i = start; i < end; i++) {
  1141. run = rl->table_run[i];
  1142. level = rl->table_level[i];
  1143. if (index_run[run] == rl->n)
  1144. index_run[run] = i;
  1145. if (level > max_level[run])
  1146. max_level[run] = level;
  1147. if (run > max_run[level])
  1148. max_run[level] = run;
  1149. }
  1150. if (static_store)
  1151. rl->max_level[last] = static_store[last];
  1152. else
  1153. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1154. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1155. if (static_store)
  1156. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1157. else
  1158. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1159. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1160. if (static_store)
  1161. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1162. else
  1163. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1164. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1165. }
  1166. }
  1167. av_cold void ff_init_vlc_rl(RLTable *rl)
  1168. {
  1169. int i, q;
  1170. for (q = 0; q < 32; q++) {
  1171. int qmul = q * 2;
  1172. int qadd = (q - 1) | 1;
  1173. if (q == 0) {
  1174. qmul = 1;
  1175. qadd = 0;
  1176. }
  1177. for (i = 0; i < rl->vlc.table_size; i++) {
  1178. int code = rl->vlc.table[i][0];
  1179. int len = rl->vlc.table[i][1];
  1180. int level, run;
  1181. if (len == 0) { // illegal code
  1182. run = 66;
  1183. level = MAX_LEVEL;
  1184. } else if (len < 0) { // more bits needed
  1185. run = 0;
  1186. level = code;
  1187. } else {
  1188. if (code == rl->n) { // esc
  1189. run = 66;
  1190. level = 0;
  1191. } else {
  1192. run = rl->table_run[code] + 1;
  1193. level = rl->table_level[code] * qmul + qadd;
  1194. if (code >= rl->last) run += 192;
  1195. }
  1196. }
  1197. rl->rl_vlc[q][i].len = len;
  1198. rl->rl_vlc[q][i].level = level;
  1199. rl->rl_vlc[q][i].run = run;
  1200. }
  1201. }
  1202. }
  1203. void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
  1204. {
  1205. int i;
  1206. /* release non reference frames */
  1207. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1208. if (!s->picture[i].reference &&
  1209. (remove_current || &s->picture[i] != s->current_picture_ptr)) {
  1210. ff_mpeg_unref_picture(s, &s->picture[i]);
  1211. }
  1212. }
  1213. }
  1214. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1215. {
  1216. if (pic->f.buf[0] == NULL)
  1217. return 1;
  1218. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1219. return 1;
  1220. return 0;
  1221. }
  1222. static int find_unused_picture(MpegEncContext *s, int shared)
  1223. {
  1224. int i;
  1225. if (shared) {
  1226. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1227. if (s->picture[i].f.buf[0] == NULL)
  1228. return i;
  1229. }
  1230. } else {
  1231. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1232. if (pic_is_unused(s, &s->picture[i]))
  1233. return i;
  1234. }
  1235. }
  1236. return AVERROR_INVALIDDATA;
  1237. }
  1238. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1239. {
  1240. int ret = find_unused_picture(s, shared);
  1241. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1242. if (s->picture[ret].needs_realloc) {
  1243. s->picture[ret].needs_realloc = 0;
  1244. free_picture_tables(&s->picture[ret]);
  1245. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1246. avcodec_get_frame_defaults(&s->picture[ret].f);
  1247. }
  1248. }
  1249. return ret;
  1250. }
  1251. static void update_noise_reduction(MpegEncContext *s)
  1252. {
  1253. int intra, i;
  1254. for (intra = 0; intra < 2; intra++) {
  1255. if (s->dct_count[intra] > (1 << 16)) {
  1256. for (i = 0; i < 64; i++) {
  1257. s->dct_error_sum[intra][i] >>= 1;
  1258. }
  1259. s->dct_count[intra] >>= 1;
  1260. }
  1261. for (i = 0; i < 64; i++) {
  1262. s->dct_offset[intra][i] = (s->avctx->noise_reduction *
  1263. s->dct_count[intra] +
  1264. s->dct_error_sum[intra][i] / 2) /
  1265. (s->dct_error_sum[intra][i] + 1);
  1266. }
  1267. }
  1268. }
  1269. /**
  1270. * generic function for encode/decode called after coding/decoding
  1271. * the header and before a frame is coded/decoded.
  1272. */
  1273. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1274. {
  1275. int i, ret;
  1276. Picture *pic;
  1277. s->mb_skipped = 0;
  1278. /* mark & release old frames */
  1279. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1280. s->last_picture_ptr != s->next_picture_ptr &&
  1281. s->last_picture_ptr->f.buf[0]) {
  1282. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1283. }
  1284. /* release forgotten pictures */
  1285. /* if (mpeg124/h263) */
  1286. if (!s->encoding) {
  1287. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1288. if (&s->picture[i] != s->last_picture_ptr &&
  1289. &s->picture[i] != s->next_picture_ptr &&
  1290. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1291. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1292. av_log(avctx, AV_LOG_ERROR,
  1293. "releasing zombie picture\n");
  1294. ff_mpeg_unref_picture(s, &s->picture[i]);
  1295. }
  1296. }
  1297. }
  1298. ff_mpeg_unref_picture(s, &s->current_picture);
  1299. if (!s->encoding) {
  1300. ff_release_unused_pictures(s, 1);
  1301. if (s->current_picture_ptr &&
  1302. s->current_picture_ptr->f.buf[0] == NULL) {
  1303. // we already have a unused image
  1304. // (maybe it was set before reading the header)
  1305. pic = s->current_picture_ptr;
  1306. } else {
  1307. i = ff_find_unused_picture(s, 0);
  1308. if (i < 0) {
  1309. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1310. return i;
  1311. }
  1312. pic = &s->picture[i];
  1313. }
  1314. pic->reference = 0;
  1315. if (!s->droppable) {
  1316. if (s->pict_type != AV_PICTURE_TYPE_B)
  1317. pic->reference = 3;
  1318. }
  1319. pic->f.coded_picture_number = s->coded_picture_number++;
  1320. if (ff_alloc_picture(s, pic, 0) < 0)
  1321. return -1;
  1322. s->current_picture_ptr = pic;
  1323. // FIXME use only the vars from current_pic
  1324. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1325. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1326. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1327. if (s->picture_structure != PICT_FRAME)
  1328. s->current_picture_ptr->f.top_field_first =
  1329. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1330. }
  1331. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1332. !s->progressive_sequence;
  1333. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1334. }
  1335. s->current_picture_ptr->f.pict_type = s->pict_type;
  1336. // if (s->flags && CODEC_FLAG_QSCALE)
  1337. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1338. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1339. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1340. s->current_picture_ptr)) < 0)
  1341. return ret;
  1342. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1343. s->last_picture_ptr = s->next_picture_ptr;
  1344. if (!s->droppable)
  1345. s->next_picture_ptr = s->current_picture_ptr;
  1346. }
  1347. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1348. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1349. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1350. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1351. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1352. s->pict_type, s->droppable);
  1353. if ((s->last_picture_ptr == NULL ||
  1354. s->last_picture_ptr->f.buf[0] == NULL) &&
  1355. (s->pict_type != AV_PICTURE_TYPE_I ||
  1356. s->picture_structure != PICT_FRAME)) {
  1357. int h_chroma_shift, v_chroma_shift;
  1358. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1359. &h_chroma_shift, &v_chroma_shift);
  1360. if (s->pict_type != AV_PICTURE_TYPE_I)
  1361. av_log(avctx, AV_LOG_ERROR,
  1362. "warning: first frame is no keyframe\n");
  1363. else if (s->picture_structure != PICT_FRAME)
  1364. av_log(avctx, AV_LOG_INFO,
  1365. "allocate dummy last picture for field based first keyframe\n");
  1366. /* Allocate a dummy frame */
  1367. i = ff_find_unused_picture(s, 0);
  1368. if (i < 0) {
  1369. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1370. return i;
  1371. }
  1372. s->last_picture_ptr = &s->picture[i];
  1373. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1374. s->last_picture_ptr = NULL;
  1375. return -1;
  1376. }
  1377. memset(s->last_picture_ptr->f.data[0], 0,
  1378. avctx->height * s->last_picture_ptr->f.linesize[0]);
  1379. memset(s->last_picture_ptr->f.data[1], 0x80,
  1380. (avctx->height >> v_chroma_shift) *
  1381. s->last_picture_ptr->f.linesize[1]);
  1382. memset(s->last_picture_ptr->f.data[2], 0x80,
  1383. (avctx->height >> v_chroma_shift) *
  1384. s->last_picture_ptr->f.linesize[2]);
  1385. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1386. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1387. }
  1388. if ((s->next_picture_ptr == NULL ||
  1389. s->next_picture_ptr->f.buf[0] == NULL) &&
  1390. s->pict_type == AV_PICTURE_TYPE_B) {
  1391. /* Allocate a dummy frame */
  1392. i = ff_find_unused_picture(s, 0);
  1393. if (i < 0) {
  1394. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1395. return i;
  1396. }
  1397. s->next_picture_ptr = &s->picture[i];
  1398. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1399. s->next_picture_ptr = NULL;
  1400. return -1;
  1401. }
  1402. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1403. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1404. }
  1405. if (s->last_picture_ptr) {
  1406. ff_mpeg_unref_picture(s, &s->last_picture);
  1407. if (s->last_picture_ptr->f.buf[0] &&
  1408. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1409. s->last_picture_ptr)) < 0)
  1410. return ret;
  1411. }
  1412. if (s->next_picture_ptr) {
  1413. ff_mpeg_unref_picture(s, &s->next_picture);
  1414. if (s->next_picture_ptr->f.buf[0] &&
  1415. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1416. s->next_picture_ptr)) < 0)
  1417. return ret;
  1418. }
  1419. if (s->pict_type != AV_PICTURE_TYPE_I &&
  1420. !(s->last_picture_ptr && s->last_picture_ptr->f.buf[0])) {
  1421. av_log(s, AV_LOG_ERROR,
  1422. "Non-reference picture received and no reference available\n");
  1423. return AVERROR_INVALIDDATA;
  1424. }
  1425. if (s->picture_structure!= PICT_FRAME) {
  1426. int i;
  1427. for (i = 0; i < 4; i++) {
  1428. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1429. s->current_picture.f.data[i] +=
  1430. s->current_picture.f.linesize[i];
  1431. }
  1432. s->current_picture.f.linesize[i] *= 2;
  1433. s->last_picture.f.linesize[i] *= 2;
  1434. s->next_picture.f.linesize[i] *= 2;
  1435. }
  1436. }
  1437. s->err_recognition = avctx->err_recognition;
  1438. /* set dequantizer, we can't do it during init as
  1439. * it might change for mpeg4 and we can't do it in the header
  1440. * decode as init is not called for mpeg4 there yet */
  1441. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1442. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1443. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1444. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1445. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1446. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1447. } else {
  1448. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1449. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1450. }
  1451. if (s->dct_error_sum) {
  1452. assert(s->avctx->noise_reduction && s->encoding);
  1453. update_noise_reduction(s);
  1454. }
  1455. #if FF_API_XVMC
  1456. FF_DISABLE_DEPRECATION_WARNINGS
  1457. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1458. return ff_xvmc_field_start(s, avctx);
  1459. FF_ENABLE_DEPRECATION_WARNINGS
  1460. #endif /* FF_API_XVMC */
  1461. return 0;
  1462. }
  1463. /* generic function for encode/decode called after a
  1464. * frame has been coded/decoded. */
  1465. void ff_MPV_frame_end(MpegEncContext *s)
  1466. {
  1467. int i;
  1468. #if FF_API_XVMC
  1469. FF_DISABLE_DEPRECATION_WARNINGS
  1470. /* redraw edges for the frame if decoding didn't complete */
  1471. // just to make sure that all data is rendered.
  1472. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1473. ff_xvmc_field_end(s);
  1474. } else
  1475. FF_ENABLE_DEPRECATION_WARNINGS
  1476. #endif /* FF_API_XVMC */
  1477. if ((s->er.error_count || s->encoding) &&
  1478. !s->avctx->hwaccel &&
  1479. s->unrestricted_mv &&
  1480. s->current_picture.reference &&
  1481. !s->intra_only &&
  1482. !(s->flags & CODEC_FLAG_EMU_EDGE)) {
  1483. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1484. int hshift = desc->log2_chroma_w;
  1485. int vshift = desc->log2_chroma_h;
  1486. s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,
  1487. s->h_edge_pos, s->v_edge_pos,
  1488. EDGE_WIDTH, EDGE_WIDTH,
  1489. EDGE_TOP | EDGE_BOTTOM);
  1490. s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,
  1491. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1492. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1493. EDGE_TOP | EDGE_BOTTOM);
  1494. s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,
  1495. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1496. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1497. EDGE_TOP | EDGE_BOTTOM);
  1498. }
  1499. emms_c();
  1500. s->last_pict_type = s->pict_type;
  1501. s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
  1502. if (s->pict_type!= AV_PICTURE_TYPE_B) {
  1503. s->last_non_b_pict_type = s->pict_type;
  1504. }
  1505. #if 0
  1506. /* copy back current_picture variables */
  1507. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1508. if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
  1509. s->picture[i] = s->current_picture;
  1510. break;
  1511. }
  1512. }
  1513. assert(i < MAX_PICTURE_COUNT);
  1514. #endif
  1515. if (s->encoding) {
  1516. /* release non-reference frames */
  1517. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1518. if (!s->picture[i].reference)
  1519. ff_mpeg_unref_picture(s, &s->picture[i]);
  1520. }
  1521. }
  1522. // clear copies, to avoid confusion
  1523. #if 0
  1524. memset(&s->last_picture, 0, sizeof(Picture));
  1525. memset(&s->next_picture, 0, sizeof(Picture));
  1526. memset(&s->current_picture, 0, sizeof(Picture));
  1527. #endif
  1528. s->avctx->coded_frame = &s->current_picture_ptr->f;
  1529. if (s->current_picture.reference)
  1530. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1531. }
  1532. /**
  1533. * Print debugging info for the given picture.
  1534. */
  1535. void ff_print_debug_info(MpegEncContext *s, Picture *p)
  1536. {
  1537. AVFrame *pict;
  1538. if (s->avctx->hwaccel || !p || !p->mb_type)
  1539. return;
  1540. pict = &p->f;
  1541. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1542. int x,y;
  1543. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1544. switch (pict->pict_type) {
  1545. case AV_PICTURE_TYPE_I:
  1546. av_log(s->avctx,AV_LOG_DEBUG,"I\n");
  1547. break;
  1548. case AV_PICTURE_TYPE_P:
  1549. av_log(s->avctx,AV_LOG_DEBUG,"P\n");
  1550. break;
  1551. case AV_PICTURE_TYPE_B:
  1552. av_log(s->avctx,AV_LOG_DEBUG,"B\n");
  1553. break;
  1554. case AV_PICTURE_TYPE_S:
  1555. av_log(s->avctx,AV_LOG_DEBUG,"S\n");
  1556. break;
  1557. case AV_PICTURE_TYPE_SI:
  1558. av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
  1559. break;
  1560. case AV_PICTURE_TYPE_SP:
  1561. av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
  1562. break;
  1563. }
  1564. for (y = 0; y < s->mb_height; y++) {
  1565. for (x = 0; x < s->mb_width; x++) {
  1566. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1567. int count = s->mbskip_table[x + y * s->mb_stride];
  1568. if (count > 9)
  1569. count = 9;
  1570. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1571. }
  1572. if (s->avctx->debug & FF_DEBUG_QP) {
  1573. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1574. p->qscale_table[x + y * s->mb_stride]);
  1575. }
  1576. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1577. int mb_type = p->mb_type[x + y * s->mb_stride];
  1578. // Type & MV direction
  1579. if (IS_PCM(mb_type))
  1580. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1581. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1582. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1583. else if (IS_INTRA4x4(mb_type))
  1584. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1585. else if (IS_INTRA16x16(mb_type))
  1586. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1587. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1588. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1589. else if (IS_DIRECT(mb_type))
  1590. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1591. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1592. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1593. else if (IS_GMC(mb_type))
  1594. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1595. else if (IS_SKIP(mb_type))
  1596. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1597. else if (!USES_LIST(mb_type, 1))
  1598. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1599. else if (!USES_LIST(mb_type, 0))
  1600. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1601. else {
  1602. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1603. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1604. }
  1605. // segmentation
  1606. if (IS_8X8(mb_type))
  1607. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1608. else if (IS_16X8(mb_type))
  1609. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1610. else if (IS_8X16(mb_type))
  1611. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1612. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1613. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1614. else
  1615. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1616. if (IS_INTERLACED(mb_type))
  1617. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1618. else
  1619. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1620. }
  1621. }
  1622. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1623. }
  1624. }
  1625. }
  1626. /**
  1627. * find the lowest MB row referenced in the MVs
  1628. */
  1629. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1630. {
  1631. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1632. int my, off, i, mvs;
  1633. if (s->picture_structure != PICT_FRAME || s->mcsel)
  1634. goto unhandled;
  1635. switch (s->mv_type) {
  1636. case MV_TYPE_16X16:
  1637. mvs = 1;
  1638. break;
  1639. case MV_TYPE_16X8:
  1640. mvs = 2;
  1641. break;
  1642. case MV_TYPE_8X8:
  1643. mvs = 4;
  1644. break;
  1645. default:
  1646. goto unhandled;
  1647. }
  1648. for (i = 0; i < mvs; i++) {
  1649. my = s->mv[dir][i][1]<<qpel_shift;
  1650. my_max = FFMAX(my_max, my);
  1651. my_min = FFMIN(my_min, my);
  1652. }
  1653. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1654. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1655. unhandled:
  1656. return s->mb_height-1;
  1657. }
  1658. /* put block[] to dest[] */
  1659. static inline void put_dct(MpegEncContext *s,
  1660. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1661. {
  1662. s->dct_unquantize_intra(s, block, i, qscale);
  1663. s->dsp.idct_put (dest, line_size, block);
  1664. }
  1665. /* add block[] to dest[] */
  1666. static inline void add_dct(MpegEncContext *s,
  1667. int16_t *block, int i, uint8_t *dest, int line_size)
  1668. {
  1669. if (s->block_last_index[i] >= 0) {
  1670. s->dsp.idct_add (dest, line_size, block);
  1671. }
  1672. }
  1673. static inline void add_dequant_dct(MpegEncContext *s,
  1674. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1675. {
  1676. if (s->block_last_index[i] >= 0) {
  1677. s->dct_unquantize_inter(s, block, i, qscale);
  1678. s->dsp.idct_add (dest, line_size, block);
  1679. }
  1680. }
  1681. /**
  1682. * Clean dc, ac, coded_block for the current non-intra MB.
  1683. */
  1684. void ff_clean_intra_table_entries(MpegEncContext *s)
  1685. {
  1686. int wrap = s->b8_stride;
  1687. int xy = s->block_index[0];
  1688. s->dc_val[0][xy ] =
  1689. s->dc_val[0][xy + 1 ] =
  1690. s->dc_val[0][xy + wrap] =
  1691. s->dc_val[0][xy + 1 + wrap] = 1024;
  1692. /* ac pred */
  1693. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1694. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1695. if (s->msmpeg4_version>=3) {
  1696. s->coded_block[xy ] =
  1697. s->coded_block[xy + 1 ] =
  1698. s->coded_block[xy + wrap] =
  1699. s->coded_block[xy + 1 + wrap] = 0;
  1700. }
  1701. /* chroma */
  1702. wrap = s->mb_stride;
  1703. xy = s->mb_x + s->mb_y * wrap;
  1704. s->dc_val[1][xy] =
  1705. s->dc_val[2][xy] = 1024;
  1706. /* ac pred */
  1707. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1708. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1709. s->mbintra_table[xy]= 0;
  1710. }
  1711. /* generic function called after a macroblock has been parsed by the
  1712. decoder or after it has been encoded by the encoder.
  1713. Important variables used:
  1714. s->mb_intra : true if intra macroblock
  1715. s->mv_dir : motion vector direction
  1716. s->mv_type : motion vector type
  1717. s->mv : motion vector
  1718. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1719. */
  1720. static av_always_inline
  1721. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  1722. int is_mpeg12)
  1723. {
  1724. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1725. #if FF_API_XVMC
  1726. FF_DISABLE_DEPRECATION_WARNINGS
  1727. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1728. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1729. return;
  1730. }
  1731. FF_ENABLE_DEPRECATION_WARNINGS
  1732. #endif /* FF_API_XVMC */
  1733. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1734. /* print DCT coefficients */
  1735. int i,j;
  1736. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1737. for(i=0; i<6; i++){
  1738. for(j=0; j<64; j++){
  1739. av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
  1740. }
  1741. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1742. }
  1743. }
  1744. s->current_picture.qscale_table[mb_xy] = s->qscale;
  1745. /* update DC predictors for P macroblocks */
  1746. if (!s->mb_intra) {
  1747. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1748. if(s->mbintra_table[mb_xy])
  1749. ff_clean_intra_table_entries(s);
  1750. } else {
  1751. s->last_dc[0] =
  1752. s->last_dc[1] =
  1753. s->last_dc[2] = 128 << s->intra_dc_precision;
  1754. }
  1755. }
  1756. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1757. s->mbintra_table[mb_xy]=1;
  1758. 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
  1759. uint8_t *dest_y, *dest_cb, *dest_cr;
  1760. int dct_linesize, dct_offset;
  1761. op_pixels_func (*op_pix)[4];
  1762. qpel_mc_func (*op_qpix)[16];
  1763. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1764. const int uvlinesize = s->current_picture.f.linesize[1];
  1765. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
  1766. const int block_size = 8;
  1767. /* avoid copy if macroblock skipped in last frame too */
  1768. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1769. if(!s->encoding){
  1770. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1771. if (s->mb_skipped) {
  1772. s->mb_skipped= 0;
  1773. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1774. *mbskip_ptr = 1;
  1775. } else if(!s->current_picture.reference) {
  1776. *mbskip_ptr = 1;
  1777. } else{
  1778. *mbskip_ptr = 0; /* not skipped */
  1779. }
  1780. }
  1781. dct_linesize = linesize << s->interlaced_dct;
  1782. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1783. if(readable){
  1784. dest_y= s->dest[0];
  1785. dest_cb= s->dest[1];
  1786. dest_cr= s->dest[2];
  1787. }else{
  1788. dest_y = s->b_scratchpad;
  1789. dest_cb= s->b_scratchpad+16*linesize;
  1790. dest_cr= s->b_scratchpad+32*linesize;
  1791. }
  1792. if (!s->mb_intra) {
  1793. /* motion handling */
  1794. /* decoding or more than one mb_type (MC was already done otherwise) */
  1795. if(!s->encoding){
  1796. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1797. if (s->mv_dir & MV_DIR_FORWARD) {
  1798. ff_thread_await_progress(&s->last_picture_ptr->tf,
  1799. ff_MPV_lowest_referenced_row(s, 0),
  1800. 0);
  1801. }
  1802. if (s->mv_dir & MV_DIR_BACKWARD) {
  1803. ff_thread_await_progress(&s->next_picture_ptr->tf,
  1804. ff_MPV_lowest_referenced_row(s, 1),
  1805. 0);
  1806. }
  1807. }
  1808. op_qpix= s->me.qpel_put;
  1809. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1810. op_pix = s->hdsp.put_pixels_tab;
  1811. }else{
  1812. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1813. }
  1814. if (s->mv_dir & MV_DIR_FORWARD) {
  1815. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  1816. op_pix = s->hdsp.avg_pixels_tab;
  1817. op_qpix= s->me.qpel_avg;
  1818. }
  1819. if (s->mv_dir & MV_DIR_BACKWARD) {
  1820. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  1821. }
  1822. }
  1823. /* skip dequant / idct if we are really late ;) */
  1824. if(s->avctx->skip_idct){
  1825. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1826. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1827. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1828. goto skip_idct;
  1829. }
  1830. /* add dct residue */
  1831. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  1832. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1833. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1834. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1835. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1836. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1837. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1838. if (s->chroma_y_shift){
  1839. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1840. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1841. }else{
  1842. dct_linesize >>= 1;
  1843. dct_offset >>=1;
  1844. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1845. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1846. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1847. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1848. }
  1849. }
  1850. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  1851. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1852. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1853. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1854. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1855. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1856. if(s->chroma_y_shift){//Chroma420
  1857. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1858. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1859. }else{
  1860. //chroma422
  1861. dct_linesize = uvlinesize << s->interlaced_dct;
  1862. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1863. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1864. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1865. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1866. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1867. if(!s->chroma_x_shift){//Chroma444
  1868. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1869. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1870. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1871. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1872. }
  1873. }
  1874. }//fi gray
  1875. }
  1876. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1877. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1878. }
  1879. } else {
  1880. /* dct only in intra block */
  1881. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  1882. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1883. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1884. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1885. put_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. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1889. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1890. }else{
  1891. dct_offset >>=1;
  1892. dct_linesize >>=1;
  1893. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1894. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1895. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1896. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1897. }
  1898. }
  1899. }else{
  1900. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1901. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1902. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1903. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1904. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1905. if(s->chroma_y_shift){
  1906. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1907. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1908. }else{
  1909. dct_linesize = uvlinesize << s->interlaced_dct;
  1910. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1911. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1912. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1913. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1914. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1915. if(!s->chroma_x_shift){//Chroma444
  1916. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1917. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1918. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1919. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1920. }
  1921. }
  1922. }//gray
  1923. }
  1924. }
  1925. skip_idct:
  1926. if(!readable){
  1927. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1928. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1929. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1930. }
  1931. }
  1932. }
  1933. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  1934. #if !CONFIG_SMALL
  1935. if(s->out_format == FMT_MPEG1) {
  1936. MPV_decode_mb_internal(s, block, 1);
  1937. } else
  1938. #endif
  1939. MPV_decode_mb_internal(s, block, 0);
  1940. }
  1941. /**
  1942. * @param h is the normal height, this will be reduced automatically if needed for the last row
  1943. */
  1944. void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur,
  1945. Picture *last, int y, int h, int picture_structure,
  1946. int first_field, int draw_edges, int low_delay,
  1947. int v_edge_pos, int h_edge_pos)
  1948. {
  1949. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1950. int hshift = desc->log2_chroma_w;
  1951. int vshift = desc->log2_chroma_h;
  1952. const int field_pic = picture_structure != PICT_FRAME;
  1953. if(field_pic){
  1954. h <<= 1;
  1955. y <<= 1;
  1956. }
  1957. if (!avctx->hwaccel &&
  1958. draw_edges &&
  1959. cur->reference &&
  1960. !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1961. int *linesize = cur->f.linesize;
  1962. int sides = 0, edge_h;
  1963. if (y==0) sides |= EDGE_TOP;
  1964. if (y + h >= v_edge_pos)
  1965. sides |= EDGE_BOTTOM;
  1966. edge_h= FFMIN(h, v_edge_pos - y);
  1967. dsp->draw_edges(cur->f.data[0] + y * linesize[0],
  1968. linesize[0], h_edge_pos, edge_h,
  1969. EDGE_WIDTH, EDGE_WIDTH, sides);
  1970. dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1],
  1971. linesize[1], h_edge_pos >> hshift, edge_h >> vshift,
  1972. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  1973. dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2],
  1974. linesize[2], h_edge_pos >> hshift, edge_h >> vshift,
  1975. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  1976. }
  1977. h = FFMIN(h, avctx->height - y);
  1978. if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  1979. if (avctx->draw_horiz_band) {
  1980. AVFrame *src;
  1981. int offset[AV_NUM_DATA_POINTERS];
  1982. int i;
  1983. if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
  1984. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  1985. src = &cur->f;
  1986. else if (last)
  1987. src = &last->f;
  1988. else
  1989. return;
  1990. if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
  1991. picture_structure == PICT_FRAME &&
  1992. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  1993. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  1994. offset[i] = 0;
  1995. }else{
  1996. offset[0]= y * src->linesize[0];
  1997. offset[1]=
  1998. offset[2]= (y >> vshift) * src->linesize[1];
  1999. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2000. offset[i] = 0;
  2001. }
  2002. emms_c();
  2003. avctx->draw_horiz_band(avctx, src, offset,
  2004. y, picture_structure, h);
  2005. }
  2006. }
  2007. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2008. {
  2009. int draw_edges = s->unrestricted_mv && !s->intra_only;
  2010. ff_draw_horiz_band(s->avctx, &s->dsp, &s->current_picture,
  2011. &s->last_picture, y, h, s->picture_structure,
  2012. s->first_field, draw_edges, s->low_delay,
  2013. s->v_edge_pos, s->h_edge_pos);
  2014. }
  2015. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2016. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2017. const int uvlinesize = s->current_picture.f.linesize[1];
  2018. const int mb_size= 4;
  2019. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2020. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2021. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2022. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2023. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2024. 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;
  2025. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2026. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2027. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2028. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2029. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2030. {
  2031. if(s->picture_structure==PICT_FRAME){
  2032. s->dest[0] += s->mb_y * linesize << mb_size;
  2033. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2034. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2035. }else{
  2036. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2037. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2038. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2039. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2040. }
  2041. }
  2042. }
  2043. /**
  2044. * Permute an 8x8 block.
  2045. * @param block the block which will be permuted according to the given permutation vector
  2046. * @param permutation the permutation vector
  2047. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2048. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2049. * (inverse) permutated to scantable order!
  2050. */
  2051. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2052. {
  2053. int i;
  2054. int16_t temp[64];
  2055. if(last<=0) return;
  2056. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2057. for(i=0; i<=last; i++){
  2058. const int j= scantable[i];
  2059. temp[j]= block[j];
  2060. block[j]=0;
  2061. }
  2062. for(i=0; i<=last; i++){
  2063. const int j= scantable[i];
  2064. const int perm_j= permutation[j];
  2065. block[perm_j]= temp[j];
  2066. }
  2067. }
  2068. void ff_mpeg_flush(AVCodecContext *avctx){
  2069. int i;
  2070. MpegEncContext *s = avctx->priv_data;
  2071. if(s==NULL || s->picture==NULL)
  2072. return;
  2073. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2074. ff_mpeg_unref_picture(s, &s->picture[i]);
  2075. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2076. ff_mpeg_unref_picture(s, &s->current_picture);
  2077. ff_mpeg_unref_picture(s, &s->last_picture);
  2078. ff_mpeg_unref_picture(s, &s->next_picture);
  2079. s->mb_x= s->mb_y= 0;
  2080. s->parse_context.state= -1;
  2081. s->parse_context.frame_start_found= 0;
  2082. s->parse_context.overread= 0;
  2083. s->parse_context.overread_index= 0;
  2084. s->parse_context.index= 0;
  2085. s->parse_context.last_index= 0;
  2086. s->bitstream_buffer_size=0;
  2087. s->pp_time=0;
  2088. }
  2089. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2090. int16_t *block, int n, int qscale)
  2091. {
  2092. int i, level, nCoeffs;
  2093. const uint16_t *quant_matrix;
  2094. nCoeffs= s->block_last_index[n];
  2095. if (n < 4)
  2096. block[0] = block[0] * s->y_dc_scale;
  2097. else
  2098. block[0] = block[0] * s->c_dc_scale;
  2099. /* XXX: only mpeg1 */
  2100. quant_matrix = s->intra_matrix;
  2101. for(i=1;i<=nCoeffs;i++) {
  2102. int j= s->intra_scantable.permutated[i];
  2103. level = block[j];
  2104. if (level) {
  2105. if (level < 0) {
  2106. level = -level;
  2107. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2108. level = (level - 1) | 1;
  2109. level = -level;
  2110. } else {
  2111. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2112. level = (level - 1) | 1;
  2113. }
  2114. block[j] = level;
  2115. }
  2116. }
  2117. }
  2118. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2119. int16_t *block, int n, int qscale)
  2120. {
  2121. int i, level, nCoeffs;
  2122. const uint16_t *quant_matrix;
  2123. nCoeffs= s->block_last_index[n];
  2124. quant_matrix = s->inter_matrix;
  2125. for(i=0; i<=nCoeffs; i++) {
  2126. int j= s->intra_scantable.permutated[i];
  2127. level = block[j];
  2128. if (level) {
  2129. if (level < 0) {
  2130. level = -level;
  2131. level = (((level << 1) + 1) * qscale *
  2132. ((int) (quant_matrix[j]))) >> 4;
  2133. level = (level - 1) | 1;
  2134. level = -level;
  2135. } else {
  2136. level = (((level << 1) + 1) * qscale *
  2137. ((int) (quant_matrix[j]))) >> 4;
  2138. level = (level - 1) | 1;
  2139. }
  2140. block[j] = level;
  2141. }
  2142. }
  2143. }
  2144. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2145. int16_t *block, int n, int qscale)
  2146. {
  2147. int i, level, nCoeffs;
  2148. const uint16_t *quant_matrix;
  2149. if(s->alternate_scan) nCoeffs= 63;
  2150. else nCoeffs= s->block_last_index[n];
  2151. if (n < 4)
  2152. block[0] = block[0] * s->y_dc_scale;
  2153. else
  2154. block[0] = block[0] * s->c_dc_scale;
  2155. quant_matrix = s->intra_matrix;
  2156. for(i=1;i<=nCoeffs;i++) {
  2157. int j= s->intra_scantable.permutated[i];
  2158. level = block[j];
  2159. if (level) {
  2160. if (level < 0) {
  2161. level = -level;
  2162. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2163. level = -level;
  2164. } else {
  2165. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2166. }
  2167. block[j] = level;
  2168. }
  2169. }
  2170. }
  2171. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2172. int16_t *block, int n, int qscale)
  2173. {
  2174. int i, level, nCoeffs;
  2175. const uint16_t *quant_matrix;
  2176. int sum=-1;
  2177. if(s->alternate_scan) nCoeffs= 63;
  2178. else nCoeffs= s->block_last_index[n];
  2179. if (n < 4)
  2180. block[0] = block[0] * s->y_dc_scale;
  2181. else
  2182. block[0] = block[0] * s->c_dc_scale;
  2183. quant_matrix = s->intra_matrix;
  2184. for(i=1;i<=nCoeffs;i++) {
  2185. int j= s->intra_scantable.permutated[i];
  2186. level = block[j];
  2187. if (level) {
  2188. if (level < 0) {
  2189. level = -level;
  2190. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2191. level = -level;
  2192. } else {
  2193. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2194. }
  2195. block[j] = level;
  2196. sum+=level;
  2197. }
  2198. }
  2199. block[63]^=sum&1;
  2200. }
  2201. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2202. int16_t *block, int n, int qscale)
  2203. {
  2204. int i, level, nCoeffs;
  2205. const uint16_t *quant_matrix;
  2206. int sum=-1;
  2207. if(s->alternate_scan) nCoeffs= 63;
  2208. else nCoeffs= s->block_last_index[n];
  2209. quant_matrix = s->inter_matrix;
  2210. for(i=0; i<=nCoeffs; i++) {
  2211. int j= s->intra_scantable.permutated[i];
  2212. level = block[j];
  2213. if (level) {
  2214. if (level < 0) {
  2215. level = -level;
  2216. level = (((level << 1) + 1) * qscale *
  2217. ((int) (quant_matrix[j]))) >> 4;
  2218. level = -level;
  2219. } else {
  2220. level = (((level << 1) + 1) * qscale *
  2221. ((int) (quant_matrix[j]))) >> 4;
  2222. }
  2223. block[j] = level;
  2224. sum+=level;
  2225. }
  2226. }
  2227. block[63]^=sum&1;
  2228. }
  2229. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2230. int16_t *block, int n, int qscale)
  2231. {
  2232. int i, level, qmul, qadd;
  2233. int nCoeffs;
  2234. assert(s->block_last_index[n]>=0);
  2235. qmul = qscale << 1;
  2236. if (!s->h263_aic) {
  2237. if (n < 4)
  2238. block[0] = block[0] * s->y_dc_scale;
  2239. else
  2240. block[0] = block[0] * s->c_dc_scale;
  2241. qadd = (qscale - 1) | 1;
  2242. }else{
  2243. qadd = 0;
  2244. }
  2245. if(s->ac_pred)
  2246. nCoeffs=63;
  2247. else
  2248. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2249. for(i=1; i<=nCoeffs; i++) {
  2250. level = block[i];
  2251. if (level) {
  2252. if (level < 0) {
  2253. level = level * qmul - qadd;
  2254. } else {
  2255. level = level * qmul + qadd;
  2256. }
  2257. block[i] = level;
  2258. }
  2259. }
  2260. }
  2261. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2262. int16_t *block, int n, int qscale)
  2263. {
  2264. int i, level, qmul, qadd;
  2265. int nCoeffs;
  2266. assert(s->block_last_index[n]>=0);
  2267. qadd = (qscale - 1) | 1;
  2268. qmul = qscale << 1;
  2269. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2270. for(i=0; i<=nCoeffs; i++) {
  2271. level = block[i];
  2272. if (level) {
  2273. if (level < 0) {
  2274. level = level * qmul - qadd;
  2275. } else {
  2276. level = level * qmul + qadd;
  2277. }
  2278. block[i] = level;
  2279. }
  2280. }
  2281. }
  2282. /**
  2283. * set qscale and update qscale dependent variables.
  2284. */
  2285. void ff_set_qscale(MpegEncContext * s, int qscale)
  2286. {
  2287. if (qscale < 1)
  2288. qscale = 1;
  2289. else if (qscale > 31)
  2290. qscale = 31;
  2291. s->qscale = qscale;
  2292. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2293. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2294. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2295. }
  2296. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2297. {
  2298. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2299. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2300. }
  2301. #if CONFIG_ERROR_RESILIENCE
  2302. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2303. {
  2304. ERContext *er = &s->er;
  2305. er->cur_pic = s->current_picture_ptr;
  2306. er->last_pic = s->last_picture_ptr;
  2307. er->next_pic = s->next_picture_ptr;
  2308. er->pp_time = s->pp_time;
  2309. er->pb_time = s->pb_time;
  2310. er->quarter_sample = s->quarter_sample;
  2311. er->partitioned_frame = s->partitioned_frame;
  2312. ff_er_frame_start(er);
  2313. }
  2314. #endif /* CONFIG_ERROR_RESILIENCE */