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.

388 lines
13KB

  1. /*
  2. * RTP JPEG-compressed Video Depacketizer, RFC 2435
  3. * Copyright (c) 2012 Samuel Pitoiset
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "rtpdec.h"
  23. #include "rtpdec_formats.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavcodec/mjpeg.h"
  26. #include "libavcodec/bytestream.h"
  27. /**
  28. * RTP/JPEG specific private data.
  29. */
  30. struct PayloadContext {
  31. AVIOContext *frame; ///< current frame buffer
  32. uint32_t timestamp; ///< current frame timestamp
  33. int hdr_size; ///< size of the current frame header
  34. uint8_t qtables[128][128];
  35. uint8_t qtables_len[128];
  36. };
  37. static const uint8_t default_quantizers[128] = {
  38. /* luma table */
  39. 16, 11, 12, 14, 12, 10, 16, 14,
  40. 13, 14, 18, 17, 16, 19, 24, 40,
  41. 26, 24, 22, 22, 24, 49, 35, 37,
  42. 29, 40, 58, 51, 61, 60, 57, 51,
  43. 56, 55, 64, 72, 92, 78, 64, 68,
  44. 87, 69, 55, 56, 80, 109, 81, 87,
  45. 95, 98, 103, 104, 103, 62, 77, 113,
  46. 121, 112, 100, 120, 92, 101, 103, 99,
  47. /* chroma table */
  48. 17, 18, 18, 24, 21, 24, 47, 26,
  49. 26, 47, 99, 66, 56, 66, 99, 99,
  50. 99, 99, 99, 99, 99, 99, 99, 99,
  51. 99, 99, 99, 99, 99, 99, 99, 99,
  52. 99, 99, 99, 99, 99, 99, 99, 99,
  53. 99, 99, 99, 99, 99, 99, 99, 99,
  54. 99, 99, 99, 99, 99, 99, 99, 99,
  55. 99, 99, 99, 99, 99, 99, 99, 99
  56. };
  57. static inline void free_frame(PayloadContext *jpeg)
  58. {
  59. if (jpeg->frame) {
  60. uint8_t *p;
  61. avio_close_dyn_buf(jpeg->frame, &p);
  62. av_free(p);
  63. jpeg->frame = NULL;
  64. }
  65. }
  66. static void jpeg_free_context(PayloadContext *jpeg)
  67. {
  68. free_frame(jpeg);
  69. av_free(jpeg);
  70. }
  71. static int jpeg_create_huffman_table(PutByteContext *p, int table_class,
  72. int table_id, const uint8_t *bits_table,
  73. const uint8_t *value_table)
  74. {
  75. int i, n = 0;
  76. bytestream2_put_byte(p, table_class << 4 | table_id);
  77. for (i = 1; i <= 16; i++) {
  78. n += bits_table[i];
  79. bytestream2_put_byte(p, bits_table[i]);
  80. }
  81. for (i = 0; i < n; i++) {
  82. bytestream2_put_byte(p, value_table[i]);
  83. }
  84. return n + 17;
  85. }
  86. static void jpeg_put_marker(PutByteContext *pbc, int code)
  87. {
  88. bytestream2_put_byte(pbc, 0xff);
  89. bytestream2_put_byte(pbc, code);
  90. }
  91. static int jpeg_create_header(uint8_t *buf, int size, uint32_t type, uint32_t w,
  92. uint32_t h, const uint8_t *qtable, int nb_qtable)
  93. {
  94. PutByteContext pbc;
  95. uint8_t *dht_size_ptr;
  96. int dht_size, i;
  97. bytestream2_init_writer(&pbc, buf, size);
  98. /* Convert from blocks to pixels. */
  99. w <<= 3;
  100. h <<= 3;
  101. /* SOI */
  102. jpeg_put_marker(&pbc, SOI);
  103. /* JFIF header */
  104. jpeg_put_marker(&pbc, APP0);
  105. bytestream2_put_be16(&pbc, 16);
  106. bytestream2_put_buffer(&pbc, "JFIF", 5);
  107. bytestream2_put_be16(&pbc, 0x0201);
  108. bytestream2_put_byte(&pbc, 0);
  109. bytestream2_put_be16(&pbc, 1);
  110. bytestream2_put_be16(&pbc, 1);
  111. bytestream2_put_byte(&pbc, 0);
  112. bytestream2_put_byte(&pbc, 0);
  113. /* DQT */
  114. jpeg_put_marker(&pbc, DQT);
  115. bytestream2_put_be16(&pbc, 2 + nb_qtable * (1 + 64));
  116. for (i = 0; i < nb_qtable; i++) {
  117. bytestream2_put_byte(&pbc, i);
  118. /* Each table is an array of 64 values given in zig-zag
  119. * order, identical to the format used in a JFIF DQT
  120. * marker segment. */
  121. bytestream2_put_buffer(&pbc, qtable + 64 * i, 64);
  122. }
  123. /* DHT */
  124. jpeg_put_marker(&pbc, DHT);
  125. dht_size_ptr = pbc.buffer;
  126. bytestream2_put_be16(&pbc, 0);
  127. dht_size = 2;
  128. dht_size += jpeg_create_huffman_table(&pbc, 0, 0,avpriv_mjpeg_bits_dc_luminance,
  129. avpriv_mjpeg_val_dc);
  130. dht_size += jpeg_create_huffman_table(&pbc, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  131. avpriv_mjpeg_val_dc);
  132. dht_size += jpeg_create_huffman_table(&pbc, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  133. avpriv_mjpeg_val_ac_luminance);
  134. dht_size += jpeg_create_huffman_table(&pbc, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  135. avpriv_mjpeg_val_ac_chrominance);
  136. AV_WB16(dht_size_ptr, dht_size);
  137. /* SOF0 */
  138. jpeg_put_marker(&pbc, SOF0);
  139. bytestream2_put_be16(&pbc, 17); /* size */
  140. bytestream2_put_byte(&pbc, 8); /* bits per component */
  141. bytestream2_put_be16(&pbc, h);
  142. bytestream2_put_be16(&pbc, w);
  143. bytestream2_put_byte(&pbc, 3); /* number of components */
  144. bytestream2_put_byte(&pbc, 1); /* component number */
  145. bytestream2_put_byte(&pbc, (2 << 4) | (type ? 2 : 1)); /* hsample/vsample */
  146. bytestream2_put_byte(&pbc, 0); /* matrix number */
  147. bytestream2_put_byte(&pbc, 2); /* component number */
  148. bytestream2_put_byte(&pbc, 1 << 4 | 1); /* hsample/vsample */
  149. bytestream2_put_byte(&pbc, nb_qtable == 2 ? 1 : 0); /* matrix number */
  150. bytestream2_put_byte(&pbc, 3); /* component number */
  151. bytestream2_put_byte(&pbc, 1 << 4 | 1); /* hsample/vsample */
  152. bytestream2_put_byte(&pbc, nb_qtable == 2 ? 1 : 0); /* matrix number */
  153. /* SOS */
  154. jpeg_put_marker(&pbc, SOS);
  155. bytestream2_put_be16(&pbc, 12);
  156. bytestream2_put_byte(&pbc, 3);
  157. bytestream2_put_byte(&pbc, 1);
  158. bytestream2_put_byte(&pbc, 0);
  159. bytestream2_put_byte(&pbc, 2);
  160. bytestream2_put_byte(&pbc, 17);
  161. bytestream2_put_byte(&pbc, 3);
  162. bytestream2_put_byte(&pbc, 17);
  163. bytestream2_put_byte(&pbc, 0);
  164. bytestream2_put_byte(&pbc, 63);
  165. bytestream2_put_byte(&pbc, 0);
  166. /* Return the length in bytes of the JPEG header. */
  167. return bytestream2_tell_p(&pbc);
  168. }
  169. static void create_default_qtables(uint8_t *qtables, uint8_t q)
  170. {
  171. int factor = q;
  172. int i;
  173. factor = av_clip(q, 1, 99);
  174. if (q < 50)
  175. q = 5000 / factor;
  176. else
  177. q = 200 - factor * 2;
  178. for (i = 0; i < 128; i++) {
  179. int val = (default_quantizers[i] * q + 50) / 100;
  180. /* Limit the quantizers to 1 <= q <= 255. */
  181. val = av_clip(val, 1, 255);
  182. qtables[i] = val;
  183. }
  184. }
  185. static int jpeg_parse_packet(AVFormatContext *ctx, PayloadContext *jpeg,
  186. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  187. const uint8_t *buf, int len, uint16_t seq,
  188. int flags)
  189. {
  190. uint8_t type, q, width, height;
  191. const uint8_t *qtables = NULL;
  192. uint16_t qtable_len;
  193. uint32_t off;
  194. int ret;
  195. if (len < 8) {
  196. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  197. return AVERROR_INVALIDDATA;
  198. }
  199. /* Parse the main JPEG header. */
  200. off = AV_RB24(buf + 1); /* fragment byte offset */
  201. type = AV_RB8(buf + 4); /* id of jpeg decoder params */
  202. q = AV_RB8(buf + 5); /* quantization factor (or table id) */
  203. width = AV_RB8(buf + 6); /* frame width in 8 pixel blocks */
  204. height = AV_RB8(buf + 7); /* frame height in 8 pixel blocks */
  205. buf += 8;
  206. len -= 8;
  207. /* Parse the restart marker header. */
  208. if (type > 63) {
  209. av_log(ctx, AV_LOG_ERROR,
  210. "Unimplemented RTP/JPEG restart marker header.\n");
  211. return AVERROR_PATCHWELCOME;
  212. }
  213. if (type > 1) {
  214. av_log(ctx, AV_LOG_ERROR, "Unimplemented RTP/JPEG type %d\n", type);
  215. return AVERROR_PATCHWELCOME;
  216. }
  217. /* Parse the quantization table header. */
  218. if (off == 0) {
  219. /* Start of JPEG data packet. */
  220. uint8_t new_qtables[128];
  221. uint8_t hdr[1024];
  222. if (q > 127) {
  223. uint8_t precision;
  224. if (len < 4) {
  225. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  226. return AVERROR_INVALIDDATA;
  227. }
  228. /* The first byte is reserved for future use. */
  229. precision = AV_RB8(buf + 1); /* size of coefficients */
  230. qtable_len = AV_RB16(buf + 2); /* length in bytes */
  231. buf += 4;
  232. len -= 4;
  233. if (precision)
  234. av_log(ctx, AV_LOG_WARNING, "Only 8-bit precision is supported.\n");
  235. if (qtable_len > 0) {
  236. if (len < qtable_len) {
  237. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  238. return AVERROR_INVALIDDATA;
  239. }
  240. qtables = buf;
  241. buf += qtable_len;
  242. len -= qtable_len;
  243. if (q < 255) {
  244. if (jpeg->qtables_len[q - 128] &&
  245. (jpeg->qtables_len[q - 128] != qtable_len ||
  246. memcmp(qtables, &jpeg->qtables[q - 128][0], qtable_len))) {
  247. av_log(ctx, AV_LOG_WARNING,
  248. "Quantization tables for q=%d changed\n", q);
  249. } else if (!jpeg->qtables_len[q - 128] && qtable_len <= 128) {
  250. memcpy(&jpeg->qtables[q - 128][0], qtables,
  251. qtable_len);
  252. jpeg->qtables_len[q - 128] = qtable_len;
  253. }
  254. }
  255. } else {
  256. if (q == 255) {
  257. av_log(ctx, AV_LOG_ERROR,
  258. "Invalid RTP/JPEG packet. Quantization tables not found.\n");
  259. return AVERROR_INVALIDDATA;
  260. }
  261. if (!jpeg->qtables_len[q - 128]) {
  262. av_log(ctx, AV_LOG_ERROR,
  263. "No quantization tables known for q=%d yet.\n", q);
  264. return AVERROR_INVALIDDATA;
  265. }
  266. qtables = &jpeg->qtables[q - 128][0];
  267. qtable_len = jpeg->qtables_len[q - 128];
  268. }
  269. } else { /* q <= 127 */
  270. if (q == 0 || q > 99) {
  271. av_log(ctx, AV_LOG_ERROR, "Reserved q value %d\n", q);
  272. return AVERROR_INVALIDDATA;
  273. }
  274. create_default_qtables(new_qtables, q);
  275. qtables = new_qtables;
  276. qtable_len = sizeof(new_qtables);
  277. }
  278. /* Skip the current frame in case of the end packet
  279. * has been lost somewhere. */
  280. free_frame(jpeg);
  281. if ((ret = avio_open_dyn_buf(&jpeg->frame)) < 0)
  282. return ret;
  283. jpeg->timestamp = *timestamp;
  284. /* Generate a frame and scan headers that can be prepended to the
  285. * RTP/JPEG data payload to produce a JPEG compressed image in
  286. * interchange format. */
  287. jpeg->hdr_size = jpeg_create_header(hdr, sizeof(hdr), type, width,
  288. height, qtables,
  289. qtable_len / 64);
  290. /* Copy JPEG header to frame buffer. */
  291. avio_write(jpeg->frame, hdr, jpeg->hdr_size);
  292. }
  293. if (!jpeg->frame) {
  294. av_log(ctx, AV_LOG_ERROR,
  295. "Received packet without a start chunk; dropping frame.\n");
  296. return AVERROR(EAGAIN);
  297. }
  298. if (jpeg->timestamp != *timestamp) {
  299. /* Skip the current frame if timestamp is incorrect.
  300. * A start packet has been lost somewhere. */
  301. free_frame(jpeg);
  302. av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match.\n");
  303. return AVERROR_INVALIDDATA;
  304. }
  305. if (off != avio_tell(jpeg->frame) - jpeg->hdr_size) {
  306. av_log(ctx, AV_LOG_ERROR,
  307. "Missing packets; dropping frame.\n");
  308. return AVERROR(EAGAIN);
  309. }
  310. /* Copy data to frame buffer. */
  311. avio_write(jpeg->frame, buf, len);
  312. if (flags & RTP_FLAG_MARKER) {
  313. /* End of JPEG data packet. */
  314. uint8_t buf[2] = { 0xff, EOI };
  315. /* Put EOI marker. */
  316. avio_write(jpeg->frame, buf, sizeof(buf));
  317. /* Prepare the JPEG packet. */
  318. if ((ret = ff_rtp_finalize_packet(pkt, &jpeg->frame, st->index)) < 0) {
  319. av_log(ctx, AV_LOG_ERROR,
  320. "Error occurred when getting frame buffer.\n");
  321. return ret;
  322. }
  323. return 0;
  324. }
  325. return AVERROR(EAGAIN);
  326. }
  327. RTPDynamicProtocolHandler ff_jpeg_dynamic_handler = {
  328. .enc_name = "JPEG",
  329. .codec_type = AVMEDIA_TYPE_VIDEO,
  330. .codec_id = AV_CODEC_ID_MJPEG,
  331. .priv_data_size = sizeof(PayloadContext),
  332. .free = jpeg_free_context,
  333. .parse_packet = jpeg_parse_packet,
  334. .static_payload_id = 26,
  335. };