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.

433 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. if (!get_bits_left(&gbc))
  204. return AVERROR_INVALIDDATA;
  205. pos = get_bits_count(&gbc);
  206. len = unit->data_size;
  207. slice->data_size = len - pos / 8;
  208. slice->data_ref = av_buffer_ref(unit->data_ref);
  209. if (!slice->data_ref)
  210. return AVERROR(ENOMEM);
  211. slice->data = unit->data + pos / 8;
  212. slice->data_bit_start = pos % 8;
  213. } else {
  214. switch (unit->type) {
  215. #define START(start_code, type, read_func, free_func) \
  216. case start_code: \
  217. { \
  218. type *header; \
  219. err = ff_cbs_alloc_unit_content(ctx, unit, \
  220. sizeof(*header), free_func); \
  221. if (err < 0) \
  222. return err; \
  223. header = unit->content; \
  224. err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
  225. if (err < 0) \
  226. return err; \
  227. } \
  228. break;
  229. START(MPEG2_START_PICTURE, MPEG2RawPictureHeader,
  230. picture_header, &cbs_mpeg2_free_picture_header);
  231. START(MPEG2_START_USER_DATA, MPEG2RawUserData,
  232. user_data, &cbs_mpeg2_free_user_data);
  233. START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader,
  234. sequence_header, NULL);
  235. START(MPEG2_START_EXTENSION, MPEG2RawExtensionData,
  236. extension_data, NULL);
  237. START(MPEG2_START_GROUP, MPEG2RawGroupOfPicturesHeader,
  238. group_of_pictures_header, NULL);
  239. START(MPEG2_START_SEQUENCE_END, MPEG2RawSequenceEnd,
  240. sequence_end, NULL);
  241. #undef START
  242. default:
  243. return AVERROR(ENOSYS);
  244. }
  245. }
  246. return 0;
  247. }
  248. static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
  249. CodedBitstreamUnit *unit,
  250. PutBitContext *pbc)
  251. {
  252. int err;
  253. switch (unit->type) {
  254. #define START(start_code, type, func) \
  255. case start_code: \
  256. err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
  257. break;
  258. START(MPEG2_START_PICTURE, MPEG2RawPictureHeader, picture_header);
  259. START(MPEG2_START_USER_DATA, MPEG2RawUserData, user_data);
  260. START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader, sequence_header);
  261. START(MPEG2_START_EXTENSION, MPEG2RawExtensionData, extension_data);
  262. START(MPEG2_START_GROUP, MPEG2RawGroupOfPicturesHeader,
  263. group_of_pictures_header);
  264. START(MPEG2_START_SEQUENCE_END, MPEG2RawSequenceEnd, sequence_end);
  265. #undef START
  266. default:
  267. av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
  268. "code %02"PRIx32".\n", unit->type);
  269. return AVERROR_PATCHWELCOME;
  270. }
  271. return err;
  272. }
  273. static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
  274. CodedBitstreamUnit *unit,
  275. PutBitContext *pbc)
  276. {
  277. MPEG2RawSlice *slice = unit->content;
  278. int err;
  279. err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
  280. if (err < 0)
  281. return err;
  282. if (slice->data) {
  283. size_t rest = slice->data_size - (slice->data_bit_start + 7) / 8;
  284. uint8_t *pos = slice->data + slice->data_bit_start / 8;
  285. av_assert0(slice->data_bit_start >= 0 &&
  286. slice->data_size > slice->data_bit_start / 8);
  287. if (slice->data_size * 8 + 8 > put_bits_left(pbc))
  288. return AVERROR(ENOSPC);
  289. // First copy the remaining bits of the first byte
  290. if (slice->data_bit_start % 8)
  291. put_bits(pbc, 8 - slice->data_bit_start % 8,
  292. *pos++ & MAX_UINT_BITS(8 - slice->data_bit_start % 8));
  293. if (put_bits_count(pbc) % 8 == 0) {
  294. // If the writer is aligned at this point,
  295. // memcpy can be used to improve performance.
  296. // This is the normal case.
  297. flush_put_bits(pbc);
  298. memcpy(put_bits_ptr(pbc), pos, rest);
  299. skip_put_bytes(pbc, rest);
  300. } else {
  301. // If not, we have to copy manually:
  302. for (; rest > 3; rest -= 4, pos += 4)
  303. put_bits32(pbc, AV_RB32(pos));
  304. for (; rest; rest--, pos++)
  305. put_bits(pbc, 8, *pos);
  306. // Align with zeros
  307. put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
  308. }
  309. }
  310. return 0;
  311. }
  312. static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
  313. CodedBitstreamUnit *unit,
  314. PutBitContext *pbc)
  315. {
  316. if (MPEG2_START_IS_SLICE(unit->type))
  317. return cbs_mpeg2_write_slice (ctx, unit, pbc);
  318. else
  319. return cbs_mpeg2_write_header(ctx, unit, pbc);
  320. }
  321. static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
  322. CodedBitstreamFragment *frag)
  323. {
  324. uint8_t *data;
  325. size_t size, dp;
  326. int i;
  327. size = 0;
  328. for (i = 0; i < frag->nb_units; i++)
  329. size += 3 + frag->units[i].data_size;
  330. frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  331. if (!frag->data_ref)
  332. return AVERROR(ENOMEM);
  333. data = frag->data_ref->data;
  334. dp = 0;
  335. for (i = 0; i < frag->nb_units; i++) {
  336. CodedBitstreamUnit *unit = &frag->units[i];
  337. data[dp++] = 0;
  338. data[dp++] = 0;
  339. data[dp++] = 1;
  340. memcpy(data + dp, unit->data, unit->data_size);
  341. dp += unit->data_size;
  342. }
  343. av_assert0(dp == size);
  344. memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  345. frag->data = data;
  346. frag->data_size = size;
  347. return 0;
  348. }
  349. const CodedBitstreamType ff_cbs_type_mpeg2 = {
  350. .codec_id = AV_CODEC_ID_MPEG2VIDEO,
  351. .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
  352. .split_fragment = &cbs_mpeg2_split_fragment,
  353. .read_unit = &cbs_mpeg2_read_unit,
  354. .write_unit = &cbs_mpeg2_write_unit,
  355. .assemble_fragment = &cbs_mpeg2_assemble_fragment,
  356. };