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.

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