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.

415 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 int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
  82. CodedBitstreamFragment *frag,
  83. int header)
  84. {
  85. const uint8_t *start, *end;
  86. uint8_t *unit_data;
  87. uint32_t start_code = -1, next_start_code = -1;
  88. size_t unit_size;
  89. int err, i, unit_type;
  90. start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
  91. &start_code);
  92. for (i = 0;; i++) {
  93. end = avpriv_find_start_code(start, frag->data + frag->data_size,
  94. &next_start_code);
  95. unit_type = start_code & 0xff;
  96. // The start and end pointers point at to the byte following the
  97. // start_code_identifier in the start code that they found.
  98. if (end == frag->data + frag->data_size) {
  99. // We didn't find a start code, so this is the final unit.
  100. unit_size = end - (start - 1);
  101. } else {
  102. // Unit runs from start to the beginning of the start code
  103. // pointed to by end (including any padding zeroes).
  104. unit_size = (end - 4) - (start - 1);
  105. }
  106. unit_data = av_malloc(unit_size + AV_INPUT_BUFFER_PADDING_SIZE);
  107. if (!unit_data)
  108. return AVERROR(ENOMEM);
  109. memcpy(unit_data, start - 1, unit_size);
  110. memset(unit_data + unit_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  111. err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
  112. unit_data, unit_size);
  113. if (err < 0) {
  114. av_freep(&unit_data);
  115. return err;
  116. }
  117. if (end == frag->data + frag->data_size)
  118. break;
  119. start_code = next_start_code;
  120. start = end;
  121. }
  122. return 0;
  123. }
  124. static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
  125. CodedBitstreamUnit *unit)
  126. {
  127. GetBitContext gbc;
  128. int err;
  129. err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
  130. if (err < 0)
  131. return err;
  132. if (MPEG2_START_IS_SLICE(unit->type)) {
  133. MPEG2RawSlice *slice;
  134. int pos, len;
  135. slice = av_mallocz(sizeof(*slice));
  136. if (!slice)
  137. return AVERROR(ENOMEM);
  138. err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
  139. if (err < 0) {
  140. av_free(slice);
  141. return err;
  142. }
  143. pos = get_bits_count(&gbc);
  144. len = unit->data_size;
  145. slice->data_size = len - pos / 8;
  146. slice->data = av_malloc(slice->data_size +
  147. AV_INPUT_BUFFER_PADDING_SIZE);
  148. if (!slice->data) {
  149. av_free(slice);
  150. return AVERROR(ENOMEM);
  151. }
  152. memcpy(slice->data,
  153. unit->data + pos / 8, slice->data_size);
  154. memset(slice->data + slice->data_size, 0,
  155. AV_INPUT_BUFFER_PADDING_SIZE);
  156. slice->data_bit_start = pos % 8;
  157. unit->content = slice;
  158. } else {
  159. switch (unit->type) {
  160. #define START(start_code, type, func) \
  161. case start_code: \
  162. { \
  163. type *header; \
  164. header = av_mallocz(sizeof(*header)); \
  165. if (!header) \
  166. return AVERROR(ENOMEM); \
  167. err = cbs_mpeg2_read_ ## func(ctx, &gbc, header); \
  168. if (err < 0) { \
  169. av_free(header); \
  170. return err; \
  171. } \
  172. unit->content = header; \
  173. } \
  174. break;
  175. START(0x00, MPEG2RawPictureHeader, picture_header);
  176. START(0xb2, MPEG2RawUserData, user_data);
  177. START(0xb3, MPEG2RawSequenceHeader, sequence_header);
  178. START(0xb5, MPEG2RawExtensionData, extension_data);
  179. START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
  180. #undef START
  181. default:
  182. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unknown start code %02"PRIx32".\n",
  183. unit->type);
  184. return AVERROR_INVALIDDATA;
  185. }
  186. }
  187. return 0;
  188. }
  189. static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
  190. CodedBitstreamUnit *unit,
  191. PutBitContext *pbc)
  192. {
  193. int err;
  194. switch (unit->type) {
  195. #define START(start_code, type, func) \
  196. case start_code: \
  197. err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
  198. break;
  199. START(0x00, MPEG2RawPictureHeader, picture_header);
  200. START(0xb2, MPEG2RawUserData, user_data);
  201. START(0xb3, MPEG2RawSequenceHeader, sequence_header);
  202. START(0xb5, MPEG2RawExtensionData, extension_data);
  203. START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
  204. #undef START
  205. default:
  206. av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
  207. "code %02"PRIx32".\n", unit->type);
  208. return AVERROR_PATCHWELCOME;
  209. }
  210. return err;
  211. }
  212. static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
  213. CodedBitstreamUnit *unit,
  214. PutBitContext *pbc)
  215. {
  216. MPEG2RawSlice *slice = unit->content;
  217. GetBitContext gbc;
  218. size_t bits_left;
  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. if (slice->data_size * 8 + 8 > put_bits_left(pbc))
  225. return AVERROR(ENOSPC);
  226. init_get_bits(&gbc, slice->data, slice->data_size * 8);
  227. skip_bits_long(&gbc, slice->data_bit_start);
  228. while (get_bits_left(&gbc) > 15)
  229. put_bits(pbc, 16, get_bits(&gbc, 16));
  230. bits_left = get_bits_left(&gbc);
  231. put_bits(pbc, bits_left, get_bits(&gbc, bits_left));
  232. // Align with zeroes.
  233. while (put_bits_count(pbc) % 8 != 0)
  234. put_bits(pbc, 1, 0);
  235. }
  236. return 0;
  237. }
  238. static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
  239. CodedBitstreamUnit *unit)
  240. {
  241. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  242. PutBitContext pbc;
  243. int err;
  244. if (!priv->write_buffer) {
  245. // Initial write buffer size is 1MB.
  246. priv->write_buffer_size = 1024 * 1024;
  247. reallocate_and_try_again:
  248. err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
  249. if (err < 0) {
  250. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
  251. "sufficiently large write buffer (last attempt "
  252. "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
  253. return err;
  254. }
  255. }
  256. init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
  257. if (unit->type >= 0x01 && unit->type <= 0xaf)
  258. err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
  259. else
  260. err = cbs_mpeg2_write_header(ctx, unit, &pbc);
  261. if (err == AVERROR(ENOSPC)) {
  262. // Overflow.
  263. priv->write_buffer_size *= 2;
  264. goto reallocate_and_try_again;
  265. }
  266. if (err < 0) {
  267. // Write failed for some other reason.
  268. return err;
  269. }
  270. if (put_bits_count(&pbc) % 8)
  271. unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
  272. else
  273. unit->data_bit_padding = 0;
  274. unit->data_size = (put_bits_count(&pbc) + 7) / 8;
  275. flush_put_bits(&pbc);
  276. err = av_reallocp(&unit->data, unit->data_size);
  277. if (err < 0)
  278. return err;
  279. memcpy(unit->data, priv->write_buffer, unit->data_size);
  280. return 0;
  281. }
  282. static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
  283. CodedBitstreamFragment *frag)
  284. {
  285. uint8_t *data;
  286. size_t size, dp, sp;
  287. int i;
  288. size = 0;
  289. for (i = 0; i < frag->nb_units; i++)
  290. size += 3 + frag->units[i].data_size;
  291. data = av_malloc(size);
  292. if (!data)
  293. return AVERROR(ENOMEM);
  294. dp = 0;
  295. for (i = 0; i < frag->nb_units; i++) {
  296. CodedBitstreamUnit *unit = &frag->units[i];
  297. data[dp++] = 0;
  298. data[dp++] = 0;
  299. data[dp++] = 1;
  300. for (sp = 0; sp < unit->data_size; sp++)
  301. data[dp++] = unit->data[sp];
  302. }
  303. av_assert0(dp == size);
  304. frag->data = data;
  305. frag->data_size = size;
  306. return 0;
  307. }
  308. static void cbs_mpeg2_free_unit(CodedBitstreamUnit *unit)
  309. {
  310. if (MPEG2_START_IS_SLICE(unit->type)) {
  311. MPEG2RawSlice *slice = unit->content;
  312. av_freep(&slice->data);
  313. av_freep(&slice->header.extra_information);
  314. } else if (unit->type == MPEG2_START_USER_DATA) {
  315. MPEG2RawUserData *user = unit->content;
  316. av_freep(&user->user_data);
  317. }
  318. av_freep(&unit->content);
  319. }
  320. static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
  321. {
  322. CodedBitstreamMPEG2Context *priv = ctx->priv_data;
  323. av_freep(&priv->write_buffer);
  324. }
  325. const CodedBitstreamType ff_cbs_type_mpeg2 = {
  326. .codec_id = AV_CODEC_ID_MPEG2VIDEO,
  327. .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
  328. .split_fragment = &cbs_mpeg2_split_fragment,
  329. .read_unit = &cbs_mpeg2_read_unit,
  330. .write_unit = &cbs_mpeg2_write_unit,
  331. .assemble_fragment = &cbs_mpeg2_assemble_fragment,
  332. .free_unit = &cbs_mpeg2_free_unit,
  333. .close = &cbs_mpeg2_close,
  334. };