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.

521 lines
15KB

  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 "cbs.h"
  19. #include "cbs_internal.h"
  20. #include "cbs_jpeg.h"
  21. #define HEADER(name) do { \
  22. ff_cbs_trace_header(ctx, name); \
  23. } while (0)
  24. #define CHECK(call) do { \
  25. err = (call); \
  26. if (err < 0) \
  27. return err; \
  28. } while (0)
  29. #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
  30. #define u(width, name, range_min, range_max) \
  31. xu(width, name, range_min, range_max, 0)
  32. #define us(width, name, sub, range_min, range_max) \
  33. xu(width, name, range_min, range_max, 1, sub)
  34. #define READ
  35. #define READWRITE read
  36. #define RWContext GetBitContext
  37. #define FUNC(name) cbs_jpeg_read_ ## name
  38. #define xu(width, name, range_min, range_max, subs, ...) do { \
  39. uint32_t value; \
  40. CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
  41. SUBSCRIPTS(subs, __VA_ARGS__), \
  42. &value, range_min, range_max)); \
  43. current->name = value; \
  44. } while (0)
  45. #include "cbs_jpeg_syntax_template.c"
  46. #undef READ
  47. #undef READWRITE
  48. #undef RWContext
  49. #undef FUNC
  50. #undef xu
  51. #define WRITE
  52. #define READWRITE write
  53. #define RWContext PutBitContext
  54. #define FUNC(name) cbs_jpeg_write_ ## name
  55. #define xu(width, name, range_min, range_max, subs, ...) do { \
  56. uint32_t value = current->name; \
  57. CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
  58. SUBSCRIPTS(subs, __VA_ARGS__), \
  59. value, range_min, range_max)); \
  60. } while (0)
  61. #include "cbs_jpeg_syntax_template.c"
  62. #undef WRITE
  63. #undef READWRITE
  64. #undef RWContext
  65. #undef FUNC
  66. #undef xu
  67. static void cbs_jpeg_free_application_data(void *unit, uint8_t *content)
  68. {
  69. JPEGRawApplicationData *ad = (JPEGRawApplicationData*)content;
  70. av_buffer_unref(&ad->Ap_ref);
  71. av_freep(&content);
  72. }
  73. static void cbs_jpeg_free_comment(void *unit, uint8_t *content)
  74. {
  75. JPEGRawComment *comment = (JPEGRawComment*)content;
  76. av_buffer_unref(&comment->Cm_ref);
  77. av_freep(&content);
  78. }
  79. static void cbs_jpeg_free_scan(void *unit, uint8_t *content)
  80. {
  81. JPEGRawScan *scan = (JPEGRawScan*)content;
  82. av_buffer_unref(&scan->data_ref);
  83. av_freep(&content);
  84. }
  85. static int cbs_jpeg_split_fragment(CodedBitstreamContext *ctx,
  86. CodedBitstreamFragment *frag,
  87. int header)
  88. {
  89. AVBufferRef *data_ref;
  90. uint8_t *data;
  91. size_t data_size;
  92. int unit, start, end, marker, next_start, next_marker;
  93. int err, i, j, length;
  94. if (frag->data_size < 4) {
  95. // Definitely too short to be meaningful.
  96. return AVERROR_INVALIDDATA;
  97. }
  98. for (i = 0; i + 1 < frag->data_size && frag->data[i] != 0xff; i++);
  99. if (i > 0) {
  100. av_log(ctx->log_ctx, AV_LOG_WARNING, "Discarding %d bytes at "
  101. "beginning of image.\n", i);
  102. }
  103. for (++i; i + 1 < frag->data_size && frag->data[i] == 0xff; i++);
  104. if (i + 1 >= frag->data_size && frag->data[i]) {
  105. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: "
  106. "no SOI marker found.\n");
  107. return AVERROR_INVALIDDATA;
  108. }
  109. marker = frag->data[i];
  110. if (marker != JPEG_MARKER_SOI) {
  111. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: first "
  112. "marker is %02x, should be SOI.\n", marker);
  113. return AVERROR_INVALIDDATA;
  114. }
  115. for (++i; i + 1 < frag->data_size && frag->data[i] == 0xff; i++);
  116. if (i + 1 >= frag->data_size) {
  117. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: "
  118. "no image content found.\n");
  119. return AVERROR_INVALIDDATA;
  120. }
  121. marker = frag->data[i];
  122. start = i + 1;
  123. for (unit = 0;; unit++) {
  124. if (marker == JPEG_MARKER_EOI) {
  125. break;
  126. } else if (marker == JPEG_MARKER_SOS) {
  127. for (i = start; i + 1 < frag->data_size; i++) {
  128. if (frag->data[i] != 0xff)
  129. continue;
  130. end = i;
  131. for (++i; i + 1 < frag->data_size &&
  132. frag->data[i] == 0xff; i++);
  133. if (i + 1 >= frag->data_size) {
  134. next_marker = -1;
  135. } else {
  136. if (frag->data[i] == 0x00)
  137. continue;
  138. next_marker = frag->data[i];
  139. next_start = i + 1;
  140. }
  141. break;
  142. }
  143. } else {
  144. i = start;
  145. if (i + 2 > frag->data_size) {
  146. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: "
  147. "truncated at %02x marker.\n", marker);
  148. return AVERROR_INVALIDDATA;
  149. }
  150. length = AV_RB16(frag->data + i);
  151. if (i + length > frag->data_size) {
  152. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: "
  153. "truncated at %02x marker segment.\n", marker);
  154. return AVERROR_INVALIDDATA;
  155. }
  156. end = start + length;
  157. i = end;
  158. if (frag->data[i] != 0xff) {
  159. next_marker = -1;
  160. } else {
  161. for (++i; i + 1 < frag->data_size &&
  162. frag->data[i] == 0xff; i++);
  163. if (i + 1 >= frag->data_size) {
  164. next_marker = -1;
  165. } else {
  166. next_marker = frag->data[i];
  167. next_start = i + 1;
  168. }
  169. }
  170. }
  171. if (marker == JPEG_MARKER_SOS) {
  172. length = AV_RB16(frag->data + start);
  173. data_ref = NULL;
  174. data = av_malloc(end - start +
  175. AV_INPUT_BUFFER_PADDING_SIZE);
  176. if (!data)
  177. return AVERROR(ENOMEM);
  178. memcpy(data, frag->data + start, length);
  179. for (i = start + length, j = length; i < end; i++, j++) {
  180. if (frag->data[i] == 0xff) {
  181. while (frag->data[i] == 0xff)
  182. ++i;
  183. data[j] = 0xff;
  184. } else {
  185. data[j] = frag->data[i];
  186. }
  187. }
  188. data_size = j;
  189. memset(data + data_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  190. } else {
  191. data = frag->data + start;
  192. data_size = end - start;
  193. data_ref = frag->data_ref;
  194. }
  195. err = ff_cbs_insert_unit_data(ctx, frag, unit, marker,
  196. data, data_size, data_ref);
  197. if (err < 0) {
  198. if (!data_ref)
  199. av_freep(&data);
  200. return err;
  201. }
  202. if (next_marker == -1)
  203. break;
  204. marker = next_marker;
  205. start = next_start;
  206. }
  207. return 0;
  208. }
  209. static int cbs_jpeg_read_unit(CodedBitstreamContext *ctx,
  210. CodedBitstreamUnit *unit)
  211. {
  212. GetBitContext gbc;
  213. int err;
  214. err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
  215. if (err < 0)
  216. return err;
  217. if (unit->type >= JPEG_MARKER_SOF0 &&
  218. unit->type <= JPEG_MARKER_SOF3) {
  219. err = ff_cbs_alloc_unit_content(ctx, unit,
  220. sizeof(JPEGRawFrameHeader),
  221. NULL);
  222. if (err < 0)
  223. return err;
  224. err = cbs_jpeg_read_frame_header(ctx, &gbc, unit->content);
  225. if (err < 0)
  226. return err;
  227. } else if (unit->type >= JPEG_MARKER_APPN &&
  228. unit->type <= JPEG_MARKER_APPN + 15) {
  229. err = ff_cbs_alloc_unit_content(ctx, unit,
  230. sizeof(JPEGRawApplicationData),
  231. &cbs_jpeg_free_application_data);
  232. if (err < 0)
  233. return err;
  234. err = cbs_jpeg_read_application_data(ctx, &gbc, unit->content);
  235. if (err < 0)
  236. return err;
  237. } else if (unit->type == JPEG_MARKER_SOS) {
  238. JPEGRawScan *scan;
  239. int pos;
  240. err = ff_cbs_alloc_unit_content(ctx, unit,
  241. sizeof(JPEGRawScan),
  242. &cbs_jpeg_free_scan);
  243. if (err < 0)
  244. return err;
  245. scan = unit->content;
  246. err = cbs_jpeg_read_scan_header(ctx, &gbc, &scan->header);
  247. if (err < 0)
  248. return err;
  249. pos = get_bits_count(&gbc);
  250. av_assert0(pos % 8 == 0);
  251. if (pos > 0) {
  252. scan->data_size = unit->data_size - pos / 8;
  253. scan->data_ref = av_buffer_ref(unit->data_ref);
  254. if (!scan->data_ref)
  255. return AVERROR(ENOMEM);
  256. scan->data = unit->data + pos / 8;
  257. }
  258. } else {
  259. switch (unit->type) {
  260. #define SEGMENT(marker, type, func, free) \
  261. case JPEG_MARKER_ ## marker: \
  262. { \
  263. err = ff_cbs_alloc_unit_content(ctx, unit, \
  264. sizeof(type), free); \
  265. if (err < 0) \
  266. return err; \
  267. err = cbs_jpeg_read_ ## func(ctx, &gbc, unit->content); \
  268. if (err < 0) \
  269. return err; \
  270. } \
  271. break
  272. SEGMENT(DQT, JPEGRawQuantisationTableSpecification, dqt, NULL);
  273. SEGMENT(DHT, JPEGRawHuffmanTableSpecification, dht, NULL);
  274. SEGMENT(COM, JPEGRawComment, comment, &cbs_jpeg_free_comment);
  275. #undef SEGMENT
  276. default:
  277. return AVERROR(ENOSYS);
  278. }
  279. }
  280. return 0;
  281. }
  282. static int cbs_jpeg_write_scan(CodedBitstreamContext *ctx,
  283. CodedBitstreamUnit *unit,
  284. PutBitContext *pbc)
  285. {
  286. JPEGRawScan *scan = unit->content;
  287. int i, err;
  288. err = cbs_jpeg_write_scan_header(ctx, pbc, &scan->header);
  289. if (err < 0)
  290. return err;
  291. if (scan->data) {
  292. if (scan->data_size * 8 > put_bits_left(pbc))
  293. return AVERROR(ENOSPC);
  294. for (i = 0; i < scan->data_size; i++)
  295. put_bits(pbc, 8, scan->data[i]);
  296. }
  297. return 0;
  298. }
  299. static int cbs_jpeg_write_segment(CodedBitstreamContext *ctx,
  300. CodedBitstreamUnit *unit,
  301. PutBitContext *pbc)
  302. {
  303. int err;
  304. if (unit->type >= JPEG_MARKER_SOF0 &&
  305. unit->type <= JPEG_MARKER_SOF3) {
  306. err = cbs_jpeg_write_frame_header(ctx, pbc, unit->content);
  307. } else if (unit->type >= JPEG_MARKER_APPN &&
  308. unit->type <= JPEG_MARKER_APPN + 15) {
  309. err = cbs_jpeg_write_application_data(ctx, pbc, unit->content);
  310. } else {
  311. switch (unit->type) {
  312. #define SEGMENT(marker, func) \
  313. case JPEG_MARKER_ ## marker: \
  314. err = cbs_jpeg_write_ ## func(ctx, pbc, unit->content); \
  315. break;
  316. SEGMENT(DQT, dqt);
  317. SEGMENT(DHT, dht);
  318. SEGMENT(COM, comment);
  319. default:
  320. return AVERROR_PATCHWELCOME;
  321. }
  322. }
  323. return err;
  324. }
  325. static int cbs_jpeg_write_unit(CodedBitstreamContext *ctx,
  326. CodedBitstreamUnit *unit)
  327. {
  328. CodedBitstreamJPEGContext *priv = ctx->priv_data;
  329. PutBitContext pbc;
  330. int err;
  331. if (!priv->write_buffer) {
  332. // Initial write buffer size is 1MB.
  333. priv->write_buffer_size = 1024 * 1024;
  334. reallocate_and_try_again:
  335. err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
  336. if (err < 0) {
  337. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
  338. "sufficiently large write buffer (last attempt "
  339. "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
  340. return err;
  341. }
  342. }
  343. init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
  344. if (unit->type == JPEG_MARKER_SOS)
  345. err = cbs_jpeg_write_scan(ctx, unit, &pbc);
  346. else
  347. err = cbs_jpeg_write_segment(ctx, unit, &pbc);
  348. if (err == AVERROR(ENOSPC)) {
  349. // Overflow.
  350. priv->write_buffer_size *= 2;
  351. goto reallocate_and_try_again;
  352. }
  353. if (err < 0) {
  354. // Write failed for some other reason.
  355. return err;
  356. }
  357. if (put_bits_count(&pbc) % 8)
  358. unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
  359. else
  360. unit->data_bit_padding = 0;
  361. unit->data_size = (put_bits_count(&pbc) + 7) / 8;
  362. flush_put_bits(&pbc);
  363. err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
  364. if (err < 0)
  365. return err;
  366. memcpy(unit->data, priv->write_buffer, unit->data_size);
  367. return 0;
  368. }
  369. static int cbs_jpeg_assemble_fragment(CodedBitstreamContext *ctx,
  370. CodedBitstreamFragment *frag)
  371. {
  372. const CodedBitstreamUnit *unit;
  373. uint8_t *data;
  374. size_t size, dp, sp;
  375. int i;
  376. size = 4; // SOI + EOI.
  377. for (i = 0; i < frag->nb_units; i++) {
  378. unit = &frag->units[i];
  379. size += 2 + unit->data_size;
  380. if (unit->type == JPEG_MARKER_SOS) {
  381. for (sp = 0; sp < unit->data_size; sp++) {
  382. if (unit->data[sp] == 0xff)
  383. ++size;
  384. }
  385. }
  386. }
  387. frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  388. if (!frag->data_ref)
  389. return AVERROR(ENOMEM);
  390. data = frag->data_ref->data;
  391. dp = 0;
  392. data[dp++] = 0xff;
  393. data[dp++] = JPEG_MARKER_SOI;
  394. for (i = 0; i < frag->nb_units; i++) {
  395. unit = &frag->units[i];
  396. data[dp++] = 0xff;
  397. data[dp++] = unit->type;
  398. if (unit->type != JPEG_MARKER_SOS) {
  399. memcpy(data + dp, unit->data, unit->data_size);
  400. dp += unit->data_size;
  401. } else {
  402. sp = AV_RB16(unit->data);
  403. av_assert0(sp <= unit->data_size);
  404. memcpy(data + dp, unit->data, sp);
  405. dp += sp;
  406. for (; sp < unit->data_size; sp++) {
  407. if (unit->data[sp] == 0xff) {
  408. data[dp++] = 0xff;
  409. data[dp++] = 0x00;
  410. } else {
  411. data[dp++] = unit->data[sp];
  412. }
  413. }
  414. }
  415. }
  416. data[dp++] = 0xff;
  417. data[dp++] = JPEG_MARKER_EOI;
  418. av_assert0(dp == size);
  419. memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  420. frag->data = data;
  421. frag->data_size = size;
  422. return 0;
  423. }
  424. static void cbs_jpeg_close(CodedBitstreamContext *ctx)
  425. {
  426. CodedBitstreamJPEGContext *priv = ctx->priv_data;
  427. av_freep(&priv->write_buffer);
  428. }
  429. const CodedBitstreamType ff_cbs_type_jpeg = {
  430. .codec_id = AV_CODEC_ID_MJPEG,
  431. .priv_data_size = sizeof(CodedBitstreamJPEGContext),
  432. .split_fragment = &cbs_jpeg_split_fragment,
  433. .read_unit = &cbs_jpeg_read_unit,
  434. .write_unit = &cbs_jpeg_write_unit,
  435. .assemble_fragment = &cbs_jpeg_assemble_fragment,
  436. .close = &cbs_jpeg_close,
  437. };