cbs_mpeg2.c 14 KB

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