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.

2618 lines
94KB

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