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.

2673 lines
95KB

  1. /*
  2. * MPEG-1/2 decoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * MPEG-1/2 decoder
  25. */
  26. //#define DEBUG
  27. #include "internal.h"
  28. #include "avcodec.h"
  29. #include "dsputil.h"
  30. #include "mpegvideo.h"
  31. #include "libavutil/avassert.h"
  32. #include "mpeg12.h"
  33. #include "mpeg12data.h"
  34. #include "mpeg12decdata.h"
  35. #include "bytestream.h"
  36. #include "vdpau_internal.h"
  37. #include "xvmc_internal.h"
  38. #include "thread.h"
  39. //#undef NDEBUG
  40. //#include <assert.h>
  41. #define MV_VLC_BITS 9
  42. #define MBINCR_VLC_BITS 9
  43. #define MB_PAT_VLC_BITS 9
  44. #define MB_PTYPE_VLC_BITS 6
  45. #define MB_BTYPE_VLC_BITS 6
  46. static VLC mv_vlc;
  47. /* as H.263, but only 17 codes */
  48. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  49. {
  50. int code, sign, val, shift;
  51. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  52. if (code == 0) {
  53. return pred;
  54. }
  55. if (code < 0) {
  56. return 0xffff;
  57. }
  58. sign = get_bits1(&s->gb);
  59. shift = fcode - 1;
  60. val = code;
  61. if (shift) {
  62. val = (val - 1) << shift;
  63. val |= get_bits(&s->gb, shift);
  64. val++;
  65. }
  66. if (sign)
  67. val = -val;
  68. val += pred;
  69. /* modulo decoding */
  70. return sign_extend(val, 5 + shift);
  71. }
  72. static inline int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
  73. {
  74. int level, dc, diff, i, j, run;
  75. int component;
  76. RLTable *rl = &ff_rl_mpeg1;
  77. uint8_t * const scantable = s->intra_scantable.permutated;
  78. const uint16_t *quant_matrix = s->intra_matrix;
  79. const int qscale = s->qscale;
  80. /* DC coefficient */
  81. component = (n <= 3 ? 0 : n - 4 + 1);
  82. diff = decode_dc(&s->gb, component);
  83. if (diff >= 0xffff)
  84. return -1;
  85. dc = s->last_dc[component];
  86. dc += diff;
  87. s->last_dc[component] = dc;
  88. block[0] = dc * quant_matrix[0];
  89. av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
  90. i = 0;
  91. {
  92. OPEN_READER(re, &s->gb);
  93. /* now quantify & encode AC coefficients */
  94. for (;;) {
  95. UPDATE_CACHE(re, &s->gb);
  96. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  97. if (level == 127) {
  98. break;
  99. } else if (level != 0) {
  100. i += run;
  101. j = scantable[i];
  102. level = (level * qscale * quant_matrix[j]) >> 4;
  103. level = (level - 1) | 1;
  104. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  105. LAST_SKIP_BITS(re, &s->gb, 1);
  106. } else {
  107. /* escape */
  108. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  109. UPDATE_CACHE(re, &s->gb);
  110. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  111. if (level == -128) {
  112. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  113. } else if (level == 0) {
  114. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  115. }
  116. i += run;
  117. j = scantable[i];
  118. if (level < 0) {
  119. level = -level;
  120. level = (level * qscale * quant_matrix[j]) >> 4;
  121. level = (level - 1) | 1;
  122. level = -level;
  123. } else {
  124. level = (level * qscale * quant_matrix[j]) >> 4;
  125. level = (level - 1) | 1;
  126. }
  127. }
  128. if (i > 63) {
  129. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  130. return -1;
  131. }
  132. block[j] = level;
  133. }
  134. CLOSE_READER(re, &s->gb);
  135. }
  136. s->block_last_index[n] = i;
  137. return 0;
  138. }
  139. int ff_mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
  140. {
  141. return mpeg1_decode_block_intra(s, block, n);
  142. }
  143. static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
  144. {
  145. int level, i, j, run;
  146. RLTable *rl = &ff_rl_mpeg1;
  147. uint8_t * const scantable = s->intra_scantable.permutated;
  148. const uint16_t *quant_matrix = s->inter_matrix;
  149. const int qscale = s->qscale;
  150. {
  151. OPEN_READER(re, &s->gb);
  152. i = -1;
  153. // special case for first coefficient, no need to add second VLC table
  154. UPDATE_CACHE(re, &s->gb);
  155. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  156. level = (3 * qscale * quant_matrix[0]) >> 5;
  157. level = (level - 1) | 1;
  158. if (GET_CACHE(re, &s->gb) & 0x40000000)
  159. level = -level;
  160. block[0] = level;
  161. i++;
  162. SKIP_BITS(re, &s->gb, 2);
  163. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  164. goto end;
  165. }
  166. /* now quantify & encode AC coefficients */
  167. for (;;) {
  168. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  169. if (level != 0) {
  170. i += run;
  171. j = scantable[i];
  172. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  173. level = (level - 1) | 1;
  174. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  175. SKIP_BITS(re, &s->gb, 1);
  176. } else {
  177. /* escape */
  178. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  179. UPDATE_CACHE(re, &s->gb);
  180. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  181. if (level == -128) {
  182. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  183. } else if (level == 0) {
  184. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  185. }
  186. i += run;
  187. j = scantable[i];
  188. if (level < 0) {
  189. level = -level;
  190. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  191. level = (level - 1) | 1;
  192. level = -level;
  193. } else {
  194. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  195. level = (level - 1) | 1;
  196. }
  197. }
  198. if (i > 63) {
  199. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  200. return -1;
  201. }
  202. block[j] = level;
  203. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  204. break;
  205. UPDATE_CACHE(re, &s->gb);
  206. }
  207. end:
  208. LAST_SKIP_BITS(re, &s->gb, 2);
  209. CLOSE_READER(re, &s->gb);
  210. }
  211. s->block_last_index[n] = i;
  212. return 0;
  213. }
  214. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
  215. {
  216. int level, i, j, run;
  217. RLTable *rl = &ff_rl_mpeg1;
  218. uint8_t * const scantable = s->intra_scantable.permutated;
  219. const int qscale = s->qscale;
  220. {
  221. OPEN_READER(re, &s->gb);
  222. i = -1;
  223. // special case for first coefficient, no need to add second VLC table
  224. UPDATE_CACHE(re, &s->gb);
  225. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  226. level = (3 * qscale) >> 1;
  227. level = (level - 1) | 1;
  228. if (GET_CACHE(re, &s->gb) & 0x40000000)
  229. level = -level;
  230. block[0] = level;
  231. i++;
  232. SKIP_BITS(re, &s->gb, 2);
  233. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  234. goto end;
  235. }
  236. /* now quantify & encode AC coefficients */
  237. for (;;) {
  238. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  239. if (level != 0) {
  240. i += run;
  241. j = scantable[i];
  242. level = ((level * 2 + 1) * qscale) >> 1;
  243. level = (level - 1) | 1;
  244. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  245. SKIP_BITS(re, &s->gb, 1);
  246. } else {
  247. /* escape */
  248. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  249. UPDATE_CACHE(re, &s->gb);
  250. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  251. if (level == -128) {
  252. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  253. } else if (level == 0) {
  254. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  255. }
  256. i += run;
  257. j = scantable[i];
  258. if (level < 0) {
  259. level = -level;
  260. level = ((level * 2 + 1) * qscale) >> 1;
  261. level = (level - 1) | 1;
  262. level = -level;
  263. } else {
  264. level = ((level * 2 + 1) * qscale) >> 1;
  265. level = (level - 1) | 1;
  266. }
  267. }
  268. block[j] = level;
  269. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  270. break;
  271. UPDATE_CACHE(re, &s->gb);
  272. }
  273. end:
  274. LAST_SKIP_BITS(re, &s->gb, 2);
  275. CLOSE_READER(re, &s->gb);
  276. }
  277. s->block_last_index[n] = i;
  278. return 0;
  279. }
  280. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n)
  281. {
  282. int level, i, j, run;
  283. RLTable *rl = &ff_rl_mpeg1;
  284. uint8_t * const scantable = s->intra_scantable.permutated;
  285. const uint16_t *quant_matrix;
  286. const int qscale = s->qscale;
  287. int mismatch;
  288. mismatch = 1;
  289. {
  290. OPEN_READER(re, &s->gb);
  291. i = -1;
  292. if (n < 4)
  293. quant_matrix = s->inter_matrix;
  294. else
  295. quant_matrix = s->chroma_inter_matrix;
  296. // special case for first coefficient, no need to add second VLC table
  297. UPDATE_CACHE(re, &s->gb);
  298. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  299. level= (3 * qscale * quant_matrix[0]) >> 5;
  300. if (GET_CACHE(re, &s->gb) & 0x40000000)
  301. level = -level;
  302. block[0] = level;
  303. mismatch ^= level;
  304. i++;
  305. SKIP_BITS(re, &s->gb, 2);
  306. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  307. goto end;
  308. }
  309. /* now quantify & encode AC coefficients */
  310. for (;;) {
  311. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  312. if (level != 0) {
  313. i += run;
  314. j = scantable[i];
  315. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  316. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  317. SKIP_BITS(re, &s->gb, 1);
  318. } else {
  319. /* escape */
  320. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  321. UPDATE_CACHE(re, &s->gb);
  322. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  323. i += run;
  324. j = scantable[i];
  325. if (level < 0) {
  326. level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  327. level = -level;
  328. } else {
  329. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  330. }
  331. }
  332. if (i > 63) {
  333. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  334. return -1;
  335. }
  336. mismatch ^= level;
  337. block[j] = level;
  338. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  339. break;
  340. UPDATE_CACHE(re, &s->gb);
  341. }
  342. end:
  343. LAST_SKIP_BITS(re, &s->gb, 2);
  344. CLOSE_READER(re, &s->gb);
  345. }
  346. block[63] ^= (mismatch & 1);
  347. s->block_last_index[n] = i;
  348. return 0;
  349. }
  350. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
  351. DCTELEM *block, int n)
  352. {
  353. int level, i, j, run;
  354. RLTable *rl = &ff_rl_mpeg1;
  355. uint8_t * const scantable = s->intra_scantable.permutated;
  356. const int qscale = s->qscale;
  357. OPEN_READER(re, &s->gb);
  358. i = -1;
  359. // special case for first coefficient, no need to add second VLC table
  360. UPDATE_CACHE(re, &s->gb);
  361. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  362. level = (3 * qscale) >> 1;
  363. if (GET_CACHE(re, &s->gb) & 0x40000000)
  364. level = -level;
  365. block[0] = level;
  366. i++;
  367. SKIP_BITS(re, &s->gb, 2);
  368. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  369. goto end;
  370. }
  371. /* now quantify & encode AC coefficients */
  372. for (;;) {
  373. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  374. if (level != 0) {
  375. i += run;
  376. j = scantable[i];
  377. level = ((level * 2 + 1) * qscale) >> 1;
  378. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  379. SKIP_BITS(re, &s->gb, 1);
  380. } else {
  381. /* escape */
  382. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  383. UPDATE_CACHE(re, &s->gb);
  384. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  385. i += run;
  386. j = scantable[i];
  387. if (level < 0) {
  388. level = ((-level * 2 + 1) * qscale) >> 1;
  389. level = -level;
  390. } else {
  391. level = ((level * 2 + 1) * qscale) >> 1;
  392. }
  393. }
  394. block[j] = level;
  395. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  396. break;
  397. UPDATE_CACHE(re, &s->gb);
  398. }
  399. end:
  400. LAST_SKIP_BITS(re, &s->gb, 2);
  401. CLOSE_READER(re, &s->gb);
  402. s->block_last_index[n] = i;
  403. return 0;
  404. }
  405. static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
  406. {
  407. int level, dc, diff, i, j, run;
  408. int component;
  409. RLTable *rl;
  410. uint8_t * const scantable = s->intra_scantable.permutated;
  411. const uint16_t *quant_matrix;
  412. const int qscale = s->qscale;
  413. int mismatch;
  414. /* DC coefficient */
  415. if (n < 4) {
  416. quant_matrix = s->intra_matrix;
  417. component = 0;
  418. } else {
  419. quant_matrix = s->chroma_intra_matrix;
  420. component = (n & 1) + 1;
  421. }
  422. diff = decode_dc(&s->gb, component);
  423. if (diff >= 0xffff)
  424. return -1;
  425. dc = s->last_dc[component];
  426. dc += diff;
  427. s->last_dc[component] = dc;
  428. block[0] = dc << (3 - s->intra_dc_precision);
  429. av_dlog(s->avctx, "dc=%d\n", block[0]);
  430. mismatch = block[0] ^ 1;
  431. i = 0;
  432. if (s->intra_vlc_format)
  433. rl = &ff_rl_mpeg2;
  434. else
  435. rl = &ff_rl_mpeg1;
  436. {
  437. OPEN_READER(re, &s->gb);
  438. /* now quantify & encode AC coefficients */
  439. for (;;) {
  440. UPDATE_CACHE(re, &s->gb);
  441. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  442. if (level == 127) {
  443. break;
  444. } else if (level != 0) {
  445. i += run;
  446. j = scantable[i];
  447. level = (level * qscale * quant_matrix[j]) >> 4;
  448. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  449. LAST_SKIP_BITS(re, &s->gb, 1);
  450. } else {
  451. /* escape */
  452. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  453. UPDATE_CACHE(re, &s->gb);
  454. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  455. i += run;
  456. j = scantable[i];
  457. if (level < 0) {
  458. level = (-level * qscale * quant_matrix[j]) >> 4;
  459. level = -level;
  460. } else {
  461. level = (level * qscale * quant_matrix[j]) >> 4;
  462. }
  463. }
  464. if (i > 63) {
  465. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  466. return -1;
  467. }
  468. mismatch ^= level;
  469. block[j] = level;
  470. }
  471. CLOSE_READER(re, &s->gb);
  472. }
  473. block[63] ^= mismatch & 1;
  474. s->block_last_index[n] = i;
  475. return 0;
  476. }
  477. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
  478. {
  479. int level, dc, diff, j, run;
  480. int component;
  481. RLTable *rl;
  482. uint8_t * scantable = s->intra_scantable.permutated;
  483. const uint16_t *quant_matrix;
  484. const int qscale = s->qscale;
  485. /* DC coefficient */
  486. if (n < 4) {
  487. quant_matrix = s->intra_matrix;
  488. component = 0;
  489. } else {
  490. quant_matrix = s->chroma_intra_matrix;
  491. component = (n & 1) + 1;
  492. }
  493. diff = decode_dc(&s->gb, component);
  494. if (diff >= 0xffff)
  495. return -1;
  496. dc = s->last_dc[component];
  497. dc += diff;
  498. s->last_dc[component] = dc;
  499. block[0] = dc << (3 - s->intra_dc_precision);
  500. if (s->intra_vlc_format)
  501. rl = &ff_rl_mpeg2;
  502. else
  503. rl = &ff_rl_mpeg1;
  504. {
  505. OPEN_READER(re, &s->gb);
  506. /* now quantify & encode AC coefficients */
  507. for (;;) {
  508. UPDATE_CACHE(re, &s->gb);
  509. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  510. if (level == 127) {
  511. break;
  512. } else if (level != 0) {
  513. scantable += run;
  514. j = *scantable;
  515. level = (level * qscale * quant_matrix[j]) >> 4;
  516. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  517. LAST_SKIP_BITS(re, &s->gb, 1);
  518. } else {
  519. /* escape */
  520. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  521. UPDATE_CACHE(re, &s->gb);
  522. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  523. scantable += run;
  524. j = *scantable;
  525. if (level < 0) {
  526. level = (-level * qscale * quant_matrix[j]) >> 4;
  527. level = -level;
  528. } else {
  529. level = (level * qscale * quant_matrix[j]) >> 4;
  530. }
  531. }
  532. block[j] = level;
  533. }
  534. CLOSE_READER(re, &s->gb);
  535. }
  536. s->block_last_index[n] = scantable - s->intra_scantable.permutated;
  537. return 0;
  538. }
  539. uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
  540. #define INIT_2D_VLC_RL(rl, static_size)\
  541. {\
  542. static RL_VLC_ELEM rl_vlc_table[static_size];\
  543. INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
  544. &rl.table_vlc[0][1], 4, 2,\
  545. &rl.table_vlc[0][0], 4, 2, static_size);\
  546. \
  547. rl.rl_vlc[0] = rl_vlc_table;\
  548. init_2d_vlc_rl(&rl);\
  549. }
  550. static void init_2d_vlc_rl(RLTable *rl)
  551. {
  552. int i;
  553. for (i = 0; i < rl->vlc.table_size; i++) {
  554. int code = rl->vlc.table[i][0];
  555. int len = rl->vlc.table[i][1];
  556. int level, run;
  557. if (len == 0) { // illegal code
  558. run = 65;
  559. level = MAX_LEVEL;
  560. } else if (len<0) { //more bits needed
  561. run = 0;
  562. level = code;
  563. } else {
  564. if (code == rl->n) { //esc
  565. run = 65;
  566. level = 0;
  567. } else if (code == rl->n+1) { //eob
  568. run = 0;
  569. level = 127;
  570. } else {
  571. run = rl->table_run [code] + 1;
  572. level = rl->table_level[code];
  573. }
  574. }
  575. rl->rl_vlc[0][i].len = len;
  576. rl->rl_vlc[0][i].level = level;
  577. rl->rl_vlc[0][i].run = run;
  578. }
  579. }
  580. void ff_mpeg12_common_init(MpegEncContext *s)
  581. {
  582. s->y_dc_scale_table =
  583. s->c_dc_scale_table = ff_mpeg2_dc_scale_table[s->intra_dc_precision];
  584. }
  585. void ff_mpeg1_clean_buffers(MpegEncContext *s)
  586. {
  587. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  588. s->last_dc[1] = s->last_dc[0];
  589. s->last_dc[2] = s->last_dc[0];
  590. memset(s->last_mv, 0, sizeof(s->last_mv));
  591. }
  592. /******************************************/
  593. /* decoding */
  594. VLC ff_dc_lum_vlc;
  595. VLC ff_dc_chroma_vlc;
  596. static VLC mbincr_vlc;
  597. static VLC mb_ptype_vlc;
  598. static VLC mb_btype_vlc;
  599. static VLC mb_pat_vlc;
  600. av_cold void ff_mpeg12_init_vlcs(void)
  601. {
  602. static int done = 0;
  603. if (!done) {
  604. done = 1;
  605. INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
  606. ff_mpeg12_vlc_dc_lum_bits, 1, 1,
  607. ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
  608. INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
  609. ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
  610. ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
  611. INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
  612. &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
  613. &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
  614. INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
  615. &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
  616. &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
  617. INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
  618. &ff_mpeg12_mbPatTable[0][1], 2, 1,
  619. &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
  620. INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
  621. &table_mb_ptype[0][1], 2, 1,
  622. &table_mb_ptype[0][0], 2, 1, 64);
  623. INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
  624. &table_mb_btype[0][1], 2, 1,
  625. &table_mb_btype[0][0], 2, 1, 64);
  626. init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
  627. init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
  628. INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
  629. INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
  630. }
  631. }
  632. static inline int get_dmv(MpegEncContext *s)
  633. {
  634. if (get_bits1(&s->gb))
  635. return 1 - (get_bits1(&s->gb) << 1);
  636. else
  637. return 0;
  638. }
  639. static inline int get_qscale(MpegEncContext *s)
  640. {
  641. int qscale = get_bits(&s->gb, 5);
  642. if (s->q_scale_type) {
  643. return non_linear_qscale[qscale];
  644. } else {
  645. return qscale << 1;
  646. }
  647. }
  648. static void exchange_uv(MpegEncContext *s)
  649. {
  650. DCTELEM (*tmp)[64];
  651. tmp = s->pblocks[4];
  652. s->pblocks[4] = s->pblocks[5];
  653. s->pblocks[5] = tmp;
  654. }
  655. /* motion type (for MPEG-2) */
  656. #define MT_FIELD 1
  657. #define MT_FRAME 2
  658. #define MT_16X8 2
  659. #define MT_DMV 3
  660. static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
  661. {
  662. int i, j, k, cbp, val, mb_type, motion_type;
  663. const int mb_block_count = 4 + (1 << s->chroma_format);
  664. av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  665. assert(s->mb_skipped == 0);
  666. if (s->mb_skip_run-- != 0) {
  667. if (s->pict_type == AV_PICTURE_TYPE_P) {
  668. s->mb_skipped = 1;
  669. s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  670. } else {
  671. int mb_type;
  672. if (s->mb_x)
  673. mb_type = s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
  674. else
  675. mb_type = s->current_picture.f.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
  676. if (IS_INTRA(mb_type))
  677. return -1;
  678. s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
  679. mb_type | MB_TYPE_SKIP;
  680. // assert(s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
  681. if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
  682. s->mb_skipped = 1;
  683. }
  684. return 0;
  685. }
  686. switch (s->pict_type) {
  687. default:
  688. case AV_PICTURE_TYPE_I:
  689. if (get_bits1(&s->gb) == 0) {
  690. if (get_bits1(&s->gb) == 0) {
  691. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
  692. return -1;
  693. }
  694. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  695. } else {
  696. mb_type = MB_TYPE_INTRA;
  697. }
  698. break;
  699. case AV_PICTURE_TYPE_P:
  700. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  701. if (mb_type < 0) {
  702. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  703. return -1;
  704. }
  705. mb_type = ptype2mb_type[mb_type];
  706. break;
  707. case AV_PICTURE_TYPE_B:
  708. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  709. if (mb_type < 0) {
  710. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  711. return -1;
  712. }
  713. mb_type = btype2mb_type[mb_type];
  714. break;
  715. }
  716. av_dlog(s->avctx, "mb_type=%x\n", mb_type);
  717. // motion_type = 0; /* avoid warning */
  718. if (IS_INTRA(mb_type)) {
  719. s->dsp.clear_blocks(s->block[0]);
  720. if (!s->chroma_y_shift) {
  721. s->dsp.clear_blocks(s->block[6]);
  722. }
  723. /* compute DCT type */
  724. if (s->picture_structure == PICT_FRAME && // FIXME add an interlaced_dct coded var?
  725. !s->frame_pred_frame_dct) {
  726. s->interlaced_dct = get_bits1(&s->gb);
  727. }
  728. if (IS_QUANT(mb_type))
  729. s->qscale = get_qscale(s);
  730. if (s->concealment_motion_vectors) {
  731. /* just parse them */
  732. if (s->picture_structure != PICT_FRAME)
  733. skip_bits1(&s->gb); /* field select */
  734. s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
  735. mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
  736. s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
  737. mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
  738. skip_bits1(&s->gb); /* marker */
  739. } else
  740. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  741. s->mb_intra = 1;
  742. // if 1, we memcpy blocks in xvmcvideo
  743. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
  744. ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
  745. if (s->swap_uv) {
  746. exchange_uv(s);
  747. }
  748. }
  749. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  750. if (s->flags2 & CODEC_FLAG2_FAST) {
  751. for (i = 0; i < 6; i++) {
  752. mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
  753. }
  754. } else {
  755. for (i = 0; i < mb_block_count; i++) {
  756. if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
  757. return -1;
  758. }
  759. }
  760. } else {
  761. for (i = 0; i < 6; i++) {
  762. if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
  763. return -1;
  764. }
  765. }
  766. } else {
  767. if (mb_type & MB_TYPE_ZERO_MV) {
  768. assert(mb_type & MB_TYPE_CBP);
  769. s->mv_dir = MV_DIR_FORWARD;
  770. if (s->picture_structure == PICT_FRAME) {
  771. if (!s->frame_pred_frame_dct)
  772. s->interlaced_dct = get_bits1(&s->gb);
  773. s->mv_type = MV_TYPE_16X16;
  774. } else {
  775. s->mv_type = MV_TYPE_FIELD;
  776. mb_type |= MB_TYPE_INTERLACED;
  777. s->field_select[0][0] = s->picture_structure - 1;
  778. }
  779. if (IS_QUANT(mb_type))
  780. s->qscale = get_qscale(s);
  781. s->last_mv[0][0][0] = 0;
  782. s->last_mv[0][0][1] = 0;
  783. s->last_mv[0][1][0] = 0;
  784. s->last_mv[0][1][1] = 0;
  785. s->mv[0][0][0] = 0;
  786. s->mv[0][0][1] = 0;
  787. } else {
  788. assert(mb_type & MB_TYPE_L0L1);
  789. // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  790. /* get additional motion vector type */
  791. if (s->frame_pred_frame_dct)
  792. motion_type = MT_FRAME;
  793. else {
  794. motion_type = get_bits(&s->gb, 2);
  795. if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
  796. s->interlaced_dct = get_bits1(&s->gb);
  797. }
  798. if (IS_QUANT(mb_type))
  799. s->qscale = get_qscale(s);
  800. /* motion vectors */
  801. s->mv_dir = (mb_type >> 13) & 3;
  802. av_dlog(s->avctx, "motion_type=%d\n", motion_type);
  803. switch (motion_type) {
  804. case MT_FRAME: /* or MT_16X8 */
  805. if (s->picture_structure == PICT_FRAME) {
  806. mb_type |= MB_TYPE_16x16;
  807. s->mv_type = MV_TYPE_16X16;
  808. for (i = 0; i < 2; i++) {
  809. if (USES_LIST(mb_type, i)) {
  810. /* MT_FRAME */
  811. s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
  812. mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
  813. s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
  814. mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
  815. /* full_pel: only for MPEG-1 */
  816. if (s->full_pel[i]) {
  817. s->mv[i][0][0] <<= 1;
  818. s->mv[i][0][1] <<= 1;
  819. }
  820. }
  821. }
  822. } else {
  823. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  824. s->mv_type = MV_TYPE_16X8;
  825. for (i = 0; i < 2; i++) {
  826. if (USES_LIST(mb_type, i)) {
  827. /* MT_16X8 */
  828. for (j = 0; j < 2; j++) {
  829. s->field_select[i][j] = get_bits1(&s->gb);
  830. for (k = 0; k < 2; k++) {
  831. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  832. s->last_mv[i][j][k]);
  833. s->last_mv[i][j][k] = val;
  834. s->mv[i][j][k] = val;
  835. }
  836. }
  837. }
  838. }
  839. }
  840. break;
  841. case MT_FIELD:
  842. if(s->progressive_sequence){
  843. av_log(s->avctx, AV_LOG_ERROR, "MT_FIELD in progressive_sequence\n");
  844. return -1;
  845. }
  846. s->mv_type = MV_TYPE_FIELD;
  847. if (s->picture_structure == PICT_FRAME) {
  848. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  849. for (i = 0; i < 2; i++) {
  850. if (USES_LIST(mb_type, i)) {
  851. for (j = 0; j < 2; j++) {
  852. s->field_select[i][j] = get_bits1(&s->gb);
  853. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  854. s->last_mv[i][j][0]);
  855. s->last_mv[i][j][0] = val;
  856. s->mv[i][j][0] = val;
  857. av_dlog(s->avctx, "fmx=%d\n", val);
  858. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  859. s->last_mv[i][j][1] >> 1);
  860. s->last_mv[i][j][1] = val << 1;
  861. s->mv[i][j][1] = val;
  862. av_dlog(s->avctx, "fmy=%d\n", val);
  863. }
  864. }
  865. }
  866. } else {
  867. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  868. for (i = 0; i < 2; i++) {
  869. if (USES_LIST(mb_type, i)) {
  870. s->field_select[i][0] = get_bits1(&s->gb);
  871. for (k = 0; k < 2; k++) {
  872. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  873. s->last_mv[i][0][k]);
  874. s->last_mv[i][0][k] = val;
  875. s->last_mv[i][1][k] = val;
  876. s->mv[i][0][k] = val;
  877. }
  878. }
  879. }
  880. }
  881. break;
  882. case MT_DMV:
  883. if(s->progressive_sequence){
  884. av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
  885. return -1;
  886. }
  887. s->mv_type = MV_TYPE_DMV;
  888. for (i = 0; i < 2; i++) {
  889. if (USES_LIST(mb_type, i)) {
  890. int dmx, dmy, mx, my, m;
  891. const int my_shift = s->picture_structure == PICT_FRAME;
  892. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  893. s->last_mv[i][0][0]);
  894. s->last_mv[i][0][0] = mx;
  895. s->last_mv[i][1][0] = mx;
  896. dmx = get_dmv(s);
  897. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  898. s->last_mv[i][0][1] >> my_shift);
  899. dmy = get_dmv(s);
  900. s->last_mv[i][0][1] = my << my_shift;
  901. s->last_mv[i][1][1] = my << my_shift;
  902. s->mv[i][0][0] = mx;
  903. s->mv[i][0][1] = my;
  904. s->mv[i][1][0] = mx; // not used
  905. s->mv[i][1][1] = my; // not used
  906. if (s->picture_structure == PICT_FRAME) {
  907. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  908. // m = 1 + 2 * s->top_field_first;
  909. m = s->top_field_first ? 1 : 3;
  910. /* top -> top pred */
  911. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  912. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  913. m = 4 - m;
  914. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  915. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  916. } else {
  917. mb_type |= MB_TYPE_16x16;
  918. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  919. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  920. if (s->picture_structure == PICT_TOP_FIELD)
  921. s->mv[i][2][1]--;
  922. else
  923. s->mv[i][2][1]++;
  924. }
  925. }
  926. }
  927. break;
  928. default:
  929. av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  930. return -1;
  931. }
  932. }
  933. s->mb_intra = 0;
  934. if (HAS_CBP(mb_type)) {
  935. s->dsp.clear_blocks(s->block[0]);
  936. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  937. if (mb_block_count > 6) {
  938. cbp <<= mb_block_count - 6;
  939. cbp |= get_bits(&s->gb, mb_block_count - 6);
  940. s->dsp.clear_blocks(s->block[6]);
  941. }
  942. if (cbp <= 0) {
  943. av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  944. return -1;
  945. }
  946. //if 1, we memcpy blocks in xvmcvideo
  947. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
  948. ff_xvmc_pack_pblocks(s, cbp);
  949. if (s->swap_uv) {
  950. exchange_uv(s);
  951. }
  952. }
  953. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  954. if (s->flags2 & CODEC_FLAG2_FAST) {
  955. for (i = 0; i < 6; i++) {
  956. if (cbp & 32) {
  957. mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
  958. } else {
  959. s->block_last_index[i] = -1;
  960. }
  961. cbp += cbp;
  962. }
  963. } else {
  964. cbp <<= 12-mb_block_count;
  965. for (i = 0; i < mb_block_count; i++) {
  966. if (cbp & (1 << 11)) {
  967. if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
  968. return -1;
  969. } else {
  970. s->block_last_index[i] = -1;
  971. }
  972. cbp += cbp;
  973. }
  974. }
  975. } else {
  976. if (s->flags2 & CODEC_FLAG2_FAST) {
  977. for (i = 0; i < 6; i++) {
  978. if (cbp & 32) {
  979. mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
  980. } else {
  981. s->block_last_index[i] = -1;
  982. }
  983. cbp += cbp;
  984. }
  985. } else {
  986. for (i = 0; i < 6; i++) {
  987. if (cbp & 32) {
  988. if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
  989. return -1;
  990. } else {
  991. s->block_last_index[i] = -1;
  992. }
  993. cbp += cbp;
  994. }
  995. }
  996. }
  997. } else {
  998. for (i = 0; i < 12; i++)
  999. s->block_last_index[i] = -1;
  1000. }
  1001. }
  1002. s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
  1003. return 0;
  1004. }
  1005. typedef struct Mpeg1Context {
  1006. MpegEncContext mpeg_enc_ctx;
  1007. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1008. int repeat_field; /* true if we must repeat the field */
  1009. AVPanScan pan_scan; /**< some temporary storage for the panscan */
  1010. int slice_count;
  1011. int swap_uv;//indicate VCR2
  1012. int save_aspect_info;
  1013. int save_width, save_height, save_progressive_seq;
  1014. AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
  1015. int sync; ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
  1016. int tmpgexs;
  1017. } Mpeg1Context;
  1018. static av_cold int mpeg_decode_init(AVCodecContext *avctx)
  1019. {
  1020. Mpeg1Context *s = avctx->priv_data;
  1021. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1022. int i;
  1023. /* we need some permutation to store matrices,
  1024. * until MPV_common_init() sets the real permutation. */
  1025. for (i = 0; i < 64; i++)
  1026. s2->dsp.idct_permutation[i]=i;
  1027. MPV_decode_defaults(s2);
  1028. s->mpeg_enc_ctx.avctx = avctx;
  1029. s->mpeg_enc_ctx.flags = avctx->flags;
  1030. s->mpeg_enc_ctx.flags2 = avctx->flags2;
  1031. ff_mpeg12_common_init(&s->mpeg_enc_ctx);
  1032. ff_mpeg12_init_vlcs();
  1033. s->mpeg_enc_ctx_allocated = 0;
  1034. s->mpeg_enc_ctx.picture_number = 0;
  1035. s->repeat_field = 0;
  1036. s->mpeg_enc_ctx.codec_id = avctx->codec->id;
  1037. avctx->color_range = AVCOL_RANGE_MPEG;
  1038. if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
  1039. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1040. else
  1041. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1042. return 0;
  1043. }
  1044. static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
  1045. {
  1046. Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
  1047. MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
  1048. int err;
  1049. if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
  1050. return 0;
  1051. err = ff_mpeg_update_thread_context(avctx, avctx_from);
  1052. if (err) return err;
  1053. if (!ctx->mpeg_enc_ctx_allocated)
  1054. memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
  1055. if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
  1056. s->picture_number++;
  1057. return 0;
  1058. }
  1059. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1060. const uint8_t *new_perm)
  1061. {
  1062. uint16_t temp_matrix[64];
  1063. int i;
  1064. memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
  1065. for (i = 0; i < 64; i++) {
  1066. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1067. }
  1068. }
  1069. static const enum PixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
  1070. #if CONFIG_MPEG_XVMC_DECODER
  1071. PIX_FMT_XVMC_MPEG2_IDCT,
  1072. PIX_FMT_XVMC_MPEG2_MC,
  1073. #endif
  1074. #if CONFIG_MPEG1_VDPAU_HWACCEL
  1075. PIX_FMT_VDPAU_MPEG1,
  1076. #endif
  1077. PIX_FMT_YUV420P,
  1078. PIX_FMT_NONE
  1079. };
  1080. static const enum PixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
  1081. #if CONFIG_MPEG_XVMC_DECODER
  1082. PIX_FMT_XVMC_MPEG2_IDCT,
  1083. PIX_FMT_XVMC_MPEG2_MC,
  1084. #endif
  1085. #if CONFIG_MPEG2_VDPAU_HWACCEL
  1086. PIX_FMT_VDPAU_MPEG2,
  1087. #endif
  1088. #if CONFIG_MPEG2_DXVA2_HWACCEL
  1089. PIX_FMT_DXVA2_VLD,
  1090. #endif
  1091. #if CONFIG_MPEG2_VAAPI_HWACCEL
  1092. PIX_FMT_VAAPI_VLD,
  1093. #endif
  1094. PIX_FMT_YUV420P,
  1095. PIX_FMT_NONE
  1096. };
  1097. static inline int uses_vdpau(AVCodecContext *avctx) {
  1098. return avctx->pix_fmt == PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == PIX_FMT_VDPAU_MPEG2;
  1099. }
  1100. static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
  1101. {
  1102. Mpeg1Context *s1 = avctx->priv_data;
  1103. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1104. if(s->chroma_format < 2) {
  1105. enum PixelFormat res;
  1106. res = avctx->get_format(avctx,
  1107. avctx->codec_id == CODEC_ID_MPEG1VIDEO ?
  1108. mpeg1_hwaccel_pixfmt_list_420 :
  1109. mpeg2_hwaccel_pixfmt_list_420);
  1110. if (res != PIX_FMT_XVMC_MPEG2_IDCT && res != PIX_FMT_XVMC_MPEG2_MC) {
  1111. avctx->xvmc_acceleration = 0;
  1112. } else if (!avctx->xvmc_acceleration) {
  1113. avctx->xvmc_acceleration = 2;
  1114. }
  1115. return res;
  1116. } else if(s->chroma_format == 2)
  1117. return PIX_FMT_YUV422P;
  1118. else
  1119. return PIX_FMT_YUV444P;
  1120. }
  1121. /* Call this function when we know all parameters.
  1122. * It may be called in different places for MPEG-1 and MPEG-2. */
  1123. static int mpeg_decode_postinit(AVCodecContext *avctx)
  1124. {
  1125. Mpeg1Context *s1 = avctx->priv_data;
  1126. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1127. uint8_t old_permutation[64];
  1128. if ((s1->mpeg_enc_ctx_allocated == 0) ||
  1129. avctx->coded_width != s->width ||
  1130. avctx->coded_height != s->height ||
  1131. s1->save_width != s->width ||
  1132. s1->save_height != s->height ||
  1133. s1->save_aspect_info != s->aspect_ratio_info ||
  1134. s1->save_progressive_seq != s->progressive_sequence ||
  1135. 0)
  1136. {
  1137. if (s1->mpeg_enc_ctx_allocated) {
  1138. ParseContext pc = s->parse_context;
  1139. s->parse_context.buffer = 0;
  1140. MPV_common_end(s);
  1141. s->parse_context = pc;
  1142. }
  1143. if ((s->width == 0) || (s->height == 0))
  1144. return -2;
  1145. avcodec_set_dimensions(avctx, s->width, s->height);
  1146. avctx->bit_rate = s->bit_rate;
  1147. s1->save_aspect_info = s->aspect_ratio_info;
  1148. s1->save_width = s->width;
  1149. s1->save_height = s->height;
  1150. s1->save_progressive_seq = s->progressive_sequence;
  1151. /* low_delay may be forced, in this case we will have B-frames
  1152. * that behave like P-frames. */
  1153. avctx->has_b_frames = !(s->low_delay);
  1154. assert((avctx->sub_id == 1) == (avctx->codec_id == CODEC_ID_MPEG1VIDEO));
  1155. if (avctx->codec_id == CODEC_ID_MPEG1VIDEO) {
  1156. //MPEG-1 fps
  1157. avctx->time_base.den = ff_frame_rate_tab[s->frame_rate_index].num;
  1158. avctx->time_base.num = ff_frame_rate_tab[s->frame_rate_index].den;
  1159. //MPEG-1 aspect
  1160. avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1161. avctx->ticks_per_frame=1;
  1162. } else {//MPEG-2
  1163. //MPEG-2 fps
  1164. av_reduce(&s->avctx->time_base.den,
  1165. &s->avctx->time_base.num,
  1166. ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
  1167. ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1168. 1 << 30);
  1169. avctx->ticks_per_frame = 2;
  1170. //MPEG-2 aspect
  1171. if (s->aspect_ratio_info > 1) {
  1172. AVRational dar =
  1173. av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1174. (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
  1175. (AVRational) {s->width, s->height});
  1176. // we ignore the spec here and guess a bit as reality does not match the spec, see for example
  1177. // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
  1178. // issue1613, 621, 562
  1179. if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
  1180. (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
  1181. s->avctx->sample_aspect_ratio =
  1182. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1183. (AVRational) {s->width, s->height});
  1184. } else {
  1185. s->avctx->sample_aspect_ratio =
  1186. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1187. (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
  1188. //issue1613 4/3 16/9 -> 16/9
  1189. //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1190. //widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1191. // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
  1192. //av_log(NULL, AV_LOG_ERROR, "A %d/%d\n", ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
  1193. //av_log(NULL, AV_LOG_ERROR, "B %d/%d\n", s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den);
  1194. }
  1195. } else {
  1196. s->avctx->sample_aspect_ratio =
  1197. ff_mpeg2_aspect[s->aspect_ratio_info];
  1198. }
  1199. } // MPEG-2
  1200. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1201. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1202. // until then pix_fmt may be changed right after codec init
  1203. if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
  1204. avctx->hwaccel )
  1205. if (avctx->idct_algo == FF_IDCT_AUTO)
  1206. avctx->idct_algo = FF_IDCT_SIMPLE;
  1207. /* Quantization matrices may need reordering
  1208. * if DCT permutation is changed. */
  1209. memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
  1210. if (MPV_common_init(s) < 0)
  1211. return -2;
  1212. quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
  1213. quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
  1214. quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
  1215. quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
  1216. s1->mpeg_enc_ctx_allocated = 1;
  1217. }
  1218. return 0;
  1219. }
  1220. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1221. const uint8_t *buf, int buf_size)
  1222. {
  1223. Mpeg1Context *s1 = avctx->priv_data;
  1224. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1225. int ref, f_code, vbv_delay;
  1226. init_get_bits(&s->gb, buf, buf_size*8);
  1227. ref = get_bits(&s->gb, 10); /* temporal ref */
  1228. s->pict_type = get_bits(&s->gb, 3);
  1229. if (s->pict_type == 0 || s->pict_type > 3)
  1230. return -1;
  1231. vbv_delay = get_bits(&s->gb, 16);
  1232. if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
  1233. s->full_pel[0] = get_bits1(&s->gb);
  1234. f_code = get_bits(&s->gb, 3);
  1235. if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
  1236. return -1;
  1237. s->mpeg_f_code[0][0] = f_code;
  1238. s->mpeg_f_code[0][1] = f_code;
  1239. }
  1240. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1241. s->full_pel[1] = get_bits1(&s->gb);
  1242. f_code = get_bits(&s->gb, 3);
  1243. if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
  1244. return -1;
  1245. s->mpeg_f_code[1][0] = f_code;
  1246. s->mpeg_f_code[1][1] = f_code;
  1247. }
  1248. s->current_picture.f.pict_type = s->pict_type;
  1249. s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1250. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1251. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1252. s->y_dc_scale = 8;
  1253. s->c_dc_scale = 8;
  1254. return 0;
  1255. }
  1256. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1257. {
  1258. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1259. int horiz_size_ext, vert_size_ext;
  1260. int bit_rate_ext;
  1261. skip_bits(&s->gb, 1); /* profile and level esc*/
  1262. s->avctx->profile = get_bits(&s->gb, 3);
  1263. s->avctx->level = get_bits(&s->gb, 4);
  1264. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1265. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1266. horiz_size_ext = get_bits(&s->gb, 2);
  1267. vert_size_ext = get_bits(&s->gb, 2);
  1268. s->width |= (horiz_size_ext << 12);
  1269. s->height |= (vert_size_ext << 12);
  1270. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1271. s->bit_rate += (bit_rate_ext << 18) * 400;
  1272. skip_bits1(&s->gb); /* marker */
  1273. s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
  1274. s->low_delay = get_bits1(&s->gb);
  1275. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1276. s->low_delay = 1;
  1277. s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
  1278. s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  1279. av_dlog(s->avctx, "sequence extension\n");
  1280. s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
  1281. s->avctx->sub_id = 2; /* indicates MPEG-2 found */
  1282. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1283. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1284. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  1285. }
  1286. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1287. {
  1288. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1289. int color_description, w, h;
  1290. skip_bits(&s->gb, 3); /* video format */
  1291. color_description = get_bits1(&s->gb);
  1292. if (color_description) {
  1293. s->avctx->color_primaries = get_bits(&s->gb, 8);
  1294. s->avctx->color_trc = get_bits(&s->gb, 8);
  1295. s->avctx->colorspace = get_bits(&s->gb, 8);
  1296. }
  1297. w = get_bits(&s->gb, 14);
  1298. skip_bits(&s->gb, 1); //marker
  1299. h = get_bits(&s->gb, 14);
  1300. // remaining 3 bits are zero padding
  1301. s1->pan_scan.width = 16 * w;
  1302. s1->pan_scan.height = 16 * h;
  1303. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1304. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1305. }
  1306. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1307. {
  1308. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1309. int i, nofco;
  1310. nofco = 1;
  1311. if (s->progressive_sequence) {
  1312. if (s->repeat_first_field) {
  1313. nofco++;
  1314. if (s->top_field_first)
  1315. nofco++;
  1316. }
  1317. } else {
  1318. if (s->picture_structure == PICT_FRAME) {
  1319. nofco++;
  1320. if (s->repeat_first_field)
  1321. nofco++;
  1322. }
  1323. }
  1324. for (i = 0; i < nofco; i++) {
  1325. s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
  1326. skip_bits(&s->gb, 1); // marker
  1327. s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
  1328. skip_bits(&s->gb, 1); // marker
  1329. }
  1330. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1331. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1332. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1333. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1334. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
  1335. }
  1336. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
  1337. {
  1338. int i;
  1339. for (i = 0; i < 64; i++) {
  1340. int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
  1341. int v = get_bits(&s->gb, 8);
  1342. if (v == 0) {
  1343. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1344. return -1;
  1345. }
  1346. if (intra && i == 0 && v != 8) {
  1347. av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
  1348. v = 8; // needed by pink.mpg / issue1046
  1349. }
  1350. matrix0[j] = v;
  1351. if (matrix1)
  1352. matrix1[j] = v;
  1353. }
  1354. return 0;
  1355. }
  1356. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1357. {
  1358. av_dlog(s->avctx, "matrix extension\n");
  1359. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1360. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1361. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
  1362. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
  1363. }
  1364. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1365. {
  1366. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1367. s->full_pel[0] = s->full_pel[1] = 0;
  1368. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1369. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1370. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1371. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1372. if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
  1373. av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
  1374. if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
  1375. if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1376. s->pict_type = AV_PICTURE_TYPE_I;
  1377. else
  1378. s->pict_type = AV_PICTURE_TYPE_P;
  1379. } else
  1380. s->pict_type = AV_PICTURE_TYPE_B;
  1381. s->current_picture.f.pict_type = s->pict_type;
  1382. s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1383. }
  1384. s->intra_dc_precision = get_bits(&s->gb, 2);
  1385. s->picture_structure = get_bits(&s->gb, 2);
  1386. s->top_field_first = get_bits1(&s->gb);
  1387. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1388. s->concealment_motion_vectors = get_bits1(&s->gb);
  1389. s->q_scale_type = get_bits1(&s->gb);
  1390. s->intra_vlc_format = get_bits1(&s->gb);
  1391. s->alternate_scan = get_bits1(&s->gb);
  1392. s->repeat_first_field = get_bits1(&s->gb);
  1393. s->chroma_420_type = get_bits1(&s->gb);
  1394. s->progressive_frame = get_bits1(&s->gb);
  1395. if (s->progressive_sequence && !s->progressive_frame) {
  1396. s->progressive_frame = 1;
  1397. av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
  1398. }
  1399. if (s->picture_structure == 0 || (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
  1400. av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
  1401. s->picture_structure = PICT_FRAME;
  1402. }
  1403. if (s->progressive_sequence && !s->frame_pred_frame_dct) {
  1404. av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
  1405. }
  1406. if (s->picture_structure == PICT_FRAME) {
  1407. s->first_field = 0;
  1408. s->v_edge_pos = 16 * s->mb_height;
  1409. } else {
  1410. s->first_field ^= 1;
  1411. s->v_edge_pos = 8 * s->mb_height;
  1412. memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
  1413. }
  1414. if (s->alternate_scan) {
  1415. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  1416. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  1417. } else {
  1418. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  1419. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  1420. }
  1421. /* composite display not parsed */
  1422. av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1423. av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1424. av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1425. av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1426. av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1427. av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1428. av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1429. av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1430. av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1431. }
  1432. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  1433. {
  1434. AVCodecContext *avctx = s->avctx;
  1435. Mpeg1Context *s1 = (Mpeg1Context*)s;
  1436. /* start frame decoding */
  1437. if (s->first_field || s->picture_structure == PICT_FRAME) {
  1438. if (MPV_frame_start(s, avctx) < 0)
  1439. return -1;
  1440. ff_er_frame_start(s);
  1441. /* first check if we must repeat the frame */
  1442. s->current_picture_ptr->f.repeat_pict = 0;
  1443. if (s->repeat_first_field) {
  1444. if (s->progressive_sequence) {
  1445. if (s->top_field_first)
  1446. s->current_picture_ptr->f.repeat_pict = 4;
  1447. else
  1448. s->current_picture_ptr->f.repeat_pict = 2;
  1449. } else if (s->progressive_frame) {
  1450. s->current_picture_ptr->f.repeat_pict = 1;
  1451. }
  1452. }
  1453. *s->current_picture_ptr->f.pan_scan = s1->pan_scan;
  1454. if (HAVE_PTHREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1455. ff_thread_finish_setup(avctx);
  1456. } else { // second field
  1457. int i;
  1458. if (!s->current_picture_ptr) {
  1459. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1460. return -1;
  1461. }
  1462. for (i = 0; i < 4; i++) {
  1463. s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
  1464. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1465. s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
  1466. }
  1467. }
  1468. }
  1469. if (avctx->hwaccel) {
  1470. if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
  1471. return -1;
  1472. }
  1473. // MPV_frame_start will call this function too,
  1474. // but we need to call it on every field
  1475. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1476. if (ff_xvmc_field_start(s, avctx) < 0)
  1477. return -1;
  1478. return 0;
  1479. }
  1480. #define DECODE_SLICE_ERROR -1
  1481. #define DECODE_SLICE_OK 0
  1482. /**
  1483. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  1484. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  1485. * DECODE_SLICE_OK if this slice is ok<br>
  1486. */
  1487. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  1488. const uint8_t **buf, int buf_size)
  1489. {
  1490. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1491. AVCodecContext *avctx = s->avctx;
  1492. const int lowres = s->avctx->lowres;
  1493. const int field_pic = s->picture_structure != PICT_FRAME;
  1494. s->resync_mb_x =
  1495. s->resync_mb_y = -1;
  1496. assert(mb_y < s->mb_height);
  1497. init_get_bits(&s->gb, *buf, buf_size * 8);
  1498. ff_mpeg1_clean_buffers(s);
  1499. s->interlaced_dct = 0;
  1500. s->qscale = get_qscale(s);
  1501. if (s->qscale == 0) {
  1502. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1503. return -1;
  1504. }
  1505. /* extra slice info */
  1506. while (get_bits1(&s->gb) != 0) {
  1507. skip_bits(&s->gb, 8);
  1508. }
  1509. s->mb_x = 0;
  1510. if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
  1511. skip_bits1(&s->gb);
  1512. } else {
  1513. for (;;) {
  1514. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1515. if (code < 0) {
  1516. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1517. return -1;
  1518. }
  1519. if (code >= 33) {
  1520. if (code == 33) {
  1521. s->mb_x += 33;
  1522. }
  1523. /* otherwise, stuffing, nothing to do */
  1524. } else {
  1525. s->mb_x += code;
  1526. break;
  1527. }
  1528. }
  1529. }
  1530. if (s->mb_x >= (unsigned)s->mb_width) {
  1531. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1532. return -1;
  1533. }
  1534. if (avctx->hwaccel) {
  1535. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1536. int start_code = -1;
  1537. buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1538. if (buf_end < *buf + buf_size)
  1539. buf_end -= 4;
  1540. s->mb_y = mb_y;
  1541. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1542. return DECODE_SLICE_ERROR;
  1543. *buf = buf_end;
  1544. return DECODE_SLICE_OK;
  1545. }
  1546. s->resync_mb_x = s->mb_x;
  1547. s->resync_mb_y = s->mb_y = mb_y;
  1548. s->mb_skip_run = 0;
  1549. ff_init_block_index(s);
  1550. if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
  1551. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1552. av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
  1553. s->qscale, s->mpeg_f_code[0][0], s->mpeg_f_code[0][1], s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
  1554. s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  1555. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1556. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1557. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1558. }
  1559. }
  1560. for (;;) {
  1561. // If 1, we memcpy blocks in xvmcvideo.
  1562. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  1563. ff_xvmc_init_block(s); // set s->block
  1564. if (mpeg_decode_mb(s, s->block) < 0)
  1565. return -1;
  1566. if (s->current_picture.f.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
  1567. const int wrap = s->b8_stride;
  1568. int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
  1569. int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
  1570. int motion_x, motion_y, dir, i;
  1571. for (i = 0; i < 2; i++) {
  1572. for (dir = 0; dir < 2; dir++) {
  1573. if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1574. motion_x = motion_y = 0;
  1575. } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
  1576. motion_x = s->mv[dir][0][0];
  1577. motion_y = s->mv[dir][0][1];
  1578. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  1579. motion_x = s->mv[dir][i][0];
  1580. motion_y = s->mv[dir][i][1];
  1581. }
  1582. s->current_picture.f.motion_val[dir][xy ][0] = motion_x;
  1583. s->current_picture.f.motion_val[dir][xy ][1] = motion_y;
  1584. s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
  1585. s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
  1586. s->current_picture.f.ref_index [dir][b8_xy ] =
  1587. s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
  1588. assert(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
  1589. }
  1590. xy += wrap;
  1591. b8_xy +=2;
  1592. }
  1593. }
  1594. s->dest[0] += 16 >> lowres;
  1595. s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
  1596. s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
  1597. MPV_decode_mb(s, s->block);
  1598. if (++s->mb_x >= s->mb_width) {
  1599. const int mb_size = 16 >> s->avctx->lowres;
  1600. ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
  1601. MPV_report_decode_progress(s);
  1602. s->mb_x = 0;
  1603. s->mb_y += 1 << field_pic;
  1604. if (s->mb_y >= s->mb_height) {
  1605. int left = get_bits_left(&s->gb);
  1606. int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
  1607. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  1608. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  1609. if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  1610. || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left > 8)) {
  1611. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  1612. return -1;
  1613. } else
  1614. goto eos;
  1615. }
  1616. ff_init_block_index(s);
  1617. }
  1618. /* skip mb handling */
  1619. if (s->mb_skip_run == -1) {
  1620. /* read increment again */
  1621. s->mb_skip_run = 0;
  1622. for (;;) {
  1623. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1624. if (code < 0) {
  1625. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1626. return -1;
  1627. }
  1628. if (code >= 33) {
  1629. if (code == 33) {
  1630. s->mb_skip_run += 33;
  1631. } else if (code == 35) {
  1632. if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
  1633. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1634. return -1;
  1635. }
  1636. goto eos; /* end of slice */
  1637. }
  1638. /* otherwise, stuffing, nothing to do */
  1639. } else {
  1640. s->mb_skip_run += code;
  1641. break;
  1642. }
  1643. }
  1644. if (s->mb_skip_run) {
  1645. int i;
  1646. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1647. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1648. return -1;
  1649. }
  1650. /* skip mb */
  1651. s->mb_intra = 0;
  1652. for (i = 0; i < 12; i++)
  1653. s->block_last_index[i] = -1;
  1654. if (s->picture_structure == PICT_FRAME)
  1655. s->mv_type = MV_TYPE_16X16;
  1656. else
  1657. s->mv_type = MV_TYPE_FIELD;
  1658. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1659. /* if P type, zero motion vector is implied */
  1660. s->mv_dir = MV_DIR_FORWARD;
  1661. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1662. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1663. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1664. s->field_select[0][0] = (s->picture_structure - 1) & 1;
  1665. } else {
  1666. /* if B type, reuse previous vectors and directions */
  1667. s->mv[0][0][0] = s->last_mv[0][0][0];
  1668. s->mv[0][0][1] = s->last_mv[0][0][1];
  1669. s->mv[1][0][0] = s->last_mv[1][0][0];
  1670. s->mv[1][0][1] = s->last_mv[1][0][1];
  1671. }
  1672. }
  1673. }
  1674. }
  1675. eos: // end of slice
  1676. *buf += (get_bits_count(&s->gb)-1)/8;
  1677. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1678. return 0;
  1679. }
  1680. static int slice_decode_thread(AVCodecContext *c, void *arg)
  1681. {
  1682. MpegEncContext *s = *(void**)arg;
  1683. const uint8_t *buf = s->gb.buffer;
  1684. int mb_y = s->start_mb_y;
  1685. const int field_pic = s->picture_structure != PICT_FRAME;
  1686. s->error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
  1687. for (;;) {
  1688. uint32_t start_code;
  1689. int ret;
  1690. ret = mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
  1691. emms_c();
  1692. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1693. //ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
  1694. if (ret < 0) {
  1695. if (c->error_recognition >= FF_ER_EXPLODE)
  1696. return ret;
  1697. if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
  1698. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR | DC_ERROR | MV_ERROR);
  1699. } else {
  1700. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END | DC_END | MV_END);
  1701. }
  1702. if (s->mb_y == s->end_mb_y)
  1703. return 0;
  1704. start_code = -1;
  1705. buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
  1706. mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
  1707. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1708. mb_y++;
  1709. if (mb_y < 0 || mb_y >= s->end_mb_y)
  1710. return -1;
  1711. }
  1712. }
  1713. /**
  1714. * Handle slice ends.
  1715. * @return 1 if it seems to be the last slice
  1716. */
  1717. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1718. {
  1719. Mpeg1Context *s1 = avctx->priv_data;
  1720. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1721. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1722. return 0;
  1723. if (s->avctx->hwaccel) {
  1724. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1725. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1726. }
  1727. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1728. ff_xvmc_field_end(s);
  1729. /* end of slice reached */
  1730. if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field && !s->first_slice) {
  1731. /* end of image */
  1732. s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_MPEG2;
  1733. ff_er_frame_end(s);
  1734. MPV_frame_end(s);
  1735. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1736. *pict = *(AVFrame*)s->current_picture_ptr;
  1737. ff_print_debug_info(s, pict);
  1738. } else {
  1739. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1740. s->picture_number++;
  1741. /* latency of 1 frame for I- and P-frames */
  1742. /* XXX: use another variable than picture_number */
  1743. if (s->last_picture_ptr != NULL) {
  1744. *pict = *(AVFrame*)s->last_picture_ptr;
  1745. ff_print_debug_info(s, pict);
  1746. }
  1747. }
  1748. return 1;
  1749. } else {
  1750. return 0;
  1751. }
  1752. }
  1753. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1754. const uint8_t *buf, int buf_size)
  1755. {
  1756. Mpeg1Context *s1 = avctx->priv_data;
  1757. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1758. int width, height;
  1759. int i, v, j;
  1760. init_get_bits(&s->gb, buf, buf_size*8);
  1761. width = get_bits(&s->gb, 12);
  1762. height = get_bits(&s->gb, 12);
  1763. if (width <= 0 || height <= 0)
  1764. return -1;
  1765. s->aspect_ratio_info = get_bits(&s->gb, 4);
  1766. if (s->aspect_ratio_info == 0) {
  1767. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1768. if (avctx->error_recognition >= FF_ER_COMPLIANT)
  1769. return -1;
  1770. }
  1771. s->frame_rate_index = get_bits(&s->gb, 4);
  1772. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  1773. return -1;
  1774. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1775. if (get_bits1(&s->gb) == 0) /* marker */
  1776. return -1;
  1777. s->width = width;
  1778. s->height = height;
  1779. s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
  1780. skip_bits(&s->gb, 1);
  1781. /* get matrix */
  1782. if (get_bits1(&s->gb)) {
  1783. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1784. } else {
  1785. for (i = 0; i < 64; i++) {
  1786. j = s->dsp.idct_permutation[i];
  1787. v = ff_mpeg1_default_intra_matrix[i];
  1788. s->intra_matrix[j] = v;
  1789. s->chroma_intra_matrix[j] = v;
  1790. }
  1791. }
  1792. if (get_bits1(&s->gb)) {
  1793. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1794. } else {
  1795. for (i = 0; i < 64; i++) {
  1796. int j = s->dsp.idct_permutation[i];
  1797. v = ff_mpeg1_default_non_intra_matrix[i];
  1798. s->inter_matrix[j] = v;
  1799. s->chroma_inter_matrix[j] = v;
  1800. }
  1801. }
  1802. if (show_bits(&s->gb, 23) != 0) {
  1803. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1804. return -1;
  1805. }
  1806. /* we set MPEG-2 parameters so that it emulates MPEG-1 */
  1807. s->progressive_sequence = 1;
  1808. s->progressive_frame = 1;
  1809. s->picture_structure = PICT_FRAME;
  1810. s->frame_pred_frame_dct = 1;
  1811. s->chroma_format = 1;
  1812. s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO;
  1813. avctx->sub_id = 1; /* indicates MPEG-1 */
  1814. s->out_format = FMT_MPEG1;
  1815. s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
  1816. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1817. s->low_delay = 1;
  1818. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1819. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1820. s->avctx->rc_buffer_size, s->bit_rate);
  1821. return 0;
  1822. }
  1823. static int vcr2_init_sequence(AVCodecContext *avctx)
  1824. {
  1825. Mpeg1Context *s1 = avctx->priv_data;
  1826. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1827. int i, v;
  1828. /* start new MPEG-1 context decoding */
  1829. s->out_format = FMT_MPEG1;
  1830. if (s1->mpeg_enc_ctx_allocated) {
  1831. MPV_common_end(s);
  1832. }
  1833. s->width = avctx->coded_width;
  1834. s->height = avctx->coded_height;
  1835. avctx->has_b_frames = 0; // true?
  1836. s->low_delay = 1;
  1837. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1838. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1839. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel )
  1840. if (avctx->idct_algo == FF_IDCT_AUTO)
  1841. avctx->idct_algo = FF_IDCT_SIMPLE;
  1842. if (MPV_common_init(s) < 0)
  1843. return -1;
  1844. exchange_uv(s); // common init reset pblocks, so we swap them here
  1845. s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
  1846. s1->mpeg_enc_ctx_allocated = 1;
  1847. for (i = 0; i < 64; i++) {
  1848. int j = s->dsp.idct_permutation[i];
  1849. v = ff_mpeg1_default_intra_matrix[i];
  1850. s->intra_matrix[j] = v;
  1851. s->chroma_intra_matrix[j] = v;
  1852. v = ff_mpeg1_default_non_intra_matrix[i];
  1853. s->inter_matrix[j] = v;
  1854. s->chroma_inter_matrix[j] = v;
  1855. }
  1856. s->progressive_sequence = 1;
  1857. s->progressive_frame = 1;
  1858. s->picture_structure = PICT_FRAME;
  1859. s->frame_pred_frame_dct = 1;
  1860. s->chroma_format = 1;
  1861. s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
  1862. avctx->sub_id = 2; /* indicates MPEG-2 */
  1863. s1->save_width = s->width;
  1864. s1->save_height = s->height;
  1865. s1->save_progressive_seq = s->progressive_sequence;
  1866. return 0;
  1867. }
  1868. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1869. const uint8_t *p, int buf_size)
  1870. {
  1871. Mpeg1Context *s = avctx->priv_data;
  1872. const uint8_t *buf_end = p + buf_size;
  1873. if(buf_size > 29){
  1874. int i;
  1875. for(i=0; i<20; i++)
  1876. if(!memcmp(p+i, "\0TMPGEXS\0", 9)){
  1877. s->tmpgexs= 1;
  1878. }
  1879. /* for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
  1880. av_log(0,0, "%c", p[i]);
  1881. }
  1882. av_log(0,0, "\n");*/
  1883. }
  1884. /* we parse the DTG active format information */
  1885. if (buf_end - p >= 5 &&
  1886. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1887. int flags = p[4];
  1888. p += 5;
  1889. if (flags & 0x80) {
  1890. /* skip event id */
  1891. p += 2;
  1892. }
  1893. if (flags & 0x40) {
  1894. if (buf_end - p < 1)
  1895. return;
  1896. avctx->dtg_active_format = p[0] & 0x0f;
  1897. }
  1898. }
  1899. }
  1900. static void mpeg_decode_gop(AVCodecContext *avctx,
  1901. const uint8_t *buf, int buf_size)
  1902. {
  1903. Mpeg1Context *s1 = avctx->priv_data;
  1904. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1905. int time_code_hours, time_code_minutes;
  1906. int time_code_seconds, time_code_pictures;
  1907. int broken_link;
  1908. init_get_bits(&s->gb, buf, buf_size*8);
  1909. skip_bits1(&s->gb); /* drop_frame_flag */
  1910. time_code_hours = get_bits(&s->gb, 5);
  1911. time_code_minutes = get_bits(&s->gb, 6);
  1912. skip_bits1(&s->gb); // marker bit
  1913. time_code_seconds = get_bits(&s->gb, 6);
  1914. time_code_pictures = get_bits(&s->gb, 6);
  1915. s->closed_gop = get_bits1(&s->gb);
  1916. /*broken_link indicate that after editing the
  1917. reference frames of the first B-Frames after GOP I-Frame
  1918. are missing (open gop)*/
  1919. broken_link = get_bits1(&s->gb);
  1920. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1921. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
  1922. time_code_hours, time_code_minutes, time_code_seconds,
  1923. time_code_pictures, s->closed_gop, broken_link);
  1924. }
  1925. /**
  1926. * Find the end of the current frame in the bitstream.
  1927. * @return the position of the first byte of the next frame, or -1
  1928. */
  1929. int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
  1930. {
  1931. int i;
  1932. uint32_t state = pc->state;
  1933. /* EOF considered as end of frame */
  1934. if (buf_size == 0)
  1935. return 0;
  1936. /*
  1937. 0 frame start -> 1/4
  1938. 1 first_SEQEXT -> 0/2
  1939. 2 first field start -> 3/0
  1940. 3 second_SEQEXT -> 2/0
  1941. 4 searching end
  1942. */
  1943. for (i = 0; i < buf_size; i++) {
  1944. assert(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
  1945. if (pc->frame_start_found & 1) {
  1946. if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
  1947. pc->frame_start_found--;
  1948. else if (state == EXT_START_CODE + 2) {
  1949. if ((buf[i] & 3) == 3)
  1950. pc->frame_start_found = 0;
  1951. else
  1952. pc->frame_start_found = (pc->frame_start_found + 1) & 3;
  1953. }
  1954. state++;
  1955. } else {
  1956. i = ff_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
  1957. if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
  1958. i++;
  1959. pc->frame_start_found = 4;
  1960. }
  1961. if (state == SEQ_END_CODE) {
  1962. pc->state=-1;
  1963. return i+1;
  1964. }
  1965. if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
  1966. pc->frame_start_found = 0;
  1967. if (pc->frame_start_found < 4 && state == EXT_START_CODE)
  1968. pc->frame_start_found++;
  1969. if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
  1970. if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
  1971. pc->frame_start_found = 0;
  1972. pc->state = -1;
  1973. return i - 3;
  1974. }
  1975. }
  1976. if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
  1977. ff_fetch_timestamp(s, i - 3, 1);
  1978. }
  1979. }
  1980. }
  1981. pc->state = state;
  1982. return END_NOT_FOUND;
  1983. }
  1984. static int decode_chunks(AVCodecContext *avctx,
  1985. AVFrame *picture, int *data_size,
  1986. const uint8_t *buf, int buf_size);
  1987. /* handle buffering and image synchronisation */
  1988. static int mpeg_decode_frame(AVCodecContext *avctx,
  1989. void *data, int *data_size,
  1990. AVPacket *avpkt)
  1991. {
  1992. const uint8_t *buf = avpkt->data;
  1993. int buf_size = avpkt->size;
  1994. Mpeg1Context *s = avctx->priv_data;
  1995. AVFrame *picture = data;
  1996. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1997. av_dlog(avctx, "fill_buffer\n");
  1998. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  1999. /* special case for last picture */
  2000. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  2001. *picture = *(AVFrame*)s2->next_picture_ptr;
  2002. s2->next_picture_ptr = NULL;
  2003. *data_size = sizeof(AVFrame);
  2004. }
  2005. return buf_size;
  2006. }
  2007. if (s2->flags & CODEC_FLAG_TRUNCATED) {
  2008. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
  2009. if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
  2010. return buf_size;
  2011. }
  2012. if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
  2013. vcr2_init_sequence(avctx);
  2014. s->slice_count = 0;
  2015. if (avctx->extradata && !avctx->frame_number) {
  2016. int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
  2017. if (ret < 0 && avctx->error_recognition >= FF_ER_EXPLODE)
  2018. return ret;
  2019. }
  2020. return decode_chunks(avctx, picture, data_size, buf, buf_size);
  2021. }
  2022. static int decode_chunks(AVCodecContext *avctx,
  2023. AVFrame *picture, int *data_size,
  2024. const uint8_t *buf, int buf_size)
  2025. {
  2026. Mpeg1Context *s = avctx->priv_data;
  2027. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2028. const uint8_t *buf_ptr = buf;
  2029. const uint8_t *buf_end = buf + buf_size;
  2030. int ret, input_size;
  2031. int last_code = 0;
  2032. for (;;) {
  2033. /* find next start code */
  2034. uint32_t start_code = -1;
  2035. buf_ptr = ff_find_start_code(buf_ptr, buf_end, &start_code);
  2036. if (start_code > 0x1ff) {
  2037. if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
  2038. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
  2039. int i;
  2040. av_assert0(avctx->thread_count > 1);
  2041. avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
  2042. for (i = 0; i < s->slice_count; i++)
  2043. s2->error_count += s2->thread_context[i]->error_count;
  2044. }
  2045. if (CONFIG_VDPAU && uses_vdpau(avctx))
  2046. ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
  2047. if (slice_end(avctx, picture)) {
  2048. if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2049. *data_size = sizeof(AVPicture);
  2050. }
  2051. }
  2052. s2->pict_type = 0;
  2053. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2054. }
  2055. input_size = buf_end - buf_ptr;
  2056. if (avctx->debug & FF_DEBUG_STARTCODE) {
  2057. av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
  2058. }
  2059. /* prepare data for next start code */
  2060. switch (start_code) {
  2061. case SEQ_START_CODE:
  2062. if (last_code == 0) {
  2063. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  2064. s->sync=1;
  2065. } else {
  2066. av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
  2067. if (avctx->error_recognition >= FF_ER_EXPLODE)
  2068. return AVERROR_INVALIDDATA;
  2069. }
  2070. break;
  2071. case PICTURE_START_CODE:
  2072. if(s->tmpgexs){
  2073. s2->intra_dc_precision= 3;
  2074. s2->intra_matrix[0]= 1;
  2075. }
  2076. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
  2077. int i;
  2078. avctx->execute(avctx, slice_decode_thread,
  2079. s2->thread_context, NULL,
  2080. s->slice_count, sizeof(void*));
  2081. for (i = 0; i < s->slice_count; i++)
  2082. s2->error_count += s2->thread_context[i]->error_count;
  2083. s->slice_count = 0;
  2084. }
  2085. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  2086. ret = mpeg_decode_postinit(avctx);
  2087. if (ret < 0) {
  2088. av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
  2089. return ret;
  2090. }
  2091. /* we have a complete image: we try to decompress it */
  2092. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  2093. s2->pict_type = 0;
  2094. s2->first_slice = 1;
  2095. last_code = PICTURE_START_CODE;
  2096. } else {
  2097. av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
  2098. if (avctx->error_recognition >= FF_ER_EXPLODE)
  2099. return AVERROR_INVALIDDATA;
  2100. }
  2101. break;
  2102. case EXT_START_CODE:
  2103. init_get_bits(&s2->gb, buf_ptr, input_size*8);
  2104. switch (get_bits(&s2->gb, 4)) {
  2105. case 0x1:
  2106. if (last_code == 0) {
  2107. mpeg_decode_sequence_extension(s);
  2108. } else {
  2109. av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
  2110. if (avctx->error_recognition >= FF_ER_EXPLODE)
  2111. return AVERROR_INVALIDDATA;
  2112. }
  2113. break;
  2114. case 0x2:
  2115. mpeg_decode_sequence_display_extension(s);
  2116. break;
  2117. case 0x3:
  2118. mpeg_decode_quant_matrix_extension(s2);
  2119. break;
  2120. case 0x7:
  2121. mpeg_decode_picture_display_extension(s);
  2122. break;
  2123. case 0x8:
  2124. if (last_code == PICTURE_START_CODE) {
  2125. mpeg_decode_picture_coding_extension(s);
  2126. } else {
  2127. av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
  2128. if (avctx->error_recognition >= FF_ER_EXPLODE)
  2129. return AVERROR_INVALIDDATA;
  2130. }
  2131. break;
  2132. }
  2133. break;
  2134. case USER_START_CODE:
  2135. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  2136. break;
  2137. case GOP_START_CODE:
  2138. if (last_code == 0) {
  2139. s2->first_field=0;
  2140. mpeg_decode_gop(avctx, buf_ptr, input_size);
  2141. s->sync=1;
  2142. } else {
  2143. av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
  2144. if (avctx->error_recognition >= FF_ER_EXPLODE)
  2145. return AVERROR_INVALIDDATA;
  2146. }
  2147. break;
  2148. default:
  2149. if (start_code >= SLICE_MIN_START_CODE &&
  2150. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  2151. const int field_pic = s2->picture_structure != PICT_FRAME;
  2152. int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  2153. last_code = SLICE_MIN_START_CODE;
  2154. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  2155. mb_y++;
  2156. if (mb_y >= s2->mb_height) {
  2157. av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2158. return -1;
  2159. }
  2160. if (s2->last_picture_ptr == NULL) {
  2161. /* Skip B-frames if we do not have reference frames and gop is not closed */
  2162. if (s2->pict_type == AV_PICTURE_TYPE_B) {
  2163. if (!s2->closed_gop)
  2164. break;
  2165. }
  2166. }
  2167. if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
  2168. s->sync=1;
  2169. if (s2->next_picture_ptr == NULL) {
  2170. /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
  2171. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
  2172. }
  2173. if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
  2174. (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
  2175. avctx->skip_frame >= AVDISCARD_ALL)
  2176. break;
  2177. if (!s->mpeg_enc_ctx_allocated)
  2178. break;
  2179. if (s2->codec_id == CODEC_ID_MPEG2VIDEO) {
  2180. if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2181. break;
  2182. }
  2183. if (!s2->pict_type) {
  2184. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2185. if (avctx->error_recognition >= FF_ER_EXPLODE)
  2186. return AVERROR_INVALIDDATA;
  2187. break;
  2188. }
  2189. if (s2->first_slice) {
  2190. s2->first_slice = 0;
  2191. if (mpeg_field_start(s2, buf, buf_size) < 0)
  2192. return -1;
  2193. }
  2194. if (!s2->current_picture_ptr) {
  2195. av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
  2196. return AVERROR_INVALIDDATA;
  2197. }
  2198. if (uses_vdpau(avctx)) {
  2199. s->slice_count++;
  2200. break;
  2201. }
  2202. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
  2203. int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
  2204. av_assert0(avctx->thread_count > 1);
  2205. if (threshold <= mb_y) {
  2206. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2207. thread_context->start_mb_y = mb_y;
  2208. thread_context->end_mb_y = s2->mb_height;
  2209. if (s->slice_count) {
  2210. s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
  2211. ff_update_duplicate_context(thread_context, s2);
  2212. }
  2213. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2214. s->slice_count++;
  2215. }
  2216. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2217. } else {
  2218. ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
  2219. emms_c();
  2220. if (ret < 0) {
  2221. if (avctx->error_recognition >= FF_ER_EXPLODE)
  2222. return ret;
  2223. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2224. ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR | DC_ERROR | MV_ERROR);
  2225. } else {
  2226. ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END | DC_END | MV_END);
  2227. }
  2228. }
  2229. }
  2230. break;
  2231. }
  2232. }
  2233. }
  2234. static void flush(AVCodecContext *avctx)
  2235. {
  2236. Mpeg1Context *s = avctx->priv_data;
  2237. s->sync=0;
  2238. ff_mpeg_flush(avctx);
  2239. }
  2240. static int mpeg_decode_end(AVCodecContext *avctx)
  2241. {
  2242. Mpeg1Context *s = avctx->priv_data;
  2243. if (s->mpeg_enc_ctx_allocated)
  2244. MPV_common_end(&s->mpeg_enc_ctx);
  2245. return 0;
  2246. }
  2247. static const AVProfile mpeg2_video_profiles[] = {
  2248. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2249. { FF_PROFILE_MPEG2_HIGH, "High" },
  2250. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2251. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2252. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2253. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2254. { FF_PROFILE_RESERVED, "Reserved" },
  2255. { FF_PROFILE_RESERVED, "Reserved" },
  2256. { FF_PROFILE_UNKNOWN },
  2257. };
  2258. AVCodec ff_mpeg1video_decoder = {
  2259. .name = "mpeg1video",
  2260. .type = AVMEDIA_TYPE_VIDEO,
  2261. .id = CODEC_ID_MPEG1VIDEO,
  2262. .priv_data_size = sizeof(Mpeg1Context),
  2263. .init = mpeg_decode_init,
  2264. .close = mpeg_decode_end,
  2265. .decode = mpeg_decode_frame,
  2266. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2267. .flush = flush,
  2268. .max_lowres = 3,
  2269. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2270. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2271. };
  2272. AVCodec ff_mpeg2video_decoder = {
  2273. .name = "mpeg2video",
  2274. .type = AVMEDIA_TYPE_VIDEO,
  2275. .id = CODEC_ID_MPEG2VIDEO,
  2276. .priv_data_size = sizeof(Mpeg1Context),
  2277. .init = mpeg_decode_init,
  2278. .close = mpeg_decode_end,
  2279. .decode = mpeg_decode_frame,
  2280. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2281. .flush = flush,
  2282. .max_lowres = 3,
  2283. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2284. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2285. };
  2286. //legacy decoder
  2287. AVCodec ff_mpegvideo_decoder = {
  2288. .name = "mpegvideo",
  2289. .type = AVMEDIA_TYPE_VIDEO,
  2290. .id = CODEC_ID_MPEG2VIDEO,
  2291. .priv_data_size = sizeof(Mpeg1Context),
  2292. .init = mpeg_decode_init,
  2293. .close = mpeg_decode_end,
  2294. .decode = mpeg_decode_frame,
  2295. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2296. .flush = flush,
  2297. .max_lowres = 3,
  2298. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2299. };
  2300. #if CONFIG_MPEG_XVMC_DECODER
  2301. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
  2302. {
  2303. if (avctx->active_thread_type & FF_THREAD_SLICE)
  2304. return -1;
  2305. if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2306. return -1;
  2307. if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  2308. av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2309. }
  2310. mpeg_decode_init(avctx);
  2311. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2312. avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
  2313. return 0;
  2314. }
  2315. AVCodec ff_mpeg_xvmc_decoder = {
  2316. .name = "mpegvideo_xvmc",
  2317. .type = AVMEDIA_TYPE_VIDEO,
  2318. .id = CODEC_ID_MPEG2VIDEO_XVMC,
  2319. .priv_data_size = sizeof(Mpeg1Context),
  2320. .init = mpeg_mc_decode_init,
  2321. .close = mpeg_decode_end,
  2322. .decode = mpeg_decode_frame,
  2323. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2324. .flush = flush,
  2325. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2326. };
  2327. #endif
  2328. #if CONFIG_MPEG_VDPAU_DECODER
  2329. AVCodec ff_mpeg_vdpau_decoder = {
  2330. .name = "mpegvideo_vdpau",
  2331. .type = AVMEDIA_TYPE_VIDEO,
  2332. .id = CODEC_ID_MPEG2VIDEO,
  2333. .priv_data_size = sizeof(Mpeg1Context),
  2334. .init = mpeg_decode_init,
  2335. .close = mpeg_decode_end,
  2336. .decode = mpeg_decode_frame,
  2337. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2338. .flush = flush,
  2339. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
  2340. };
  2341. #endif
  2342. #if CONFIG_MPEG1_VDPAU_DECODER
  2343. AVCodec ff_mpeg1_vdpau_decoder = {
  2344. .name = "mpeg1video_vdpau",
  2345. .type = AVMEDIA_TYPE_VIDEO,
  2346. .id = CODEC_ID_MPEG1VIDEO,
  2347. .priv_data_size = sizeof(Mpeg1Context),
  2348. .init = mpeg_decode_init,
  2349. .close = mpeg_decode_end,
  2350. .decode = mpeg_decode_frame,
  2351. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2352. .flush = flush,
  2353. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
  2354. };
  2355. #endif