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.

430 lines
14KB

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