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.

407 lines
12KB

  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. GetBitContext gbc;
  220. size_t bits_left;
  221. int err;
  222. err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
  223. if (err < 0)
  224. return err;
  225. if (slice->data) {
  226. if (slice->data_size * 8 + 8 > put_bits_left(pbc))
  227. return AVERROR(ENOSPC);
  228. init_get_bits(&gbc, slice->data, slice->data_size * 8);
  229. skip_bits_long(&gbc, slice->data_bit_start);
  230. while (get_bits_left(&gbc) > 15)
  231. put_bits(pbc, 16, get_bits(&gbc, 16));
  232. bits_left = get_bits_left(&gbc);
  233. put_bits(pbc, bits_left, get_bits(&gbc, bits_left));
  234. // Align with zeroes.
  235. while (put_bits_count(pbc) % 8 != 0)
  236. put_bits(pbc, 1, 0);
  237. }
  238. return 0;
  239. }
  240. static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
  241. CodedBitstreamUnit *unit)
  242. {
  243. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  244. PutBitContext pbc;
  245. int err;
  246. if (!priv->write_buffer) {
  247. // Initial write buffer size is 1MB.
  248. priv->write_buffer_size = 1024 * 1024;
  249. reallocate_and_try_again:
  250. err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
  251. if (err < 0) {
  252. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
  253. "sufficiently large write buffer (last attempt "
  254. "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
  255. return err;
  256. }
  257. }
  258. init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
  259. if (unit->type >= 0x01 && unit->type <= 0xaf)
  260. err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
  261. else
  262. err = cbs_mpeg2_write_header(ctx, unit, &pbc);
  263. if (err == AVERROR(ENOSPC)) {
  264. // Overflow.
  265. priv->write_buffer_size *= 2;
  266. goto reallocate_and_try_again;
  267. }
  268. if (err < 0) {
  269. // Write failed for some other reason.
  270. return err;
  271. }
  272. if (put_bits_count(&pbc) % 8)
  273. unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
  274. else
  275. unit->data_bit_padding = 0;
  276. unit->data_size = (put_bits_count(&pbc) + 7) / 8;
  277. flush_put_bits(&pbc);
  278. err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
  279. if (err < 0)
  280. return err;
  281. memcpy(unit->data, priv->write_buffer, unit->data_size);
  282. return 0;
  283. }
  284. static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
  285. CodedBitstreamFragment *frag)
  286. {
  287. uint8_t *data;
  288. size_t size, dp;
  289. int i;
  290. size = 0;
  291. for (i = 0; i < frag->nb_units; i++)
  292. size += 3 + frag->units[i].data_size;
  293. frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  294. if (!frag->data_ref)
  295. return AVERROR(ENOMEM);
  296. data = frag->data_ref->data;
  297. dp = 0;
  298. for (i = 0; i < frag->nb_units; i++) {
  299. CodedBitstreamUnit *unit = &frag->units[i];
  300. data[dp++] = 0;
  301. data[dp++] = 0;
  302. data[dp++] = 1;
  303. memcpy(data + dp, unit->data, unit->data_size);
  304. dp += unit->data_size;
  305. }
  306. av_assert0(dp == size);
  307. memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  308. frag->data = data;
  309. frag->data_size = size;
  310. return 0;
  311. }
  312. static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
  313. {
  314. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  315. av_freep(&priv->write_buffer);
  316. }
  317. const CodedBitstreamType ff_cbs_type_mpeg2 = {
  318. .codec_id = AV_CODEC_ID_MPEG2VIDEO,
  319. .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
  320. .split_fragment = &cbs_mpeg2_split_fragment,
  321. .read_unit = &cbs_mpeg2_read_unit,
  322. .write_unit = &cbs_mpeg2_write_unit,
  323. .assemble_fragment = &cbs_mpeg2_assemble_fragment,
  324. .close = &cbs_mpeg2_close,
  325. };