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.

422 lines
13KB

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