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.

2614 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. #endif
  1002. #if CONFIG_MPEG2_VAAPI_HWACCEL
  1003. AV_PIX_FMT_VAAPI,
  1004. #endif
  1005. #if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
  1006. AV_PIX_FMT_VDPAU,
  1007. #endif
  1008. AV_PIX_FMT_YUV420P,
  1009. AV_PIX_FMT_NONE
  1010. };
  1011. static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
  1012. AV_PIX_FMT_YUV422P,
  1013. AV_PIX_FMT_NONE
  1014. };
  1015. static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
  1016. AV_PIX_FMT_YUV444P,
  1017. AV_PIX_FMT_NONE
  1018. };
  1019. static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
  1020. {
  1021. Mpeg1Context *s1 = avctx->priv_data;
  1022. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1023. const enum AVPixelFormat *pix_fmts;
  1024. if (s->chroma_format < 2)
  1025. pix_fmts = mpeg12_hwaccel_pixfmt_list_420;
  1026. else if (s->chroma_format == 2)
  1027. pix_fmts = mpeg12_pixfmt_list_422;
  1028. else
  1029. pix_fmts = mpeg12_pixfmt_list_444;
  1030. return ff_get_format(avctx, pix_fmts);
  1031. }
  1032. /* Call this function when we know all parameters.
  1033. * It may be called in different places for MPEG-1 and MPEG-2. */
  1034. static int mpeg_decode_postinit(AVCodecContext *avctx)
  1035. {
  1036. Mpeg1Context *s1 = avctx->priv_data;
  1037. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1038. uint8_t old_permutation[64];
  1039. int ret;
  1040. if ((s1->mpeg_enc_ctx_allocated == 0) ||
  1041. avctx->coded_width != s->width ||
  1042. avctx->coded_height != s->height ||
  1043. s1->save_width != s->width ||
  1044. s1->save_height != s->height ||
  1045. s1->save_aspect_info != s->aspect_ratio_info ||
  1046. s1->save_progressive_seq != s->progressive_sequence ||
  1047. 0) {
  1048. if (s1->mpeg_enc_ctx_allocated) {
  1049. ParseContext pc = s->parse_context;
  1050. s->parse_context.buffer = 0;
  1051. ff_mpv_common_end(s);
  1052. s->parse_context = pc;
  1053. }
  1054. ret = ff_set_dimensions(avctx, s->width, s->height);
  1055. if (ret < 0)
  1056. return ret;
  1057. avctx->bit_rate = s->bit_rate;
  1058. s1->save_aspect_info = s->aspect_ratio_info;
  1059. s1->save_width = s->width;
  1060. s1->save_height = s->height;
  1061. s1->save_progressive_seq = s->progressive_sequence;
  1062. /* low_delay may be forced, in this case we will have B-frames
  1063. * that behave like P-frames. */
  1064. avctx->has_b_frames = !s->low_delay;
  1065. if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  1066. // MPEG-1 fps
  1067. avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
  1068. // MPEG-1 aspect
  1069. avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1070. avctx->ticks_per_frame = 1;
  1071. } else { // MPEG-2
  1072. // MPEG-2 fps
  1073. av_reduce(&s->avctx->framerate.num,
  1074. &s->avctx->framerate.den,
  1075. ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
  1076. ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1077. 1 << 30);
  1078. avctx->ticks_per_frame = 2;
  1079. // MPEG-2 aspect
  1080. if (s->aspect_ratio_info > 1) {
  1081. AVRational dar =
  1082. av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1083. (AVRational) { s1->pan_scan.width,
  1084. s1->pan_scan.height }),
  1085. (AVRational) { s->width, s->height });
  1086. /* We ignore the spec here and guess a bit as reality does not
  1087. * match the spec, see for example res_change_ffmpeg_aspect.ts
  1088. * and sequence-display-aspect.mpg.
  1089. * issue1613, 621, 562 */
  1090. if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
  1091. (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
  1092. av_cmp_q(dar, (AVRational) { 16, 9 }))) {
  1093. s->avctx->sample_aspect_ratio =
  1094. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1095. (AVRational) { s->width, s->height });
  1096. } else {
  1097. s->avctx->sample_aspect_ratio =
  1098. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1099. (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
  1100. // issue1613 4/3 16/9 -> 16/9
  1101. // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1102. // widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1103. // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
  1104. ff_dlog(avctx, "A %d/%d\n",
  1105. ff_mpeg2_aspect[s->aspect_ratio_info].num,
  1106. ff_mpeg2_aspect[s->aspect_ratio_info].den);
  1107. ff_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
  1108. s->avctx->sample_aspect_ratio.den);
  1109. }
  1110. } else {
  1111. s->avctx->sample_aspect_ratio =
  1112. ff_mpeg2_aspect[s->aspect_ratio_info];
  1113. }
  1114. } // MPEG-2
  1115. ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
  1116. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1117. // until then pix_fmt may be changed right after codec init
  1118. if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
  1119. avctx->idct_algo = FF_IDCT_SIMPLE;
  1120. /* Quantization matrices may need reordering
  1121. * if DCT permutation is changed. */
  1122. memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
  1123. ff_mpv_idct_init(s);
  1124. if ((ret = ff_mpv_common_init(s)) < 0)
  1125. return ret;
  1126. quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
  1127. quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
  1128. quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
  1129. quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
  1130. s1->mpeg_enc_ctx_allocated = 1;
  1131. }
  1132. return 0;
  1133. }
  1134. static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
  1135. int buf_size)
  1136. {
  1137. Mpeg1Context *s1 = avctx->priv_data;
  1138. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1139. int ref, f_code, vbv_delay;
  1140. init_get_bits(&s->gb, buf, buf_size * 8);
  1141. ref = get_bits(&s->gb, 10); /* temporal ref */
  1142. s->pict_type = get_bits(&s->gb, 3);
  1143. if (s->pict_type == 0 || s->pict_type > 3)
  1144. return AVERROR_INVALIDDATA;
  1145. vbv_delay = get_bits(&s->gb, 16);
  1146. if (s->pict_type == AV_PICTURE_TYPE_P ||
  1147. s->pict_type == AV_PICTURE_TYPE_B) {
  1148. s->full_pel[0] = get_bits1(&s->gb);
  1149. f_code = get_bits(&s->gb, 3);
  1150. if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
  1151. return AVERROR_INVALIDDATA;
  1152. s->mpeg_f_code[0][0] = f_code;
  1153. s->mpeg_f_code[0][1] = f_code;
  1154. }
  1155. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1156. s->full_pel[1] = get_bits1(&s->gb);
  1157. f_code = get_bits(&s->gb, 3);
  1158. if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
  1159. return AVERROR_INVALIDDATA;
  1160. s->mpeg_f_code[1][0] = f_code;
  1161. s->mpeg_f_code[1][1] = f_code;
  1162. }
  1163. s->current_picture.f->pict_type = s->pict_type;
  1164. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1165. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1166. av_log(avctx, AV_LOG_DEBUG,
  1167. "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1168. s->y_dc_scale = 8;
  1169. s->c_dc_scale = 8;
  1170. return 0;
  1171. }
  1172. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1173. {
  1174. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1175. int horiz_size_ext, vert_size_ext;
  1176. int bit_rate_ext;
  1177. skip_bits(&s->gb, 1); /* profile and level esc*/
  1178. s->avctx->profile = get_bits(&s->gb, 3);
  1179. s->avctx->level = get_bits(&s->gb, 4);
  1180. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1181. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1182. horiz_size_ext = get_bits(&s->gb, 2);
  1183. vert_size_ext = get_bits(&s->gb, 2);
  1184. s->width |= (horiz_size_ext << 12);
  1185. s->height |= (vert_size_ext << 12);
  1186. bit_rate_ext = get_bits(&s->gb, 12) << 18;
  1187. if (bit_rate_ext < INT_MAX / 400 &&
  1188. bit_rate_ext * 400 < INT_MAX - s->bit_rate) {
  1189. s->bit_rate += bit_rate_ext * 400;
  1190. } else {
  1191. av_log(s->avctx, AV_LOG_WARNING, "Invalid bit rate extension value: %d\n",
  1192. bit_rate_ext >> 18);
  1193. s->bit_rate = 0;
  1194. }
  1195. skip_bits1(&s->gb); /* marker */
  1196. s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
  1197. s->low_delay = get_bits1(&s->gb);
  1198. if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
  1199. s->low_delay = 1;
  1200. s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
  1201. s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  1202. ff_dlog(s->avctx, "sequence extension\n");
  1203. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1204. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1205. av_log(s->avctx, AV_LOG_DEBUG,
  1206. "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1207. s->avctx->profile, s->avctx->level,
  1208. s->avctx->rc_buffer_size, s->bit_rate);
  1209. }
  1210. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1211. {
  1212. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1213. int color_description, w, h;
  1214. skip_bits(&s->gb, 3); /* video format */
  1215. color_description = get_bits1(&s->gb);
  1216. if (color_description) {
  1217. s->avctx->color_primaries = get_bits(&s->gb, 8);
  1218. s->avctx->color_trc = get_bits(&s->gb, 8);
  1219. s->avctx->colorspace = get_bits(&s->gb, 8);
  1220. }
  1221. w = get_bits(&s->gb, 14);
  1222. skip_bits(&s->gb, 1); // marker
  1223. h = get_bits(&s->gb, 14);
  1224. // remaining 3 bits are zero padding
  1225. s1->pan_scan.width = 16 * w;
  1226. s1->pan_scan.height = 16 * h;
  1227. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1228. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1229. }
  1230. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1231. {
  1232. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1233. int i, nofco;
  1234. nofco = 1;
  1235. if (s->progressive_sequence) {
  1236. if (s->repeat_first_field) {
  1237. nofco++;
  1238. if (s->top_field_first)
  1239. nofco++;
  1240. }
  1241. } else {
  1242. if (s->picture_structure == PICT_FRAME) {
  1243. nofco++;
  1244. if (s->repeat_first_field)
  1245. nofco++;
  1246. }
  1247. }
  1248. for (i = 0; i < nofco; i++) {
  1249. s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
  1250. skip_bits(&s->gb, 1); // marker
  1251. s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
  1252. skip_bits(&s->gb, 1); // marker
  1253. }
  1254. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1255. av_log(s->avctx, AV_LOG_DEBUG,
  1256. "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
  1257. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1258. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1259. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
  1260. }
  1261. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
  1262. uint16_t matrix1[64], int intra)
  1263. {
  1264. int i;
  1265. for (i = 0; i < 64; i++) {
  1266. int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  1267. int v = get_bits(&s->gb, 8);
  1268. if (v == 0) {
  1269. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1270. return AVERROR_INVALIDDATA;
  1271. }
  1272. if (intra && i == 0 && v != 8) {
  1273. av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
  1274. v = 8; // needed by pink.mpg / issue1046
  1275. }
  1276. matrix0[j] = v;
  1277. if (matrix1)
  1278. matrix1[j] = v;
  1279. }
  1280. return 0;
  1281. }
  1282. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1283. {
  1284. ff_dlog(s->avctx, "matrix extension\n");
  1285. if (get_bits1(&s->gb))
  1286. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1287. if (get_bits1(&s->gb))
  1288. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1289. if (get_bits1(&s->gb))
  1290. load_matrix(s, s->chroma_intra_matrix, NULL, 1);
  1291. if (get_bits1(&s->gb))
  1292. load_matrix(s, s->chroma_inter_matrix, NULL, 0);
  1293. }
  1294. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1295. {
  1296. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1297. s->full_pel[0] = s->full_pel[1] = 0;
  1298. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1299. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1300. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1301. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1302. if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
  1303. av_log(s->avctx, AV_LOG_ERROR,
  1304. "Missing picture start code, guessing missing values\n");
  1305. if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
  1306. if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1307. s->pict_type = AV_PICTURE_TYPE_I;
  1308. else
  1309. s->pict_type = AV_PICTURE_TYPE_P;
  1310. } else
  1311. s->pict_type = AV_PICTURE_TYPE_B;
  1312. s->current_picture.f->pict_type = s->pict_type;
  1313. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1314. }
  1315. s->intra_dc_precision = get_bits(&s->gb, 2);
  1316. s->picture_structure = get_bits(&s->gb, 2);
  1317. s->top_field_first = get_bits1(&s->gb);
  1318. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1319. s->concealment_motion_vectors = get_bits1(&s->gb);
  1320. s->q_scale_type = get_bits1(&s->gb);
  1321. s->intra_vlc_format = get_bits1(&s->gb);
  1322. s->alternate_scan = get_bits1(&s->gb);
  1323. s->repeat_first_field = get_bits1(&s->gb);
  1324. s->chroma_420_type = get_bits1(&s->gb);
  1325. s->progressive_frame = get_bits1(&s->gb);
  1326. if (s->progressive_sequence && !s->progressive_frame) {
  1327. s->progressive_frame = 1;
  1328. av_log(s->avctx, AV_LOG_ERROR,
  1329. "interlaced frame in progressive sequence, ignoring\n");
  1330. }
  1331. if (s->picture_structure == 0 ||
  1332. (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
  1333. av_log(s->avctx, AV_LOG_ERROR,
  1334. "picture_structure %d invalid, ignoring\n",
  1335. s->picture_structure);
  1336. s->picture_structure = PICT_FRAME;
  1337. }
  1338. if (s->progressive_sequence && !s->frame_pred_frame_dct)
  1339. av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
  1340. if (s->picture_structure == PICT_FRAME) {
  1341. s->v_edge_pos = 16 * s->mb_height;
  1342. } else {
  1343. s->v_edge_pos = 8 * s->mb_height;
  1344. memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
  1345. }
  1346. if (s->alternate_scan) {
  1347. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  1348. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  1349. } else {
  1350. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  1351. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  1352. }
  1353. /* composite display not parsed */
  1354. ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1355. ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1356. ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1357. ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1358. ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1359. ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1360. ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1361. ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1362. ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1363. }
  1364. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  1365. {
  1366. AVCodecContext *avctx = s->avctx;
  1367. Mpeg1Context *s1 = (Mpeg1Context *) s;
  1368. int ret;
  1369. if (s->picture_structure == PICT_FRAME)
  1370. s->first_field = 0;
  1371. else
  1372. s->first_field ^= 1;
  1373. /* start frame decoding */
  1374. if (s->first_field || s->picture_structure == PICT_FRAME) {
  1375. AVFrameSideData *pan_scan;
  1376. if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
  1377. return ret;
  1378. ff_mpeg_er_frame_start(s);
  1379. /* first check if we must repeat the frame */
  1380. s->current_picture_ptr->f->repeat_pict = 0;
  1381. if (s->repeat_first_field) {
  1382. if (s->progressive_sequence) {
  1383. if (s->top_field_first)
  1384. s->current_picture_ptr->f->repeat_pict = 4;
  1385. else
  1386. s->current_picture_ptr->f->repeat_pict = 2;
  1387. } else if (s->progressive_frame) {
  1388. s->current_picture_ptr->f->repeat_pict = 1;
  1389. }
  1390. }
  1391. pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
  1392. AV_FRAME_DATA_PANSCAN,
  1393. sizeof(s1->pan_scan));
  1394. if (!pan_scan)
  1395. return AVERROR(ENOMEM);
  1396. memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
  1397. if (s1->a53_caption) {
  1398. AVFrameSideData *sd = av_frame_new_side_data(
  1399. s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
  1400. s1->a53_caption_size);
  1401. if (sd)
  1402. memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
  1403. av_freep(&s1->a53_caption);
  1404. }
  1405. if (s1->has_stereo3d) {
  1406. AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
  1407. if (!stereo)
  1408. return AVERROR(ENOMEM);
  1409. *stereo = s1->stereo3d;
  1410. s1->has_stereo3d = 0;
  1411. }
  1412. if (s1->has_afd) {
  1413. AVFrameSideData *sd =
  1414. av_frame_new_side_data(s->current_picture_ptr->f,
  1415. AV_FRAME_DATA_AFD, 1);
  1416. if (!sd)
  1417. return AVERROR(ENOMEM);
  1418. *sd->data = s1->afd;
  1419. s1->has_afd = 0;
  1420. }
  1421. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1422. ff_thread_finish_setup(avctx);
  1423. } else { // second field
  1424. int i;
  1425. if (!s->current_picture_ptr) {
  1426. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1427. return AVERROR_INVALIDDATA;
  1428. }
  1429. if (s->avctx->hwaccel &&
  1430. (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  1431. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1432. av_log(avctx, AV_LOG_ERROR,
  1433. "hardware accelerator failed to decode first field\n");
  1434. }
  1435. for (i = 0; i < 4; i++) {
  1436. s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
  1437. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1438. s->current_picture.f->data[i] +=
  1439. s->current_picture_ptr->f->linesize[i];
  1440. }
  1441. }
  1442. if (avctx->hwaccel) {
  1443. if ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
  1444. return ret;
  1445. }
  1446. return 0;
  1447. }
  1448. #define DECODE_SLICE_ERROR -1
  1449. #define DECODE_SLICE_OK 0
  1450. /**
  1451. * Decode a slice.
  1452. * MpegEncContext.mb_y must be set to the MB row from the startcode.
  1453. * @return DECODE_SLICE_ERROR if the slice is damaged,
  1454. * DECODE_SLICE_OK if this slice is OK
  1455. */
  1456. static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
  1457. const uint8_t **buf, int buf_size)
  1458. {
  1459. AVCodecContext *avctx = s->avctx;
  1460. const int field_pic = s->picture_structure != PICT_FRAME;
  1461. int ret;
  1462. s->resync_mb_x =
  1463. s->resync_mb_y = -1;
  1464. assert(mb_y < s->mb_height);
  1465. init_get_bits(&s->gb, *buf, buf_size * 8);
  1466. ff_mpeg1_clean_buffers(s);
  1467. s->interlaced_dct = 0;
  1468. s->qscale = get_qscale(s);
  1469. if (s->qscale == 0) {
  1470. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1471. return AVERROR_INVALIDDATA;
  1472. }
  1473. /* extra slice info */
  1474. while (get_bits1(&s->gb) != 0)
  1475. skip_bits(&s->gb, 8);
  1476. s->mb_x = 0;
  1477. if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
  1478. skip_bits1(&s->gb);
  1479. } else {
  1480. while (get_bits_left(&s->gb) > 0) {
  1481. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1482. MBINCR_VLC_BITS, 2);
  1483. if (code < 0) {
  1484. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1485. return AVERROR_INVALIDDATA;
  1486. }
  1487. if (code >= 33) {
  1488. if (code == 33)
  1489. s->mb_x += 33;
  1490. /* otherwise, stuffing, nothing to do */
  1491. } else {
  1492. s->mb_x += code;
  1493. break;
  1494. }
  1495. }
  1496. }
  1497. if (s->mb_x >= (unsigned) s->mb_width) {
  1498. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1499. return AVERROR_INVALIDDATA;
  1500. }
  1501. if (avctx->hwaccel) {
  1502. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1503. int start_code = -1;
  1504. buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1505. if (buf_end < *buf + buf_size)
  1506. buf_end -= 4;
  1507. s->mb_y = mb_y;
  1508. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1509. return DECODE_SLICE_ERROR;
  1510. *buf = buf_end;
  1511. return DECODE_SLICE_OK;
  1512. }
  1513. s->resync_mb_x = s->mb_x;
  1514. s->resync_mb_y = s->mb_y = mb_y;
  1515. s->mb_skip_run = 0;
  1516. ff_init_block_index(s);
  1517. if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
  1518. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1519. av_log(s->avctx, AV_LOG_DEBUG,
  1520. "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",
  1521. s->qscale,
  1522. s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
  1523. s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
  1524. s->pict_type == AV_PICTURE_TYPE_I ? "I" :
  1525. (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
  1526. (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  1527. s->progressive_sequence ? "ps" : "",
  1528. s->progressive_frame ? "pf" : "",
  1529. s->alternate_scan ? "alt" : "",
  1530. s->top_field_first ? "top" : "",
  1531. s->intra_dc_precision, s->picture_structure,
  1532. s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1533. s->q_scale_type, s->intra_vlc_format,
  1534. s->repeat_first_field, s->chroma_420_type ? "420" : "");
  1535. }
  1536. }
  1537. for (;;) {
  1538. if ((ret = mpeg_decode_mb(s, s->block)) < 0)
  1539. return ret;
  1540. // Note motion_val is normally NULL unless we want to extract the MVs.
  1541. if (s->current_picture.motion_val[0] && !s->encoding) {
  1542. const int wrap = s->b8_stride;
  1543. int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
  1544. int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
  1545. int motion_x, motion_y, dir, i;
  1546. for (i = 0; i < 2; i++) {
  1547. for (dir = 0; dir < 2; dir++) {
  1548. if (s->mb_intra ||
  1549. (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1550. motion_x = motion_y = 0;
  1551. } else if (s->mv_type == MV_TYPE_16X16 ||
  1552. (s->mv_type == MV_TYPE_FIELD && field_pic)) {
  1553. motion_x = s->mv[dir][0][0];
  1554. motion_y = s->mv[dir][0][1];
  1555. } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
  1556. motion_x = s->mv[dir][i][0];
  1557. motion_y = s->mv[dir][i][1];
  1558. }
  1559. s->current_picture.motion_val[dir][xy][0] = motion_x;
  1560. s->current_picture.motion_val[dir][xy][1] = motion_y;
  1561. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1562. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1563. s->current_picture.ref_index [dir][b8_xy] =
  1564. s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
  1565. assert(s->field_select[dir][i] == 0 ||
  1566. s->field_select[dir][i] == 1);
  1567. }
  1568. xy += wrap;
  1569. b8_xy += 2;
  1570. }
  1571. }
  1572. s->dest[0] += 16;
  1573. s->dest[1] += 16 >> s->chroma_x_shift;
  1574. s->dest[2] += 16 >> s->chroma_x_shift;
  1575. ff_mpv_decode_mb(s, s->block);
  1576. if (++s->mb_x >= s->mb_width) {
  1577. const int mb_size = 16;
  1578. ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
  1579. ff_mpv_report_decode_progress(s);
  1580. s->mb_x = 0;
  1581. s->mb_y += 1 << field_pic;
  1582. if (s->mb_y >= s->mb_height) {
  1583. int left = get_bits_left(&s->gb);
  1584. int is_d10 = s->chroma_format == 2 &&
  1585. s->pict_type == AV_PICTURE_TYPE_I &&
  1586. avctx->profile == 0 && avctx->level == 5 &&
  1587. s->intra_dc_precision == 2 &&
  1588. s->q_scale_type == 1 && s->alternate_scan == 0 &&
  1589. s->progressive_frame == 0
  1590. /* vbv_delay == 0xBBB || 0xE10 */;
  1591. if (left < 0 ||
  1592. (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
  1593. ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
  1594. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
  1595. left, show_bits(&s->gb, FFMIN(left, 23)));
  1596. return AVERROR_INVALIDDATA;
  1597. } else
  1598. goto eos;
  1599. }
  1600. ff_init_block_index(s);
  1601. }
  1602. /* skip mb handling */
  1603. if (s->mb_skip_run == -1) {
  1604. /* read increment again */
  1605. s->mb_skip_run = 0;
  1606. for (;;) {
  1607. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1608. MBINCR_VLC_BITS, 2);
  1609. if (code < 0) {
  1610. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1611. return AVERROR_INVALIDDATA;
  1612. }
  1613. if (code >= 33) {
  1614. if (code == 33) {
  1615. s->mb_skip_run += 33;
  1616. } else if (code == 35) {
  1617. if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
  1618. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1619. return AVERROR_INVALIDDATA;
  1620. }
  1621. goto eos; /* end of slice */
  1622. }
  1623. /* otherwise, stuffing, nothing to do */
  1624. } else {
  1625. s->mb_skip_run += code;
  1626. break;
  1627. }
  1628. }
  1629. if (s->mb_skip_run) {
  1630. int i;
  1631. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1632. av_log(s->avctx, AV_LOG_ERROR,
  1633. "skipped MB in I-frame at %d %d\n", s->mb_x, s->mb_y);
  1634. return AVERROR_INVALIDDATA;
  1635. }
  1636. /* skip mb */
  1637. s->mb_intra = 0;
  1638. for (i = 0; i < 12; i++)
  1639. s->block_last_index[i] = -1;
  1640. if (s->picture_structure == PICT_FRAME)
  1641. s->mv_type = MV_TYPE_16X16;
  1642. else
  1643. s->mv_type = MV_TYPE_FIELD;
  1644. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1645. /* if P type, zero motion vector is implied */
  1646. s->mv_dir = MV_DIR_FORWARD;
  1647. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1648. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1649. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1650. s->field_select[0][0] = (s->picture_structure - 1) & 1;
  1651. } else {
  1652. /* if B type, reuse previous vectors and directions */
  1653. s->mv[0][0][0] = s->last_mv[0][0][0];
  1654. s->mv[0][0][1] = s->last_mv[0][0][1];
  1655. s->mv[1][0][0] = s->last_mv[1][0][0];
  1656. s->mv[1][0][1] = s->last_mv[1][0][1];
  1657. }
  1658. }
  1659. }
  1660. }
  1661. eos: // end of slice
  1662. *buf += (get_bits_count(&s->gb) - 1) / 8;
  1663. ff_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1664. return 0;
  1665. }
  1666. static int slice_decode_thread(AVCodecContext *c, void *arg)
  1667. {
  1668. MpegEncContext *s = *(void **) arg;
  1669. const uint8_t *buf = s->gb.buffer;
  1670. int mb_y = s->start_mb_y;
  1671. const int field_pic = s->picture_structure != PICT_FRAME;
  1672. s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
  1673. for (;;) {
  1674. uint32_t start_code;
  1675. int ret;
  1676. ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
  1677. emms_c();
  1678. ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1679. ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
  1680. s->start_mb_y, s->end_mb_y, s->er.error_count);
  1681. if (ret < 0) {
  1682. if (c->err_recognition & AV_EF_EXPLODE)
  1683. return ret;
  1684. if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
  1685. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1686. s->mb_x, s->mb_y,
  1687. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  1688. } else {
  1689. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1690. s->mb_x - 1, s->mb_y,
  1691. ER_AC_END | ER_DC_END | ER_MV_END);
  1692. }
  1693. if (s->mb_y == s->end_mb_y)
  1694. return 0;
  1695. start_code = -1;
  1696. buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
  1697. mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  1698. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1699. mb_y++;
  1700. if (mb_y < 0 || mb_y >= s->end_mb_y)
  1701. return AVERROR_INVALIDDATA;
  1702. }
  1703. }
  1704. /**
  1705. * Handle slice ends.
  1706. * @return 1 if it seems to be the last slice
  1707. */
  1708. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1709. {
  1710. Mpeg1Context *s1 = avctx->priv_data;
  1711. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1712. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1713. return 0;
  1714. if (s->avctx->hwaccel) {
  1715. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1716. av_log(avctx, AV_LOG_ERROR,
  1717. "hardware accelerator failed to decode picture\n");
  1718. }
  1719. /* end of slice reached */
  1720. if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
  1721. /* end of image */
  1722. ff_er_frame_end(&s->er);
  1723. ff_mpv_frame_end(s);
  1724. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1725. int ret = av_frame_ref(pict, s->current_picture_ptr->f);
  1726. if (ret < 0)
  1727. return ret;
  1728. ff_print_debug_info(s, s->current_picture_ptr);
  1729. } else {
  1730. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1731. s->picture_number++;
  1732. /* latency of 1 frame for I- and P-frames */
  1733. /* XXX: use another variable than picture_number */
  1734. if (s->last_picture_ptr) {
  1735. int ret = av_frame_ref(pict, s->last_picture_ptr->f);
  1736. if (ret < 0)
  1737. return ret;
  1738. ff_print_debug_info(s, s->last_picture_ptr);
  1739. }
  1740. }
  1741. return 1;
  1742. } else {
  1743. return 0;
  1744. }
  1745. }
  1746. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1747. const uint8_t *buf, int buf_size)
  1748. {
  1749. Mpeg1Context *s1 = avctx->priv_data;
  1750. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1751. int width, height;
  1752. int i, v, j;
  1753. init_get_bits(&s->gb, buf, buf_size * 8);
  1754. width = get_bits(&s->gb, 12);
  1755. height = get_bits(&s->gb, 12);
  1756. if (width == 0 || height == 0) {
  1757. av_log(avctx, AV_LOG_WARNING,
  1758. "Invalid horizontal or vertical size value.\n");
  1759. if (avctx->err_recognition & AV_EF_BITSTREAM)
  1760. return AVERROR_INVALIDDATA;
  1761. }
  1762. s->aspect_ratio_info = get_bits(&s->gb, 4);
  1763. if (s->aspect_ratio_info == 0) {
  1764. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1765. if (avctx->err_recognition & AV_EF_BITSTREAM)
  1766. return AVERROR_INVALIDDATA;
  1767. }
  1768. s->frame_rate_index = get_bits(&s->gb, 4);
  1769. if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
  1770. av_log(avctx, AV_LOG_WARNING,
  1771. "frame_rate_index %d is invalid\n", s->frame_rate_index);
  1772. return AVERROR_INVALIDDATA;
  1773. }
  1774. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1775. if (get_bits1(&s->gb) == 0) { /* marker */
  1776. av_log(avctx, AV_LOG_ERROR, "Marker in sequence header missing\n");
  1777. return AVERROR_INVALIDDATA;
  1778. }
  1779. s->width = width;
  1780. s->height = height;
  1781. s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
  1782. skip_bits(&s->gb, 1);
  1783. /* get matrix */
  1784. if (get_bits1(&s->gb)) {
  1785. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1786. } else {
  1787. for (i = 0; i < 64; i++) {
  1788. j = s->idsp.idct_permutation[i];
  1789. v = ff_mpeg1_default_intra_matrix[i];
  1790. s->intra_matrix[j] = v;
  1791. s->chroma_intra_matrix[j] = v;
  1792. }
  1793. }
  1794. if (get_bits1(&s->gb)) {
  1795. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1796. } else {
  1797. for (i = 0; i < 64; i++) {
  1798. int j = s->idsp.idct_permutation[i];
  1799. v = ff_mpeg1_default_non_intra_matrix[i];
  1800. s->inter_matrix[j] = v;
  1801. s->chroma_inter_matrix[j] = v;
  1802. }
  1803. }
  1804. if (show_bits(&s->gb, 23) != 0) {
  1805. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1806. return AVERROR_INVALIDDATA;
  1807. }
  1808. /* We set MPEG-2 parameters so that it emulates MPEG-1. */
  1809. s->progressive_sequence = 1;
  1810. s->progressive_frame = 1;
  1811. s->picture_structure = PICT_FRAME;
  1812. s->frame_pred_frame_dct = 1;
  1813. s->chroma_format = 1;
  1814. s->codec_id =
  1815. s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  1816. s->out_format = FMT_MPEG1;
  1817. if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
  1818. s->low_delay = 1;
  1819. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1820. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1821. s->avctx->rc_buffer_size, s->bit_rate);
  1822. return 0;
  1823. }
  1824. static int vcr2_init_sequence(AVCodecContext *avctx)
  1825. {
  1826. Mpeg1Context *s1 = avctx->priv_data;
  1827. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1828. int i, v, ret;
  1829. /* start new MPEG-1 context decoding */
  1830. s->out_format = FMT_MPEG1;
  1831. if (s1->mpeg_enc_ctx_allocated) {
  1832. ff_mpv_common_end(s);
  1833. }
  1834. s->width = avctx->coded_width;
  1835. s->height = avctx->coded_height;
  1836. avctx->has_b_frames = 0; // true?
  1837. s->low_delay = 1;
  1838. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1839. if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
  1840. avctx->idct_algo = FF_IDCT_SIMPLE;
  1841. ff_mpv_idct_init(s);
  1842. if ((ret = ff_mpv_common_init(s)) < 0)
  1843. return ret;
  1844. s1->mpeg_enc_ctx_allocated = 1;
  1845. for (i = 0; i < 64; i++) {
  1846. int j = s->idsp.idct_permutation[i];
  1847. v = ff_mpeg1_default_intra_matrix[i];
  1848. s->intra_matrix[j] = v;
  1849. s->chroma_intra_matrix[j] = v;
  1850. v = ff_mpeg1_default_non_intra_matrix[i];
  1851. s->inter_matrix[j] = v;
  1852. s->chroma_inter_matrix[j] = v;
  1853. }
  1854. s->progressive_sequence = 1;
  1855. s->progressive_frame = 1;
  1856. s->picture_structure = PICT_FRAME;
  1857. s->frame_pred_frame_dct = 1;
  1858. s->chroma_format = 1;
  1859. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1860. s1->save_width = s->width;
  1861. s1->save_height = s->height;
  1862. s1->save_progressive_seq = s->progressive_sequence;
  1863. return 0;
  1864. }
  1865. static int mpeg_decode_a53_cc(AVCodecContext *avctx,
  1866. const uint8_t *p, int buf_size)
  1867. {
  1868. Mpeg1Context *s1 = avctx->priv_data;
  1869. if (buf_size >= 6 &&
  1870. p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
  1871. p[4] == 3 && (p[5] & 0x40)) {
  1872. /* extract A53 Part 4 CC data */
  1873. int cc_count = p[5] & 0x1f;
  1874. if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
  1875. av_freep(&s1->a53_caption);
  1876. s1->a53_caption_size = cc_count * 3;
  1877. s1->a53_caption = av_malloc(s1->a53_caption_size);
  1878. if (s1->a53_caption)
  1879. memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
  1880. }
  1881. return 1;
  1882. } else if (buf_size >= 11 &&
  1883. p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
  1884. /* extract DVD CC data */
  1885. int cc_count = 0;
  1886. int i;
  1887. // There is a caption count field in the data, but it is often
  1888. // incorrect. So count the number of captions present.
  1889. for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
  1890. cc_count++;
  1891. // Transform the DVD format into A53 Part 4 format
  1892. if (cc_count > 0) {
  1893. av_freep(&s1->a53_caption);
  1894. s1->a53_caption_size = cc_count * 6;
  1895. s1->a53_caption = av_malloc(s1->a53_caption_size);
  1896. if (s1->a53_caption) {
  1897. uint8_t field1 = !!(p[4] & 0x80);
  1898. uint8_t *cap = s1->a53_caption;
  1899. p += 5;
  1900. for (i = 0; i < cc_count; i++) {
  1901. cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
  1902. cap[1] = p[1];
  1903. cap[2] = p[2];
  1904. cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
  1905. cap[4] = p[4];
  1906. cap[5] = p[5];
  1907. cap += 6;
  1908. p += 6;
  1909. }
  1910. }
  1911. }
  1912. return 1;
  1913. }
  1914. return 0;
  1915. }
  1916. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1917. const uint8_t *p, int buf_size)
  1918. {
  1919. const uint8_t *buf_end = p + buf_size;
  1920. Mpeg1Context *s1 = avctx->priv_data;
  1921. /* we parse the DTG active format information */
  1922. if (buf_end - p >= 5 &&
  1923. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1924. int flags = p[4];
  1925. p += 5;
  1926. if (flags & 0x80) {
  1927. /* skip event id */
  1928. p += 2;
  1929. }
  1930. if (flags & 0x40) {
  1931. if (buf_end - p < 1)
  1932. return;
  1933. s1->has_afd = 1;
  1934. s1->afd = p[0] & 0x0f;
  1935. }
  1936. } else if (buf_end - p >= 6 &&
  1937. p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
  1938. p[4] == 0x03) { // S3D_video_format_length
  1939. // the 0x7F mask ignores the reserved_bit value
  1940. const uint8_t S3D_video_format_type = p[5] & 0x7F;
  1941. if (S3D_video_format_type == 0x03 ||
  1942. S3D_video_format_type == 0x04 ||
  1943. S3D_video_format_type == 0x08 ||
  1944. S3D_video_format_type == 0x23) {
  1945. s1->has_stereo3d = 1;
  1946. switch (S3D_video_format_type) {
  1947. case 0x03:
  1948. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
  1949. break;
  1950. case 0x04:
  1951. s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
  1952. break;
  1953. case 0x08:
  1954. s1->stereo3d.type = AV_STEREO3D_2D;
  1955. break;
  1956. case 0x23:
  1957. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  1958. break;
  1959. }
  1960. }
  1961. } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
  1962. return;
  1963. }
  1964. }
  1965. static void mpeg_decode_gop(AVCodecContext *avctx,
  1966. const uint8_t *buf, int buf_size)
  1967. {
  1968. Mpeg1Context *s1 = avctx->priv_data;
  1969. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1970. int time_code_hours, time_code_minutes;
  1971. int time_code_seconds, time_code_pictures;
  1972. int broken_link;
  1973. init_get_bits(&s->gb, buf, buf_size * 8);
  1974. skip_bits1(&s->gb); /* drop_frame_flag */
  1975. time_code_hours = get_bits(&s->gb, 5);
  1976. time_code_minutes = get_bits(&s->gb, 6);
  1977. skip_bits1(&s->gb); // marker bit
  1978. time_code_seconds = get_bits(&s->gb, 6);
  1979. time_code_pictures = get_bits(&s->gb, 6);
  1980. s1->closed_gop = get_bits1(&s->gb);
  1981. /* broken_link indicate that after editing the
  1982. * reference frames of the first B-Frames after GOP I-Frame
  1983. * are missing (open gop) */
  1984. broken_link = get_bits1(&s->gb);
  1985. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1986. av_log(s->avctx, AV_LOG_DEBUG,
  1987. "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
  1988. time_code_hours, time_code_minutes, time_code_seconds,
  1989. time_code_pictures, s1->closed_gop, broken_link);
  1990. }
  1991. static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
  1992. int *got_output, const uint8_t *buf, int buf_size)
  1993. {
  1994. Mpeg1Context *s = avctx->priv_data;
  1995. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1996. const uint8_t *buf_ptr = buf;
  1997. const uint8_t *buf_end = buf + buf_size;
  1998. int ret, input_size;
  1999. int last_code = 0, skip_frame = 0;
  2000. for (;;) {
  2001. /* find next start code */
  2002. uint32_t start_code = -1;
  2003. buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
  2004. if (start_code > 0x1ff) {
  2005. if (!skip_frame) {
  2006. if (HAVE_THREADS &&
  2007. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2008. !avctx->hwaccel) {
  2009. int i;
  2010. avctx->execute(avctx, slice_decode_thread,
  2011. &s2->thread_context[0], NULL,
  2012. s->slice_count, sizeof(void *));
  2013. for (i = 0; i < s->slice_count; i++)
  2014. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2015. }
  2016. ret = slice_end(avctx, picture);
  2017. if (ret < 0)
  2018. return ret;
  2019. else if (ret) {
  2020. // FIXME: merge with the stuff in mpeg_decode_slice
  2021. if (s2->last_picture_ptr || s2->low_delay)
  2022. *got_output = 1;
  2023. }
  2024. }
  2025. s2->pict_type = 0;
  2026. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2027. }
  2028. input_size = buf_end - buf_ptr;
  2029. if (avctx->debug & FF_DEBUG_STARTCODE)
  2030. av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
  2031. start_code, buf_ptr - buf, input_size);
  2032. /* prepare data for next start code */
  2033. switch (start_code) {
  2034. case SEQ_START_CODE:
  2035. if (last_code == 0) {
  2036. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  2037. s->sync = 1;
  2038. } else {
  2039. av_log(avctx, AV_LOG_ERROR,
  2040. "ignoring SEQ_START_CODE after %X\n", last_code);
  2041. if (avctx->err_recognition & AV_EF_EXPLODE)
  2042. return AVERROR_INVALIDDATA;
  2043. }
  2044. break;
  2045. case PICTURE_START_CODE:
  2046. if (s2->width <= 0 || s2->height <= 0) {
  2047. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
  2048. s2->width, s2->height);
  2049. return AVERROR_INVALIDDATA;
  2050. }
  2051. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2052. !avctx->hwaccel && s->slice_count) {
  2053. int i;
  2054. avctx->execute(avctx, slice_decode_thread,
  2055. s2->thread_context, NULL,
  2056. s->slice_count, sizeof(void *));
  2057. for (i = 0; i < s->slice_count; i++)
  2058. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2059. s->slice_count = 0;
  2060. }
  2061. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  2062. ret = mpeg_decode_postinit(avctx);
  2063. if (ret < 0) {
  2064. av_log(avctx, AV_LOG_ERROR,
  2065. "mpeg_decode_postinit() failure\n");
  2066. return ret;
  2067. }
  2068. /* We have a complete image: we try to decompress it. */
  2069. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  2070. s2->pict_type = 0;
  2071. s->first_slice = 1;
  2072. last_code = PICTURE_START_CODE;
  2073. } else {
  2074. av_log(avctx, AV_LOG_ERROR,
  2075. "ignoring pic after %X\n", last_code);
  2076. if (avctx->err_recognition & AV_EF_EXPLODE)
  2077. return AVERROR_INVALIDDATA;
  2078. }
  2079. break;
  2080. case EXT_START_CODE:
  2081. init_get_bits(&s2->gb, buf_ptr, input_size * 8);
  2082. switch (get_bits(&s2->gb, 4)) {
  2083. case 0x1:
  2084. if (last_code == 0) {
  2085. mpeg_decode_sequence_extension(s);
  2086. } else {
  2087. av_log(avctx, AV_LOG_ERROR,
  2088. "ignoring seq ext after %X\n", last_code);
  2089. if (avctx->err_recognition & AV_EF_EXPLODE)
  2090. return AVERROR_INVALIDDATA;
  2091. }
  2092. break;
  2093. case 0x2:
  2094. mpeg_decode_sequence_display_extension(s);
  2095. break;
  2096. case 0x3:
  2097. mpeg_decode_quant_matrix_extension(s2);
  2098. break;
  2099. case 0x7:
  2100. mpeg_decode_picture_display_extension(s);
  2101. break;
  2102. case 0x8:
  2103. if (last_code == PICTURE_START_CODE) {
  2104. mpeg_decode_picture_coding_extension(s);
  2105. } else {
  2106. av_log(avctx, AV_LOG_ERROR,
  2107. "ignoring pic cod ext after %X\n", last_code);
  2108. if (avctx->err_recognition & AV_EF_EXPLODE)
  2109. return AVERROR_INVALIDDATA;
  2110. }
  2111. break;
  2112. }
  2113. break;
  2114. case USER_START_CODE:
  2115. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  2116. break;
  2117. case GOP_START_CODE:
  2118. if (last_code == 0) {
  2119. s2->first_field = 0;
  2120. mpeg_decode_gop(avctx, buf_ptr, input_size);
  2121. s->sync = 1;
  2122. } else {
  2123. av_log(avctx, AV_LOG_ERROR,
  2124. "ignoring GOP_START_CODE after %X\n", last_code);
  2125. if (avctx->err_recognition & AV_EF_EXPLODE)
  2126. return AVERROR_INVALIDDATA;
  2127. }
  2128. break;
  2129. default:
  2130. if (start_code >= SLICE_MIN_START_CODE &&
  2131. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  2132. const int field_pic = s2->picture_structure != PICT_FRAME;
  2133. int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  2134. last_code = SLICE_MIN_START_CODE;
  2135. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  2136. mb_y++;
  2137. if (mb_y >= s2->mb_height) {
  2138. av_log(s2->avctx, AV_LOG_ERROR,
  2139. "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2140. return AVERROR_INVALIDDATA;
  2141. }
  2142. if (!s2->last_picture_ptr) {
  2143. /* Skip B-frames if we do not have reference frames and
  2144. * GOP is not closed. */
  2145. if (s2->pict_type == AV_PICTURE_TYPE_B) {
  2146. if (!s->closed_gop) {
  2147. skip_frame = 1;
  2148. break;
  2149. }
  2150. }
  2151. }
  2152. if (s2->pict_type == AV_PICTURE_TYPE_I)
  2153. s->sync = 1;
  2154. if (!s2->next_picture_ptr) {
  2155. /* Skip P-frames if we do not have a reference frame or
  2156. * we have an invalid header. */
  2157. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
  2158. skip_frame = 1;
  2159. break;
  2160. }
  2161. }
  2162. if ((avctx->skip_frame >= AVDISCARD_NONREF &&
  2163. s2->pict_type == AV_PICTURE_TYPE_B) ||
  2164. (avctx->skip_frame >= AVDISCARD_NONKEY &&
  2165. s2->pict_type != AV_PICTURE_TYPE_I) ||
  2166. avctx->skip_frame >= AVDISCARD_ALL) {
  2167. skip_frame = 1;
  2168. break;
  2169. }
  2170. if (!s->mpeg_enc_ctx_allocated)
  2171. break;
  2172. if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  2173. if (mb_y < avctx->skip_top ||
  2174. mb_y >= s2->mb_height - avctx->skip_bottom)
  2175. break;
  2176. }
  2177. if (!s2->pict_type) {
  2178. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2179. if (avctx->err_recognition & AV_EF_EXPLODE)
  2180. return AVERROR_INVALIDDATA;
  2181. break;
  2182. }
  2183. if (s->first_slice) {
  2184. skip_frame = 0;
  2185. s->first_slice = 0;
  2186. if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
  2187. return ret;
  2188. }
  2189. if (!s2->current_picture_ptr) {
  2190. av_log(avctx, AV_LOG_ERROR,
  2191. "current_picture not initialized\n");
  2192. return AVERROR_INVALIDDATA;
  2193. }
  2194. if (HAVE_THREADS &&
  2195. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2196. !avctx->hwaccel) {
  2197. int threshold = (s2->mb_height * s->slice_count +
  2198. s2->slice_context_count / 2) /
  2199. s2->slice_context_count;
  2200. if (threshold <= mb_y) {
  2201. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2202. thread_context->start_mb_y = mb_y;
  2203. thread_context->end_mb_y = s2->mb_height;
  2204. if (s->slice_count) {
  2205. s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
  2206. ret = ff_update_duplicate_context(thread_context, s2);
  2207. if (ret < 0)
  2208. return ret;
  2209. }
  2210. init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
  2211. s->slice_count++;
  2212. }
  2213. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2214. } else {
  2215. ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
  2216. emms_c();
  2217. if (ret < 0) {
  2218. if (avctx->err_recognition & AV_EF_EXPLODE)
  2219. return ret;
  2220. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2221. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2222. s2->resync_mb_y, s2->mb_x, s2->mb_y,
  2223. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  2224. } else {
  2225. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2226. s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
  2227. ER_AC_END | ER_DC_END | ER_MV_END);
  2228. }
  2229. }
  2230. }
  2231. break;
  2232. }
  2233. }
  2234. }
  2235. static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
  2236. int *got_output, AVPacket *avpkt)
  2237. {
  2238. const uint8_t *buf = avpkt->data;
  2239. int buf_size = avpkt->size;
  2240. Mpeg1Context *s = avctx->priv_data;
  2241. AVFrame *picture = data;
  2242. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2243. ff_dlog(avctx, "fill_buffer\n");
  2244. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  2245. /* special case for last picture */
  2246. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  2247. int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
  2248. if (ret < 0)
  2249. return ret;
  2250. s2->next_picture_ptr = NULL;
  2251. *got_output = 1;
  2252. }
  2253. return buf_size;
  2254. }
  2255. if (s2->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
  2256. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
  2257. buf_size, NULL);
  2258. if (ff_combine_frame(&s2->parse_context, next,
  2259. (const uint8_t **) &buf, &buf_size) < 0)
  2260. return buf_size;
  2261. }
  2262. if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
  2263. vcr2_init_sequence(avctx);
  2264. s->slice_count = 0;
  2265. if (avctx->extradata && !s->extradata_decoded) {
  2266. int ret = decode_chunks(avctx, picture, got_output,
  2267. avctx->extradata, avctx->extradata_size);
  2268. s->extradata_decoded = 1;
  2269. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  2270. return ret;
  2271. }
  2272. return decode_chunks(avctx, picture, got_output, buf, buf_size);
  2273. }
  2274. static void flush(AVCodecContext *avctx)
  2275. {
  2276. Mpeg1Context *s = avctx->priv_data;
  2277. s->sync = 0;
  2278. s->closed_gop = 0;
  2279. ff_mpeg_flush(avctx);
  2280. }
  2281. static av_cold int mpeg_decode_end(AVCodecContext *avctx)
  2282. {
  2283. Mpeg1Context *s = avctx->priv_data;
  2284. if (s->mpeg_enc_ctx_allocated)
  2285. ff_mpv_common_end(&s->mpeg_enc_ctx);
  2286. av_freep(&s->a53_caption);
  2287. return 0;
  2288. }
  2289. AVCodec ff_mpeg1video_decoder = {
  2290. .name = "mpeg1video",
  2291. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2292. .type = AVMEDIA_TYPE_VIDEO,
  2293. .id = AV_CODEC_ID_MPEG1VIDEO,
  2294. .priv_data_size = sizeof(Mpeg1Context),
  2295. .init = mpeg_decode_init,
  2296. .close = mpeg_decode_end,
  2297. .decode = mpeg_decode_frame,
  2298. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  2299. AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
  2300. AV_CODEC_CAP_SLICE_THREADS,
  2301. .flush = flush,
  2302. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2303. };
  2304. AVCodec ff_mpeg2video_decoder = {
  2305. .name = "mpeg2video",
  2306. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2307. .type = AVMEDIA_TYPE_VIDEO,
  2308. .id = AV_CODEC_ID_MPEG2VIDEO,
  2309. .priv_data_size = sizeof(Mpeg1Context),
  2310. .init = mpeg_decode_init,
  2311. .close = mpeg_decode_end,
  2312. .decode = mpeg_decode_frame,
  2313. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  2314. AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
  2315. AV_CODEC_CAP_SLICE_THREADS,
  2316. .flush = flush,
  2317. .profiles = NULL_IF_CONFIG_SMALL(ff_mpeg2_video_profiles),
  2318. };