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.

463 lines
14KB

  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 sis(width, name, subs, ...) \
  44. xsi(width, name, current->name, subs, __VA_ARGS__)
  45. #define READ
  46. #define READWRITE read
  47. #define RWContext GetBitContext
  48. #define xui(width, name, var, range_min, range_max, subs, ...) do { \
  49. uint32_t value; \
  50. CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
  51. SUBSCRIPTS(subs, __VA_ARGS__), \
  52. &value, range_min, range_max)); \
  53. var = value; \
  54. } while (0)
  55. #define xsi(width, name, var, subs, ...) do { \
  56. int32_t value; \
  57. CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
  58. SUBSCRIPTS(subs, __VA_ARGS__), &value, \
  59. MIN_INT_BITS(width), \
  60. MAX_INT_BITS(width))); \
  61. var = value; \
  62. } while (0)
  63. #define marker_bit() do { \
  64. av_unused uint32_t one; \
  65. CHECK(ff_cbs_read_unsigned(ctx, rw, 1, "marker_bit", NULL, &one, 1, 1)); \
  66. } while (0)
  67. #define nextbits(width, compare, var) \
  68. (get_bits_left(rw) >= width && \
  69. (var = show_bits(rw, width)) == (compare))
  70. #define infer(name, value) do { \
  71. current->name = value; \
  72. } while (0)
  73. #include "cbs_mpeg2_syntax_template.c"
  74. #undef READ
  75. #undef READWRITE
  76. #undef RWContext
  77. #undef xui
  78. #undef xsi
  79. #undef marker_bit
  80. #undef nextbits
  81. #undef infer
  82. #define WRITE
  83. #define READWRITE write
  84. #define RWContext PutBitContext
  85. #define xui(width, name, var, range_min, range_max, subs, ...) do { \
  86. CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
  87. SUBSCRIPTS(subs, __VA_ARGS__), \
  88. var, range_min, range_max)); \
  89. } while (0)
  90. #define xsi(width, name, var, subs, ...) do { \
  91. CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
  92. SUBSCRIPTS(subs, __VA_ARGS__), var, \
  93. MIN_INT_BITS(width), \
  94. MAX_INT_BITS(width))); \
  95. } while (0)
  96. #define marker_bit() do { \
  97. CHECK(ff_cbs_write_unsigned(ctx, rw, 1, "marker_bit", NULL, 1, 1, 1)); \
  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 xui
  113. #undef xsi
  114. #undef marker_bit
  115. #undef nextbits
  116. #undef infer
  117. static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)
  118. {
  119. MPEG2RawUserData *user = (MPEG2RawUserData*)content;
  120. av_buffer_unref(&user->user_data_ref);
  121. av_freep(&content);
  122. }
  123. static void cbs_mpeg2_free_slice(void *unit, uint8_t *content)
  124. {
  125. MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
  126. av_buffer_unref(&slice->header.extra_information_ref);
  127. av_buffer_unref(&slice->data_ref);
  128. av_freep(&content);
  129. }
  130. static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
  131. CodedBitstreamFragment *frag,
  132. int header)
  133. {
  134. const uint8_t *start, *end;
  135. uint8_t *unit_data;
  136. uint32_t start_code = -1, next_start_code = -1;
  137. size_t unit_size;
  138. int err, i, unit_type;
  139. start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
  140. &start_code);
  141. for (i = 0;; i++) {
  142. end = avpriv_find_start_code(start, frag->data + frag->data_size,
  143. &next_start_code);
  144. unit_type = start_code & 0xff;
  145. // The start and end pointers point at to the byte following the
  146. // start_code_identifier in the start code that they found.
  147. if (end == frag->data + frag->data_size) {
  148. // We didn't find a start code, so this is the final unit.
  149. unit_size = end - (start - 1);
  150. } else {
  151. // Unit runs from start to the beginning of the start code
  152. // pointed to by end (including any padding zeroes).
  153. unit_size = (end - 4) - (start - 1);
  154. }
  155. unit_data = (uint8_t *)start - 1;
  156. err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
  157. unit_data, unit_size, frag->data_ref);
  158. if (err < 0)
  159. return err;
  160. if (end == frag->data + frag->data_size)
  161. break;
  162. start_code = next_start_code;
  163. start = end;
  164. }
  165. return 0;
  166. }
  167. static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
  168. CodedBitstreamUnit *unit)
  169. {
  170. GetBitContext gbc;
  171. int err;
  172. err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
  173. if (err < 0)
  174. return err;
  175. if (MPEG2_START_IS_SLICE(unit->type)) {
  176. MPEG2RawSlice *slice;
  177. int pos, len;
  178. err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
  179. &cbs_mpeg2_free_slice);
  180. if (err < 0)
  181. return err;
  182. slice = unit->content;
  183. err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
  184. if (err < 0)
  185. return err;
  186. pos = get_bits_count(&gbc);
  187. len = unit->data_size;
  188. slice->data_size = len - pos / 8;
  189. slice->data_ref = av_buffer_ref(unit->data_ref);
  190. if (!slice->data_ref)
  191. return AVERROR(ENOMEM);
  192. slice->data = unit->data + pos / 8;
  193. slice->data_bit_start = pos % 8;
  194. } else {
  195. switch (unit->type) {
  196. #define START(start_code, type, read_func, free_func) \
  197. case start_code: \
  198. { \
  199. type *header; \
  200. err = ff_cbs_alloc_unit_content(ctx, unit, \
  201. sizeof(*header), free_func); \
  202. if (err < 0) \
  203. return err; \
  204. header = unit->content; \
  205. err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
  206. if (err < 0) \
  207. return err; \
  208. } \
  209. break;
  210. START(MPEG2_START_PICTURE, MPEG2RawPictureHeader,
  211. picture_header, NULL);
  212. START(MPEG2_START_USER_DATA, MPEG2RawUserData,
  213. user_data, &cbs_mpeg2_free_user_data);
  214. START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader,
  215. sequence_header, NULL);
  216. START(MPEG2_START_EXTENSION, MPEG2RawExtensionData,
  217. extension_data, NULL);
  218. START(MPEG2_START_GROUP, MPEG2RawGroupOfPicturesHeader,
  219. group_of_pictures_header, NULL);
  220. #undef START
  221. default:
  222. return AVERROR(ENOSYS);
  223. }
  224. }
  225. return 0;
  226. }
  227. static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
  228. CodedBitstreamUnit *unit,
  229. PutBitContext *pbc)
  230. {
  231. int err;
  232. switch (unit->type) {
  233. #define START(start_code, type, func) \
  234. case start_code: \
  235. err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
  236. break;
  237. START(MPEG2_START_PICTURE, MPEG2RawPictureHeader, picture_header);
  238. START(MPEG2_START_USER_DATA, MPEG2RawUserData, user_data);
  239. START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader, sequence_header);
  240. START(MPEG2_START_EXTENSION, MPEG2RawExtensionData, extension_data);
  241. START(MPEG2_START_GROUP, MPEG2RawGroupOfPicturesHeader,
  242. group_of_pictures_header);
  243. #undef START
  244. default:
  245. av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
  246. "code %02"PRIx32".\n", unit->type);
  247. return AVERROR_PATCHWELCOME;
  248. }
  249. return err;
  250. }
  251. static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
  252. CodedBitstreamUnit *unit,
  253. PutBitContext *pbc)
  254. {
  255. MPEG2RawSlice *slice = unit->content;
  256. int err;
  257. err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
  258. if (err < 0)
  259. return err;
  260. if (slice->data) {
  261. size_t rest = slice->data_size - (slice->data_bit_start + 7) / 8;
  262. uint8_t *pos = slice->data + slice->data_bit_start / 8;
  263. av_assert0(slice->data_bit_start >= 0 &&
  264. 8 * slice->data_size > slice->data_bit_start);
  265. if (slice->data_size * 8 + 8 > put_bits_left(pbc))
  266. return AVERROR(ENOSPC);
  267. // First copy the remaining bits of the first byte
  268. if (slice->data_bit_start % 8)
  269. put_bits(pbc, 8 - slice->data_bit_start % 8,
  270. *pos++ & MAX_UINT_BITS(8 - slice->data_bit_start % 8));
  271. if (put_bits_count(pbc) % 8 == 0) {
  272. // If the writer is aligned at this point,
  273. // memcpy can be used to improve performance.
  274. // This is the normal case.
  275. flush_put_bits(pbc);
  276. memcpy(put_bits_ptr(pbc), pos, rest);
  277. skip_put_bytes(pbc, rest);
  278. } else {
  279. // If not, we have to copy manually:
  280. for (; rest > 3; rest -= 4, pos += 4)
  281. put_bits32(pbc, AV_RB32(pos));
  282. for (; rest; rest--, pos++)
  283. put_bits(pbc, 8, *pos);
  284. // Align with zeros
  285. put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
  286. }
  287. }
  288. return 0;
  289. }
  290. static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
  291. CodedBitstreamUnit *unit)
  292. {
  293. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  294. PutBitContext pbc;
  295. int err;
  296. if (!priv->write_buffer) {
  297. // Initial write buffer size is 1MB.
  298. priv->write_buffer_size = 1024 * 1024;
  299. reallocate_and_try_again:
  300. err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
  301. if (err < 0) {
  302. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
  303. "sufficiently large write buffer (last attempt "
  304. "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
  305. return err;
  306. }
  307. }
  308. init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
  309. if (MPEG2_START_IS_SLICE(unit->type))
  310. err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
  311. else
  312. err = cbs_mpeg2_write_header(ctx, unit, &pbc);
  313. if (err == AVERROR(ENOSPC)) {
  314. // Overflow.
  315. priv->write_buffer_size *= 2;
  316. goto reallocate_and_try_again;
  317. }
  318. if (err < 0) {
  319. // Write failed for some other reason.
  320. return err;
  321. }
  322. if (put_bits_count(&pbc) % 8)
  323. unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
  324. else
  325. unit->data_bit_padding = 0;
  326. unit->data_size = (put_bits_count(&pbc) + 7) / 8;
  327. flush_put_bits(&pbc);
  328. err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
  329. if (err < 0)
  330. return err;
  331. memcpy(unit->data, priv->write_buffer, unit->data_size);
  332. return 0;
  333. }
  334. static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
  335. CodedBitstreamFragment *frag)
  336. {
  337. uint8_t *data;
  338. size_t size, dp;
  339. int i;
  340. size = 0;
  341. for (i = 0; i < frag->nb_units; i++)
  342. size += 3 + frag->units[i].data_size;
  343. frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  344. if (!frag->data_ref)
  345. return AVERROR(ENOMEM);
  346. data = frag->data_ref->data;
  347. dp = 0;
  348. for (i = 0; i < frag->nb_units; i++) {
  349. CodedBitstreamUnit *unit = &frag->units[i];
  350. data[dp++] = 0;
  351. data[dp++] = 0;
  352. data[dp++] = 1;
  353. memcpy(data + dp, unit->data, unit->data_size);
  354. dp += unit->data_size;
  355. }
  356. av_assert0(dp == size);
  357. memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  358. frag->data = data;
  359. frag->data_size = size;
  360. return 0;
  361. }
  362. static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
  363. {
  364. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  365. av_freep(&priv->write_buffer);
  366. }
  367. const CodedBitstreamType ff_cbs_type_mpeg2 = {
  368. .codec_id = AV_CODEC_ID_MPEG2VIDEO,
  369. .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
  370. .split_fragment = &cbs_mpeg2_split_fragment,
  371. .read_unit = &cbs_mpeg2_read_unit,
  372. .write_unit = &cbs_mpeg2_write_unit,
  373. .assemble_fragment = &cbs_mpeg2_assemble_fragment,
  374. .close = &cbs_mpeg2_close,
  375. };