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.

416 lines
12KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 READ
  35. #define READWRITE read
  36. #define RWContext BitstreamContext
  37. #define xui(width, name, var) do { \
  38. uint32_t value = 0; \
  39. CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
  40. &value, 0, (1 << width) - 1)); \
  41. var = value; \
  42. } while (0)
  43. #define ui(width, name) \
  44. xui(width, name, current->name)
  45. #define marker_bit() do { \
  46. av_unused uint32_t one; \
  47. CHECK(ff_cbs_read_unsigned(ctx, rw, 1, "marker_bit", &one, 1, 1)); \
  48. } while (0)
  49. #define nextbits(width, compare, var) \
  50. (bitstream_bits_left(rw) >= width && \
  51. (var = bitstream_peek(rw, width)) == (compare))
  52. #include "cbs_mpeg2_syntax_template.c"
  53. #undef READ
  54. #undef READWRITE
  55. #undef RWContext
  56. #undef xui
  57. #undef ui
  58. #undef marker_bit
  59. #undef nextbits
  60. #define WRITE
  61. #define READWRITE write
  62. #define RWContext PutBitContext
  63. #define xui(width, name, var) do { \
  64. CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
  65. var, 0, (1 << width) - 1)); \
  66. } while (0)
  67. #define ui(width, name) \
  68. xui(width, name, current->name)
  69. #define marker_bit() do { \
  70. CHECK(ff_cbs_write_unsigned(ctx, rw, 1, "marker_bit", 1, 1, 1)); \
  71. } while (0)
  72. #define nextbits(width, compare, var) (var)
  73. #include "cbs_mpeg2_syntax_template.c"
  74. #undef READ
  75. #undef READWRITE
  76. #undef RWContext
  77. #undef xui
  78. #undef ui
  79. #undef marker_bit
  80. #undef nextbits
  81. static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)
  82. {
  83. MPEG2RawUserData *user = (MPEG2RawUserData*)content;
  84. av_buffer_unref(&user->user_data_ref);
  85. av_freep(&content);
  86. }
  87. static void cbs_mpeg2_free_slice(void *unit, uint8_t *content)
  88. {
  89. MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
  90. av_buffer_unref(&slice->header.extra_information_ref);
  91. av_buffer_unref(&slice->data_ref);
  92. av_freep(&content);
  93. }
  94. static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
  95. CodedBitstreamFragment *frag,
  96. int header)
  97. {
  98. const uint8_t *start, *end;
  99. uint8_t *unit_data;
  100. uint32_t start_code = -1, next_start_code = -1;
  101. size_t unit_size;
  102. int err, i, unit_type;
  103. start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
  104. &start_code);
  105. for (i = 0;; i++) {
  106. end = avpriv_find_start_code(start, frag->data + frag->data_size,
  107. &next_start_code);
  108. unit_type = start_code & 0xff;
  109. // The start and end pointers point at to the byte following the
  110. // start_code_identifier in the start code that they found.
  111. if (end == frag->data + frag->data_size) {
  112. // We didn't find a start code, so this is the final unit.
  113. unit_size = end - (start - 1);
  114. } else {
  115. // Unit runs from start to the beginning of the start code
  116. // pointed to by end (including any padding zeroes).
  117. unit_size = (end - 4) - (start - 1);
  118. }
  119. unit_data = av_malloc(unit_size + AV_INPUT_BUFFER_PADDING_SIZE);
  120. if (!unit_data)
  121. return AVERROR(ENOMEM);
  122. memcpy(unit_data, start - 1, unit_size);
  123. memset(unit_data + unit_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  124. err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
  125. unit_data, unit_size, NULL);
  126. if (err < 0) {
  127. av_freep(&unit_data);
  128. return err;
  129. }
  130. if (end == frag->data + frag->data_size)
  131. break;
  132. start_code = next_start_code;
  133. start = end;
  134. }
  135. return 0;
  136. }
  137. static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
  138. CodedBitstreamUnit *unit)
  139. {
  140. BitstreamContext bc;
  141. int err;
  142. err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
  143. if (err < 0)
  144. return err;
  145. if (MPEG2_START_IS_SLICE(unit->type)) {
  146. MPEG2RawSlice *slice;
  147. int pos, len;
  148. err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
  149. &cbs_mpeg2_free_slice);
  150. if (err < 0)
  151. return err;
  152. slice = unit->content;
  153. err = cbs_mpeg2_read_slice_header(ctx, &bc, &slice->header);
  154. if (err < 0)
  155. return err;
  156. pos = bitstream_tell(&bc);
  157. len = unit->data_size;
  158. slice->data_size = len - pos / 8;
  159. slice->data_ref = av_buffer_alloc(slice->data_size +
  160. AV_INPUT_BUFFER_PADDING_SIZE);
  161. if (!slice->data_ref)
  162. return AVERROR(ENOMEM);
  163. slice->data = slice->data_ref->data;
  164. memcpy(slice->data,
  165. unit->data + pos / 8, slice->data_size);
  166. memset(slice->data + slice->data_size, 0,
  167. AV_INPUT_BUFFER_PADDING_SIZE);
  168. slice->data_bit_start = pos % 8;
  169. } else {
  170. switch (unit->type) {
  171. #define START(start_code, type, read_func, free_func) \
  172. case start_code: \
  173. { \
  174. type *header; \
  175. err = ff_cbs_alloc_unit_content(ctx, unit, \
  176. sizeof(*header), free_func); \
  177. if (err < 0) \
  178. return err; \
  179. header = unit->content; \
  180. err = cbs_mpeg2_read_ ## read_func(ctx, &bc, header); \
  181. if (err < 0) \
  182. return err; \
  183. } \
  184. break;
  185. START(0x00, MPEG2RawPictureHeader, picture_header, NULL);
  186. START(0xb2, MPEG2RawUserData, user_data,
  187. &cbs_mpeg2_free_user_data);
  188. START(0xb3, MPEG2RawSequenceHeader, sequence_header, NULL);
  189. START(0xb5, MPEG2RawExtensionData, extension_data, NULL);
  190. START(0xb8, MPEG2RawGroupOfPicturesHeader,
  191. group_of_pictures_header, NULL);
  192. #undef START
  193. default:
  194. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unknown start code %02"PRIx32".\n",
  195. unit->type);
  196. return AVERROR_INVALIDDATA;
  197. }
  198. }
  199. return 0;
  200. }
  201. static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
  202. CodedBitstreamUnit *unit,
  203. PutBitContext *pbc)
  204. {
  205. int err;
  206. switch (unit->type) {
  207. #define START(start_code, type, func) \
  208. case start_code: \
  209. err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
  210. break;
  211. START(0x00, MPEG2RawPictureHeader, picture_header);
  212. START(0xb2, MPEG2RawUserData, user_data);
  213. START(0xb3, MPEG2RawSequenceHeader, sequence_header);
  214. START(0xb5, MPEG2RawExtensionData, extension_data);
  215. START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
  216. #undef START
  217. default:
  218. av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
  219. "code %02"PRIx32".\n", unit->type);
  220. return AVERROR_PATCHWELCOME;
  221. }
  222. return err;
  223. }
  224. static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
  225. CodedBitstreamUnit *unit,
  226. PutBitContext *pbc)
  227. {
  228. MPEG2RawSlice *slice = unit->content;
  229. BitstreamContext bc;
  230. size_t bits_left;
  231. int err;
  232. err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
  233. if (err < 0)
  234. return err;
  235. if (slice->data) {
  236. if (slice->data_size * 8 + 8 > put_bits_left(pbc))
  237. return AVERROR(ENOSPC);
  238. bitstream_init(&bc, slice->data, slice->data_size * 8);
  239. bitstream_skip(&bc, slice->data_bit_start);
  240. while (bitstream_bits_left(&bc) > 15)
  241. put_bits(pbc, 16, bitstream_read(&bc, 16));
  242. bits_left = bitstream_bits_left(&bc);
  243. put_bits(pbc, bits_left, bitstream_read(&bc, bits_left));
  244. // Align with zeroes.
  245. while (put_bits_count(pbc) % 8 != 0)
  246. put_bits(pbc, 1, 0);
  247. }
  248. return 0;
  249. }
  250. static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
  251. CodedBitstreamUnit *unit)
  252. {
  253. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  254. PutBitContext pbc;
  255. int err;
  256. if (!priv->write_buffer) {
  257. // Initial write buffer size is 1MB.
  258. priv->write_buffer_size = 1024 * 1024;
  259. reallocate_and_try_again:
  260. err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
  261. if (err < 0) {
  262. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
  263. "sufficiently large write buffer (last attempt "
  264. "%zu bytes).\n", priv->write_buffer_size);
  265. return err;
  266. }
  267. }
  268. init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
  269. if (unit->type >= 0x01 && unit->type <= 0xaf)
  270. err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
  271. else
  272. err = cbs_mpeg2_write_header(ctx, unit, &pbc);
  273. if (err == AVERROR(ENOSPC)) {
  274. // Overflow.
  275. priv->write_buffer_size *= 2;
  276. goto reallocate_and_try_again;
  277. }
  278. if (err < 0) {
  279. // Write failed for some other reason.
  280. return err;
  281. }
  282. if (put_bits_count(&pbc) % 8)
  283. unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
  284. else
  285. unit->data_bit_padding = 0;
  286. unit->data_size = (put_bits_count(&pbc) + 7) / 8;
  287. flush_put_bits(&pbc);
  288. err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
  289. if (err < 0)
  290. return err;
  291. memcpy(unit->data, priv->write_buffer, unit->data_size);
  292. return 0;
  293. }
  294. static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
  295. CodedBitstreamFragment *frag)
  296. {
  297. uint8_t *data;
  298. size_t size, dp, sp;
  299. int i;
  300. size = 0;
  301. for (i = 0; i < frag->nb_units; i++)
  302. size += 3 + frag->units[i].data_size;
  303. frag->data_ref = av_buffer_alloc(size);
  304. if (!frag->data_ref)
  305. return AVERROR(ENOMEM);
  306. data = frag->data_ref->data;
  307. dp = 0;
  308. for (i = 0; i < frag->nb_units; i++) {
  309. CodedBitstreamUnit *unit = &frag->units[i];
  310. data[dp++] = 0;
  311. data[dp++] = 0;
  312. data[dp++] = 1;
  313. for (sp = 0; sp < unit->data_size; sp++)
  314. data[dp++] = unit->data[sp];
  315. }
  316. av_assert0(dp == size);
  317. frag->data = data;
  318. frag->data_size = size;
  319. return 0;
  320. }
  321. static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
  322. {
  323. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  324. av_freep(&priv->write_buffer);
  325. }
  326. const CodedBitstreamType ff_cbs_type_mpeg2 = {
  327. .codec_id = AV_CODEC_ID_MPEG2VIDEO,
  328. .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
  329. .split_fragment = &cbs_mpeg2_split_fragment,
  330. .read_unit = &cbs_mpeg2_read_unit,
  331. .write_unit = &cbs_mpeg2_write_unit,
  332. .assemble_fragment = &cbs_mpeg2_assemble_fragment,
  333. .close = &cbs_mpeg2_close,
  334. };