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.

2615 lines
93KB

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