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.

469 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 "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 *opaque, 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 *opaque, 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 *opaque, 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. next_marker = -1;
  128. end = start;
  129. for (i = start; i + 1 < frag->data_size; i++) {
  130. if (frag->data[i] != 0xff)
  131. continue;
  132. end = i;
  133. for (++i; i + 1 < frag->data_size &&
  134. frag->data[i] == 0xff; i++);
  135. if (i + 1 < frag->data_size) {
  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. if (length > end - start)
  174. return AVERROR_INVALIDDATA;
  175. data_ref = NULL;
  176. data = av_malloc(end - start +
  177. AV_INPUT_BUFFER_PADDING_SIZE);
  178. if (!data)
  179. return AVERROR(ENOMEM);
  180. memcpy(data, frag->data + start, length);
  181. for (i = start + length, j = length; i < end; i++, j++) {
  182. if (frag->data[i] == 0xff) {
  183. while (frag->data[i] == 0xff)
  184. ++i;
  185. data[j] = 0xff;
  186. } else {
  187. data[j] = frag->data[i];
  188. }
  189. }
  190. data_size = j;
  191. memset(data + data_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  192. } else {
  193. data = frag->data + start;
  194. data_size = end - start;
  195. data_ref = frag->data_ref;
  196. }
  197. err = ff_cbs_insert_unit_data(frag, unit, marker,
  198. data, data_size, data_ref);
  199. if (err < 0)
  200. return err;
  201. if (next_marker == -1)
  202. break;
  203. marker = next_marker;
  204. start = next_start;
  205. }
  206. return 0;
  207. }
  208. static int cbs_jpeg_read_unit(CodedBitstreamContext *ctx,
  209. CodedBitstreamUnit *unit)
  210. {
  211. GetBitContext gbc;
  212. int err;
  213. err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
  214. if (err < 0)
  215. return err;
  216. if (unit->type >= JPEG_MARKER_SOF0 &&
  217. unit->type <= JPEG_MARKER_SOF3) {
  218. err = ff_cbs_alloc_unit_content(unit,
  219. sizeof(JPEGRawFrameHeader),
  220. NULL);
  221. if (err < 0)
  222. return err;
  223. err = cbs_jpeg_read_frame_header(ctx, &gbc, unit->content);
  224. if (err < 0)
  225. return err;
  226. } else if (unit->type >= JPEG_MARKER_APPN &&
  227. unit->type <= JPEG_MARKER_APPN + 15) {
  228. err = ff_cbs_alloc_unit_content(unit,
  229. sizeof(JPEGRawApplicationData),
  230. &cbs_jpeg_free_application_data);
  231. if (err < 0)
  232. return err;
  233. err = cbs_jpeg_read_application_data(ctx, &gbc, unit->content);
  234. if (err < 0)
  235. return err;
  236. } else if (unit->type == JPEG_MARKER_SOS) {
  237. JPEGRawScan *scan;
  238. int pos;
  239. err = ff_cbs_alloc_unit_content(unit,
  240. sizeof(JPEGRawScan),
  241. &cbs_jpeg_free_scan);
  242. if (err < 0)
  243. return err;
  244. scan = unit->content;
  245. err = cbs_jpeg_read_scan_header(ctx, &gbc, &scan->header);
  246. if (err < 0)
  247. return err;
  248. pos = get_bits_count(&gbc);
  249. av_assert0(pos % 8 == 0);
  250. if (pos > 0) {
  251. scan->data_size = unit->data_size - pos / 8;
  252. scan->data_ref = av_buffer_ref(unit->data_ref);
  253. if (!scan->data_ref)
  254. return AVERROR(ENOMEM);
  255. scan->data = unit->data + pos / 8;
  256. }
  257. } else {
  258. switch (unit->type) {
  259. #define SEGMENT(marker, type, func, free) \
  260. case JPEG_MARKER_ ## marker: \
  261. { \
  262. err = ff_cbs_alloc_unit_content(unit, \
  263. sizeof(type), free); \
  264. if (err < 0) \
  265. return err; \
  266. err = cbs_jpeg_read_ ## func(ctx, &gbc, unit->content); \
  267. if (err < 0) \
  268. return err; \
  269. } \
  270. break
  271. SEGMENT(DQT, JPEGRawQuantisationTableSpecification, dqt, NULL);
  272. SEGMENT(DHT, JPEGRawHuffmanTableSpecification, dht, NULL);
  273. SEGMENT(COM, JPEGRawComment, comment, &cbs_jpeg_free_comment);
  274. #undef SEGMENT
  275. default:
  276. return AVERROR(ENOSYS);
  277. }
  278. }
  279. return 0;
  280. }
  281. static int cbs_jpeg_write_scan(CodedBitstreamContext *ctx,
  282. CodedBitstreamUnit *unit,
  283. PutBitContext *pbc)
  284. {
  285. JPEGRawScan *scan = unit->content;
  286. int err;
  287. err = cbs_jpeg_write_scan_header(ctx, pbc, &scan->header);
  288. if (err < 0)
  289. return err;
  290. if (scan->data) {
  291. if (scan->data_size * 8 > put_bits_left(pbc))
  292. return AVERROR(ENOSPC);
  293. av_assert0(put_bits_count(pbc) % 8 == 0);
  294. flush_put_bits(pbc);
  295. memcpy(put_bits_ptr(pbc), scan->data, scan->data_size);
  296. skip_put_bytes(pbc, scan->data_size);
  297. }
  298. return 0;
  299. }
  300. static int cbs_jpeg_write_segment(CodedBitstreamContext *ctx,
  301. CodedBitstreamUnit *unit,
  302. PutBitContext *pbc)
  303. {
  304. int err;
  305. if (unit->type >= JPEG_MARKER_SOF0 &&
  306. unit->type <= JPEG_MARKER_SOF3) {
  307. err = cbs_jpeg_write_frame_header(ctx, pbc, unit->content);
  308. } else if (unit->type >= JPEG_MARKER_APPN &&
  309. unit->type <= JPEG_MARKER_APPN + 15) {
  310. err = cbs_jpeg_write_application_data(ctx, pbc, unit->content);
  311. } else {
  312. switch (unit->type) {
  313. #define SEGMENT(marker, func) \
  314. case JPEG_MARKER_ ## marker: \
  315. err = cbs_jpeg_write_ ## func(ctx, pbc, unit->content); \
  316. break;
  317. SEGMENT(DQT, dqt);
  318. SEGMENT(DHT, dht);
  319. SEGMENT(COM, comment);
  320. default:
  321. return AVERROR_PATCHWELCOME;
  322. }
  323. }
  324. return err;
  325. }
  326. static int cbs_jpeg_write_unit(CodedBitstreamContext *ctx,
  327. CodedBitstreamUnit *unit,
  328. PutBitContext *pbc)
  329. {
  330. if (unit->type == JPEG_MARKER_SOS)
  331. return cbs_jpeg_write_scan (ctx, unit, pbc);
  332. else
  333. return cbs_jpeg_write_segment(ctx, unit, pbc);
  334. }
  335. static int cbs_jpeg_assemble_fragment(CodedBitstreamContext *ctx,
  336. CodedBitstreamFragment *frag)
  337. {
  338. const CodedBitstreamUnit *unit;
  339. uint8_t *data;
  340. size_t size, dp, sp;
  341. int i;
  342. size = 4; // SOI + EOI.
  343. for (i = 0; i < frag->nb_units; i++) {
  344. unit = &frag->units[i];
  345. size += 2 + unit->data_size;
  346. if (unit->type == JPEG_MARKER_SOS) {
  347. for (sp = 0; sp < unit->data_size; sp++) {
  348. if (unit->data[sp] == 0xff)
  349. ++size;
  350. }
  351. }
  352. }
  353. frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  354. if (!frag->data_ref)
  355. return AVERROR(ENOMEM);
  356. data = frag->data_ref->data;
  357. dp = 0;
  358. data[dp++] = 0xff;
  359. data[dp++] = JPEG_MARKER_SOI;
  360. for (i = 0; i < frag->nb_units; i++) {
  361. unit = &frag->units[i];
  362. data[dp++] = 0xff;
  363. data[dp++] = unit->type;
  364. if (unit->type != JPEG_MARKER_SOS) {
  365. memcpy(data + dp, unit->data, unit->data_size);
  366. dp += unit->data_size;
  367. } else {
  368. sp = AV_RB16(unit->data);
  369. av_assert0(sp <= unit->data_size);
  370. memcpy(data + dp, unit->data, sp);
  371. dp += sp;
  372. for (; sp < unit->data_size; sp++) {
  373. if (unit->data[sp] == 0xff) {
  374. data[dp++] = 0xff;
  375. data[dp++] = 0x00;
  376. } else {
  377. data[dp++] = unit->data[sp];
  378. }
  379. }
  380. }
  381. }
  382. data[dp++] = 0xff;
  383. data[dp++] = JPEG_MARKER_EOI;
  384. av_assert0(dp == size);
  385. memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  386. frag->data = data;
  387. frag->data_size = size;
  388. return 0;
  389. }
  390. const CodedBitstreamType ff_cbs_type_jpeg = {
  391. .codec_id = AV_CODEC_ID_MJPEG,
  392. .split_fragment = &cbs_jpeg_split_fragment,
  393. .read_unit = &cbs_jpeg_read_unit,
  394. .write_unit = &cbs_jpeg_write_unit,
  395. .assemble_fragment = &cbs_jpeg_assemble_fragment,
  396. };