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.

406 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 READ
  35. #define READWRITE read
  36. #define RWContext GetBitContext
  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. (get_bits_left(rw) >= width && \
  51. (var = show_bits(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 = (uint8_t *)start - 1;
  120. err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
  121. unit_data, unit_size, frag->data_ref);
  122. if (err < 0)
  123. return err;
  124. if (end == frag->data + frag->data_size)
  125. break;
  126. start_code = next_start_code;
  127. start = end;
  128. }
  129. return 0;
  130. }
  131. static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
  132. CodedBitstreamUnit *unit)
  133. {
  134. GetBitContext gbc;
  135. int err;
  136. err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
  137. if (err < 0)
  138. return err;
  139. if (MPEG2_START_IS_SLICE(unit->type)) {
  140. MPEG2RawSlice *slice;
  141. int pos, len;
  142. err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
  143. &cbs_mpeg2_free_slice);
  144. if (err < 0)
  145. return err;
  146. slice = unit->content;
  147. err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
  148. if (err < 0)
  149. return err;
  150. pos = get_bits_count(&gbc);
  151. len = unit->data_size;
  152. slice->data_size = len - pos / 8;
  153. slice->data_ref = av_buffer_ref(unit->data_ref);
  154. if (!slice->data_ref)
  155. return AVERROR(ENOMEM);
  156. slice->data = unit->data + pos / 8;
  157. slice->data_bit_start = pos % 8;
  158. } else {
  159. switch (unit->type) {
  160. #define START(start_code, type, read_func, free_func) \
  161. case start_code: \
  162. { \
  163. type *header; \
  164. err = ff_cbs_alloc_unit_content(ctx, unit, \
  165. sizeof(*header), free_func); \
  166. if (err < 0) \
  167. return err; \
  168. header = unit->content; \
  169. err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
  170. if (err < 0) \
  171. return err; \
  172. } \
  173. break;
  174. START(0x00, MPEG2RawPictureHeader, picture_header, NULL);
  175. START(0xb2, MPEG2RawUserData, user_data,
  176. &cbs_mpeg2_free_user_data);
  177. START(0xb3, MPEG2RawSequenceHeader, sequence_header, NULL);
  178. START(0xb5, MPEG2RawExtensionData, extension_data, NULL);
  179. START(0xb8, MPEG2RawGroupOfPicturesHeader,
  180. group_of_pictures_header, NULL);
  181. #undef START
  182. default:
  183. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unknown start code %02"PRIx32".\n",
  184. unit->type);
  185. return AVERROR_INVALIDDATA;
  186. }
  187. }
  188. return 0;
  189. }
  190. static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
  191. CodedBitstreamUnit *unit,
  192. PutBitContext *pbc)
  193. {
  194. int err;
  195. switch (unit->type) {
  196. #define START(start_code, type, func) \
  197. case start_code: \
  198. err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
  199. break;
  200. START(0x00, MPEG2RawPictureHeader, picture_header);
  201. START(0xb2, MPEG2RawUserData, user_data);
  202. START(0xb3, MPEG2RawSequenceHeader, sequence_header);
  203. START(0xb5, MPEG2RawExtensionData, extension_data);
  204. START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
  205. #undef START
  206. default:
  207. av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
  208. "code %02"PRIx32".\n", unit->type);
  209. return AVERROR_PATCHWELCOME;
  210. }
  211. return err;
  212. }
  213. static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
  214. CodedBitstreamUnit *unit,
  215. PutBitContext *pbc)
  216. {
  217. MPEG2RawSlice *slice = unit->content;
  218. GetBitContext gbc;
  219. size_t bits_left;
  220. int err;
  221. err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
  222. if (err < 0)
  223. return err;
  224. if (slice->data) {
  225. if (slice->data_size * 8 + 8 > put_bits_left(pbc))
  226. return AVERROR(ENOSPC);
  227. init_get_bits(&gbc, slice->data, slice->data_size * 8);
  228. skip_bits_long(&gbc, slice->data_bit_start);
  229. while (get_bits_left(&gbc) > 15)
  230. put_bits(pbc, 16, get_bits(&gbc, 16));
  231. bits_left = get_bits_left(&gbc);
  232. put_bits(pbc, bits_left, get_bits(&gbc, bits_left));
  233. // Align with zeroes.
  234. while (put_bits_count(pbc) % 8 != 0)
  235. put_bits(pbc, 1, 0);
  236. }
  237. return 0;
  238. }
  239. static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
  240. CodedBitstreamUnit *unit)
  241. {
  242. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  243. PutBitContext pbc;
  244. int err;
  245. if (!priv->write_buffer) {
  246. // Initial write buffer size is 1MB.
  247. priv->write_buffer_size = 1024 * 1024;
  248. reallocate_and_try_again:
  249. err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
  250. if (err < 0) {
  251. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
  252. "sufficiently large write buffer (last attempt "
  253. "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
  254. return err;
  255. }
  256. }
  257. init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
  258. if (unit->type >= 0x01 && unit->type <= 0xaf)
  259. err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
  260. else
  261. err = cbs_mpeg2_write_header(ctx, unit, &pbc);
  262. if (err == AVERROR(ENOSPC)) {
  263. // Overflow.
  264. priv->write_buffer_size *= 2;
  265. goto reallocate_and_try_again;
  266. }
  267. if (err < 0) {
  268. // Write failed for some other reason.
  269. return err;
  270. }
  271. if (put_bits_count(&pbc) % 8)
  272. unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
  273. else
  274. unit->data_bit_padding = 0;
  275. unit->data_size = (put_bits_count(&pbc) + 7) / 8;
  276. flush_put_bits(&pbc);
  277. err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
  278. if (err < 0)
  279. return err;
  280. memcpy(unit->data, priv->write_buffer, unit->data_size);
  281. return 0;
  282. }
  283. static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
  284. CodedBitstreamFragment *frag)
  285. {
  286. uint8_t *data;
  287. size_t size, dp;
  288. int i;
  289. size = 0;
  290. for (i = 0; i < frag->nb_units; i++)
  291. size += 3 + frag->units[i].data_size;
  292. frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  293. if (!frag->data_ref)
  294. return AVERROR(ENOMEM);
  295. data = frag->data_ref->data;
  296. dp = 0;
  297. for (i = 0; i < frag->nb_units; i++) {
  298. CodedBitstreamUnit *unit = &frag->units[i];
  299. data[dp++] = 0;
  300. data[dp++] = 0;
  301. data[dp++] = 1;
  302. memcpy(data + dp, unit->data, unit->data_size);
  303. dp += unit->data_size;
  304. }
  305. av_assert0(dp == size);
  306. memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  307. frag->data = data;
  308. frag->data_size = size;
  309. return 0;
  310. }
  311. static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
  312. {
  313. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  314. av_freep(&priv->write_buffer);
  315. }
  316. const CodedBitstreamType ff_cbs_type_mpeg2 = {
  317. .codec_id = AV_CODEC_ID_MPEG2VIDEO,
  318. .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
  319. .split_fragment = &cbs_mpeg2_split_fragment,
  320. .read_unit = &cbs_mpeg2_read_unit,
  321. .write_unit = &cbs_mpeg2_write_unit,
  322. .assemble_fragment = &cbs_mpeg2_assemble_fragment,
  323. .close = &cbs_mpeg2_close,
  324. };