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.

471 lines
15KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/avassert.h"
  19. #include "cbs.h"
  20. #include "cbs_internal.h"
  21. #include "cbs_mpeg2.h"
  22. #include "internal.h"
  23. #define HEADER(name) do { \
  24. ff_cbs_trace_header(ctx, name); \
  25. } while (0)
  26. #define CHECK(call) do { \
  27. err = (call); \
  28. if (err < 0) \
  29. return err; \
  30. } while (0)
  31. #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
  32. #define FUNC_MPEG2(rw, name) FUNC_NAME(rw, mpeg2, name)
  33. #define FUNC(name) FUNC_MPEG2(READWRITE, name)
  34. #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
  35. #define ui(width, name) \
  36. xui(width, name, current->name, 0, MAX_UINT_BITS(width), 0)
  37. #define uir(width, name) \
  38. xui(width, name, current->name, 1, MAX_UINT_BITS(width), 0)
  39. #define uis(width, name, subs, ...) \
  40. xui(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
  41. #define uirs(width, name, subs, ...) \
  42. xui(width, name, current->name, 1, MAX_UINT_BITS(width), subs, __VA_ARGS__)
  43. #define xui(width, name, var, range_min, range_max, subs, ...) \
  44. xuia(width, #name, var, range_min, range_max, subs, __VA_ARGS__)
  45. #define sis(width, name, subs, ...) \
  46. xsi(width, name, current->name, subs, __VA_ARGS__)
  47. #define marker_bit() \
  48. bit("marker_bit", 1)
  49. #define bit(string, value) do { \
  50. av_unused uint32_t bit = value; \
  51. xuia(1, string, bit, value, value, 0); \
  52. } while (0)
  53. #define READ
  54. #define READWRITE read
  55. #define RWContext GetBitContext
  56. #define xuia(width, string, var, range_min, range_max, subs, ...) do { \
  57. uint32_t value; \
  58. CHECK(ff_cbs_read_unsigned(ctx, rw, width, string, \
  59. SUBSCRIPTS(subs, __VA_ARGS__), \
  60. &value, range_min, range_max)); \
  61. var = value; \
  62. } while (0)
  63. #define xsi(width, name, var, subs, ...) do { \
  64. int32_t value; \
  65. CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
  66. SUBSCRIPTS(subs, __VA_ARGS__), &value, \
  67. MIN_INT_BITS(width), \
  68. MAX_INT_BITS(width))); \
  69. var = value; \
  70. } while (0)
  71. #define nextbits(width, compare, var) \
  72. (get_bits_left(rw) >= width && \
  73. (var = show_bits(rw, width)) == (compare))
  74. #define infer(name, value) do { \
  75. current->name = value; \
  76. } while (0)
  77. #include "cbs_mpeg2_syntax_template.c"
  78. #undef READ
  79. #undef READWRITE
  80. #undef RWContext
  81. #undef xuia
  82. #undef xsi
  83. #undef nextbits
  84. #undef infer
  85. #define WRITE
  86. #define READWRITE write
  87. #define RWContext PutBitContext
  88. #define xuia(width, string, var, range_min, range_max, subs, ...) do { \
  89. CHECK(ff_cbs_write_unsigned(ctx, rw, width, string, \
  90. SUBSCRIPTS(subs, __VA_ARGS__), \
  91. var, range_min, range_max)); \
  92. } while (0)
  93. #define xsi(width, name, var, subs, ...) do { \
  94. CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
  95. SUBSCRIPTS(subs, __VA_ARGS__), var, \
  96. MIN_INT_BITS(width), \
  97. MAX_INT_BITS(width))); \
  98. } while (0)
  99. #define nextbits(width, compare, var) (var)
  100. #define infer(name, value) do { \
  101. if (current->name != (value)) { \
  102. av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
  103. "%s does not match inferred value: " \
  104. "%"PRId64", but should be %"PRId64".\n", \
  105. #name, (int64_t)current->name, (int64_t)(value)); \
  106. } \
  107. } while (0)
  108. #include "cbs_mpeg2_syntax_template.c"
  109. #undef WRITE
  110. #undef READWRITE
  111. #undef RWContext
  112. #undef xuia
  113. #undef xsi
  114. #undef nextbits
  115. #undef infer
  116. static void cbs_mpeg2_free_picture_header(void *opaque, uint8_t *content)
  117. {
  118. MPEG2RawPictureHeader *picture = (MPEG2RawPictureHeader*)content;
  119. av_buffer_unref(&picture->extra_information_picture.extra_information_ref);
  120. av_freep(&content);
  121. }
  122. static void cbs_mpeg2_free_user_data(void *opaque, uint8_t *content)
  123. {
  124. MPEG2RawUserData *user = (MPEG2RawUserData*)content;
  125. av_buffer_unref(&user->user_data_ref);
  126. av_freep(&content);
  127. }
  128. static void cbs_mpeg2_free_slice(void *opaque, uint8_t *content)
  129. {
  130. MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
  131. av_buffer_unref(&slice->header.extra_information_slice.extra_information_ref);
  132. av_buffer_unref(&slice->data_ref);
  133. av_freep(&content);
  134. }
  135. static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
  136. CodedBitstreamFragment *frag,
  137. int header)
  138. {
  139. const uint8_t *start, *end;
  140. uint8_t *unit_data;
  141. uint32_t start_code = -1, next_start_code = -1;
  142. size_t unit_size;
  143. int err, i, unit_type;
  144. start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
  145. &start_code);
  146. for (i = 0;; i++) {
  147. end = avpriv_find_start_code(start, frag->data + frag->data_size,
  148. &next_start_code);
  149. unit_type = start_code & 0xff;
  150. // The start and end pointers point at to the byte following the
  151. // start_code_identifier in the start code that they found.
  152. if (end == frag->data + frag->data_size) {
  153. // We didn't find a start code, so this is the final unit.
  154. unit_size = end - (start - 1);
  155. } else {
  156. // Unit runs from start to the beginning of the start code
  157. // pointed to by end (including any padding zeroes).
  158. unit_size = (end - 4) - (start - 1);
  159. }
  160. unit_data = (uint8_t *)start - 1;
  161. err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
  162. unit_data, unit_size, frag->data_ref);
  163. if (err < 0)
  164. return err;
  165. if (end == frag->data + frag->data_size)
  166. break;
  167. start_code = next_start_code;
  168. start = end;
  169. }
  170. return 0;
  171. }
  172. static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
  173. CodedBitstreamUnit *unit)
  174. {
  175. GetBitContext gbc;
  176. int err;
  177. err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
  178. if (err < 0)
  179. return err;
  180. if (MPEG2_START_IS_SLICE(unit->type)) {
  181. MPEG2RawSlice *slice;
  182. int pos, len;
  183. err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
  184. &cbs_mpeg2_free_slice);
  185. if (err < 0)
  186. return err;
  187. slice = unit->content;
  188. err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
  189. if (err < 0)
  190. return err;
  191. pos = get_bits_count(&gbc);
  192. len = unit->data_size;
  193. slice->data_size = len - pos / 8;
  194. slice->data_ref = av_buffer_ref(unit->data_ref);
  195. if (!slice->data_ref)
  196. return AVERROR(ENOMEM);
  197. slice->data = unit->data + pos / 8;
  198. slice->data_bit_start = pos % 8;
  199. } else {
  200. switch (unit->type) {
  201. #define START(start_code, type, read_func, free_func) \
  202. case start_code: \
  203. { \
  204. type *header; \
  205. err = ff_cbs_alloc_unit_content(ctx, unit, \
  206. sizeof(*header), free_func); \
  207. if (err < 0) \
  208. return err; \
  209. header = unit->content; \
  210. err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
  211. if (err < 0) \
  212. return err; \
  213. } \
  214. break;
  215. START(MPEG2_START_PICTURE, MPEG2RawPictureHeader,
  216. picture_header, &cbs_mpeg2_free_picture_header);
  217. START(MPEG2_START_USER_DATA, MPEG2RawUserData,
  218. user_data, &cbs_mpeg2_free_user_data);
  219. START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader,
  220. sequence_header, NULL);
  221. START(MPEG2_START_EXTENSION, MPEG2RawExtensionData,
  222. extension_data, NULL);
  223. START(MPEG2_START_GROUP, MPEG2RawGroupOfPicturesHeader,
  224. group_of_pictures_header, NULL);
  225. START(MPEG2_START_SEQUENCE_END, MPEG2RawSequenceEnd,
  226. sequence_end, NULL);
  227. #undef START
  228. default:
  229. return AVERROR(ENOSYS);
  230. }
  231. }
  232. return 0;
  233. }
  234. static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
  235. CodedBitstreamUnit *unit,
  236. PutBitContext *pbc)
  237. {
  238. int err;
  239. switch (unit->type) {
  240. #define START(start_code, type, func) \
  241. case start_code: \
  242. err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
  243. break;
  244. START(MPEG2_START_PICTURE, MPEG2RawPictureHeader, picture_header);
  245. START(MPEG2_START_USER_DATA, MPEG2RawUserData, user_data);
  246. START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader, sequence_header);
  247. START(MPEG2_START_EXTENSION, MPEG2RawExtensionData, extension_data);
  248. START(MPEG2_START_GROUP, MPEG2RawGroupOfPicturesHeader,
  249. group_of_pictures_header);
  250. START(MPEG2_START_SEQUENCE_END, MPEG2RawSequenceEnd, sequence_end);
  251. #undef START
  252. default:
  253. av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
  254. "code %02"PRIx32".\n", unit->type);
  255. return AVERROR_PATCHWELCOME;
  256. }
  257. return err;
  258. }
  259. static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
  260. CodedBitstreamUnit *unit,
  261. PutBitContext *pbc)
  262. {
  263. MPEG2RawSlice *slice = unit->content;
  264. int err;
  265. err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
  266. if (err < 0)
  267. return err;
  268. if (slice->data) {
  269. size_t rest = slice->data_size - (slice->data_bit_start + 7) / 8;
  270. uint8_t *pos = slice->data + slice->data_bit_start / 8;
  271. av_assert0(slice->data_bit_start >= 0 &&
  272. 8 * slice->data_size > slice->data_bit_start);
  273. if (slice->data_size * 8 + 8 > put_bits_left(pbc))
  274. return AVERROR(ENOSPC);
  275. // First copy the remaining bits of the first byte
  276. if (slice->data_bit_start % 8)
  277. put_bits(pbc, 8 - slice->data_bit_start % 8,
  278. *pos++ & MAX_UINT_BITS(8 - slice->data_bit_start % 8));
  279. if (put_bits_count(pbc) % 8 == 0) {
  280. // If the writer is aligned at this point,
  281. // memcpy can be used to improve performance.
  282. // This is the normal case.
  283. flush_put_bits(pbc);
  284. memcpy(put_bits_ptr(pbc), pos, rest);
  285. skip_put_bytes(pbc, rest);
  286. } else {
  287. // If not, we have to copy manually:
  288. for (; rest > 3; rest -= 4, pos += 4)
  289. put_bits32(pbc, AV_RB32(pos));
  290. for (; rest; rest--, pos++)
  291. put_bits(pbc, 8, *pos);
  292. // Align with zeros
  293. put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
  294. }
  295. }
  296. return 0;
  297. }
  298. static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
  299. CodedBitstreamUnit *unit)
  300. {
  301. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  302. PutBitContext pbc;
  303. int err;
  304. if (!priv->write_buffer) {
  305. // Initial write buffer size is 1MB.
  306. priv->write_buffer_size = 1024 * 1024;
  307. reallocate_and_try_again:
  308. err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
  309. if (err < 0) {
  310. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
  311. "sufficiently large write buffer (last attempt "
  312. "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
  313. return err;
  314. }
  315. }
  316. init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
  317. if (MPEG2_START_IS_SLICE(unit->type))
  318. err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
  319. else
  320. err = cbs_mpeg2_write_header(ctx, unit, &pbc);
  321. if (err == AVERROR(ENOSPC)) {
  322. // Overflow.
  323. priv->write_buffer_size *= 2;
  324. goto reallocate_and_try_again;
  325. }
  326. if (err < 0) {
  327. // Write failed for some other reason.
  328. return err;
  329. }
  330. if (put_bits_count(&pbc) % 8)
  331. unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
  332. else
  333. unit->data_bit_padding = 0;
  334. unit->data_size = (put_bits_count(&pbc) + 7) / 8;
  335. flush_put_bits(&pbc);
  336. err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
  337. if (err < 0)
  338. return err;
  339. memcpy(unit->data, priv->write_buffer, unit->data_size);
  340. return 0;
  341. }
  342. static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
  343. CodedBitstreamFragment *frag)
  344. {
  345. uint8_t *data;
  346. size_t size, dp;
  347. int i;
  348. size = 0;
  349. for (i = 0; i < frag->nb_units; i++)
  350. size += 3 + frag->units[i].data_size;
  351. frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  352. if (!frag->data_ref)
  353. return AVERROR(ENOMEM);
  354. data = frag->data_ref->data;
  355. dp = 0;
  356. for (i = 0; i < frag->nb_units; i++) {
  357. CodedBitstreamUnit *unit = &frag->units[i];
  358. data[dp++] = 0;
  359. data[dp++] = 0;
  360. data[dp++] = 1;
  361. memcpy(data + dp, unit->data, unit->data_size);
  362. dp += unit->data_size;
  363. }
  364. av_assert0(dp == size);
  365. memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  366. frag->data = data;
  367. frag->data_size = size;
  368. return 0;
  369. }
  370. static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
  371. {
  372. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  373. av_freep(&priv->write_buffer);
  374. }
  375. const CodedBitstreamType ff_cbs_type_mpeg2 = {
  376. .codec_id = AV_CODEC_ID_MPEG2VIDEO,
  377. .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
  378. .split_fragment = &cbs_mpeg2_split_fragment,
  379. .read_unit = &cbs_mpeg2_read_unit,
  380. .write_unit = &cbs_mpeg2_write_unit,
  381. .assemble_fragment = &cbs_mpeg2_assemble_fragment,
  382. .close = &cbs_mpeg2_close,
  383. };