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.

372 lines
12KB

  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_formats.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavcodec/mjpeg.h"
  25. #include "libavcodec/bytestream.h"
  26. /**
  27. * RTP/JPEG specific private data.
  28. */
  29. struct PayloadContext {
  30. AVIOContext *frame; ///< current frame buffer
  31. uint32_t timestamp; ///< current frame timestamp
  32. int hdr_size; ///< size of the current frame header
  33. };
  34. static const uint8_t default_quantizers[128] = {
  35. /* luma table */
  36. 16, 11, 12, 14, 12, 10, 16, 14,
  37. 13, 14, 18, 17, 16, 19, 24, 40,
  38. 26, 24, 22, 22, 24, 49, 35, 37,
  39. 29, 40, 58, 51, 61, 60, 57, 51,
  40. 56, 55, 64, 72, 92, 78, 64, 68,
  41. 87, 69, 55, 56, 80, 109, 81, 87,
  42. 95, 98, 103, 104, 103, 62, 77, 113,
  43. 121, 112, 100, 120, 92, 101, 103, 99,
  44. /* chroma table */
  45. 17, 18, 18, 24, 21, 24, 47, 26,
  46. 26, 47, 99, 66, 56, 66, 99, 99,
  47. 99, 99, 99, 99, 99, 99, 99, 99,
  48. 99, 99, 99, 99, 99, 99, 99, 99,
  49. 99, 99, 99, 99, 99, 99, 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. };
  54. static PayloadContext *jpeg_new_context(void)
  55. {
  56. return av_mallocz(sizeof(PayloadContext));
  57. }
  58. static inline void free_frame_if_needed(PayloadContext *jpeg)
  59. {
  60. if (jpeg->frame) {
  61. uint8_t *p;
  62. avio_close_dyn_buf(jpeg->frame, &p);
  63. av_free(p);
  64. jpeg->frame = NULL;
  65. }
  66. }
  67. static void jpeg_free_context(PayloadContext *jpeg)
  68. {
  69. free_frame_if_needed(jpeg);
  70. av_free(jpeg);
  71. }
  72. static int jpeg_create_huffman_table(PutByteContext *p, int table_class,
  73. int table_id, const uint8_t *bits_table,
  74. const uint8_t *value_table)
  75. {
  76. int i, n = 0;
  77. bytestream2_put_byte(p, table_class << 4 | table_id);
  78. for (i = 1; i <= 16; i++) {
  79. n += bits_table[i];
  80. bytestream2_put_byte(p, bits_table[i]);
  81. }
  82. for (i = 0; i < n; i++) {
  83. bytestream2_put_byte(p, value_table[i]);
  84. }
  85. return n + 17;
  86. }
  87. static void jpeg_put_marker(PutByteContext *pbc, int code)
  88. {
  89. bytestream2_put_byte(pbc, 0xff);
  90. bytestream2_put_byte(pbc, code);
  91. }
  92. static int jpeg_create_header(uint8_t *buf, int size, uint32_t type, uint32_t w,
  93. uint32_t h, const uint8_t *qtable, int nb_qtable)
  94. {
  95. PutByteContext pbc;
  96. uint8_t *dht_size_ptr;
  97. int dht_size, i;
  98. bytestream2_init_writer(&pbc, buf, size);
  99. /* Convert from blocks to pixels. */
  100. w <<= 3;
  101. h <<= 3;
  102. /* SOI */
  103. jpeg_put_marker(&pbc, SOI);
  104. /* JFIF header */
  105. jpeg_put_marker(&pbc, APP0);
  106. bytestream2_put_be16(&pbc, 16);
  107. bytestream2_put_buffer(&pbc, "JFIF", 5);
  108. bytestream2_put_be16(&pbc, 0x0201);
  109. bytestream2_put_byte(&pbc, 0);
  110. bytestream2_put_be16(&pbc, 1);
  111. bytestream2_put_be16(&pbc, 1);
  112. bytestream2_put_byte(&pbc, 0);
  113. bytestream2_put_byte(&pbc, 0);
  114. /* DQT */
  115. jpeg_put_marker(&pbc, DQT);
  116. bytestream2_put_be16(&pbc, 2 + nb_qtable * (1 + 64));
  117. for (i = 0; i < nb_qtable; i++) {
  118. bytestream2_put_byte(&pbc, i);
  119. /* Each table is an array of 64 values given in zig-zag
  120. * order, identical to the format used in a JFIF DQT
  121. * marker segment. */
  122. bytestream2_put_buffer(&pbc, qtable + 64 * i, 64);
  123. }
  124. /* DHT */
  125. jpeg_put_marker(&pbc, DHT);
  126. dht_size_ptr = pbc.buffer;
  127. bytestream2_put_be16(&pbc, 0);
  128. dht_size = 2;
  129. dht_size += jpeg_create_huffman_table(&pbc, 0, 0,avpriv_mjpeg_bits_dc_luminance,
  130. avpriv_mjpeg_val_dc);
  131. dht_size += jpeg_create_huffman_table(&pbc, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  132. avpriv_mjpeg_val_dc);
  133. dht_size += jpeg_create_huffman_table(&pbc, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  134. avpriv_mjpeg_val_ac_luminance);
  135. dht_size += jpeg_create_huffman_table(&pbc, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  136. avpriv_mjpeg_val_ac_chrominance);
  137. AV_WB16(dht_size_ptr, dht_size);
  138. /* SOF0 */
  139. jpeg_put_marker(&pbc, SOF0);
  140. bytestream2_put_be16(&pbc, 17); /* size */
  141. bytestream2_put_byte(&pbc, 8); /* bits per component */
  142. bytestream2_put_be16(&pbc, h);
  143. bytestream2_put_be16(&pbc, w);
  144. bytestream2_put_byte(&pbc, 3); /* number of components */
  145. bytestream2_put_byte(&pbc, 1); /* component number */
  146. bytestream2_put_byte(&pbc, (2 << 4) | (type ? 2 : 1)); /* hsample/vsample */
  147. bytestream2_put_byte(&pbc, 0); /* matrix number */
  148. bytestream2_put_byte(&pbc, 2); /* component number */
  149. bytestream2_put_byte(&pbc, 1 << 4 | 1); /* hsample/vsample */
  150. bytestream2_put_byte(&pbc, nb_qtable == 2 ? 1 : 0); /* matrix number */
  151. bytestream2_put_byte(&pbc, 3); /* component number */
  152. bytestream2_put_byte(&pbc, 1 << 4 | 1); /* hsample/vsample */
  153. bytestream2_put_byte(&pbc, nb_qtable == 2 ? 1 : 0); /* matrix number */
  154. /* SOS */
  155. jpeg_put_marker(&pbc, SOS);
  156. bytestream2_put_be16(&pbc, 12);
  157. bytestream2_put_byte(&pbc, 3);
  158. bytestream2_put_byte(&pbc, 1);
  159. bytestream2_put_byte(&pbc, 0);
  160. bytestream2_put_byte(&pbc, 2);
  161. bytestream2_put_byte(&pbc, 17);
  162. bytestream2_put_byte(&pbc, 3);
  163. bytestream2_put_byte(&pbc, 17);
  164. bytestream2_put_byte(&pbc, 0);
  165. bytestream2_put_byte(&pbc, 63);
  166. bytestream2_put_byte(&pbc, 0);
  167. /* Return the length in bytes of the JPEG header. */
  168. return bytestream2_tell_p(&pbc);
  169. }
  170. static void create_default_qtables(uint8_t *qtables, uint8_t q)
  171. {
  172. int factor = q;
  173. int i;
  174. factor = av_clip(q, 1, 99);
  175. if (q < 50)
  176. q = 5000 / factor;
  177. else
  178. q = 200 - factor * 2;
  179. for (i = 0; i < 128; i++) {
  180. int val = (default_quantizers[i] * q + 50) / 100;
  181. /* Limit the quantizers to 1 <= q <= 255. */
  182. val = av_clip(val, 1, 255);
  183. qtables[i] = val;
  184. }
  185. }
  186. static int jpeg_parse_packet(AVFormatContext *ctx, PayloadContext *jpeg,
  187. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  188. const uint8_t *buf, int len, 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. /* Parse the quantization table header. */
  214. if (off == 0) {
  215. /* Start of JPEG data packet. */
  216. uint8_t new_qtables[128];
  217. uint8_t hdr[1024];
  218. if (q > 127) {
  219. uint8_t precision;
  220. if (len < 4) {
  221. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  222. return AVERROR_INVALIDDATA;
  223. }
  224. /* The first byte is reserved for future use. */
  225. precision = AV_RB8(buf + 1); /* size of coefficients */
  226. qtable_len = AV_RB16(buf + 2); /* length in bytes */
  227. buf += 4;
  228. len -= 4;
  229. if (precision)
  230. av_log(ctx, AV_LOG_WARNING, "Only 8-bit precision is supported.\n");
  231. if (q == 255 && qtable_len == 0) {
  232. av_log(ctx, AV_LOG_ERROR,
  233. "Invalid RTP/JPEG packet. Quantization tables not found.\n");
  234. return AVERROR_INVALIDDATA;
  235. }
  236. if (qtable_len > 0) {
  237. if (len < qtable_len) {
  238. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  239. return AVERROR_INVALIDDATA;
  240. }
  241. qtables = buf;
  242. buf += qtable_len;
  243. len -= qtable_len;
  244. }
  245. }
  246. /* Skip the current frame in case of the end packet
  247. * has been lost somewhere. */
  248. free_frame_if_needed(jpeg);
  249. if ((ret = avio_open_dyn_buf(&jpeg->frame)) < 0)
  250. return ret;
  251. jpeg->timestamp = *timestamp;
  252. if (!qtables) {
  253. create_default_qtables(new_qtables, q);
  254. qtables = new_qtables;
  255. qtable_len = sizeof(new_qtables);
  256. }
  257. /* Generate a frame and scan headers that can be prepended to the
  258. * RTP/JPEG data payload to produce a JPEG compressed image in
  259. * interchange format. */
  260. jpeg->hdr_size = jpeg_create_header(hdr, sizeof(hdr), type, width,
  261. height, qtables,
  262. qtable_len > 64 ? 2 : 1);
  263. /* Copy JPEG header to frame buffer. */
  264. avio_write(jpeg->frame, hdr, jpeg->hdr_size);
  265. }
  266. if (!jpeg->frame) {
  267. av_log(ctx, AV_LOG_ERROR,
  268. "Received packet without a start chunk; dropping frame.\n");
  269. return AVERROR(EAGAIN);
  270. }
  271. if (jpeg->timestamp != *timestamp) {
  272. /* Skip the current frame if timestamp is incorrect.
  273. * A start packet has been lost somewhere. */
  274. free_frame_if_needed(jpeg);
  275. av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match.\n");
  276. return AVERROR_INVALIDDATA;
  277. }
  278. if (off != avio_tell(jpeg->frame) - jpeg->hdr_size) {
  279. av_log(ctx, AV_LOG_ERROR,
  280. "Missing packets; dropping frame.\n");
  281. return AVERROR(EAGAIN);
  282. }
  283. /* Copy data to frame buffer. */
  284. avio_write(jpeg->frame, buf, len);
  285. if (flags & RTP_FLAG_MARKER) {
  286. /* End of JPEG data packet. */
  287. uint8_t buf[2] = { 0xff, EOI };
  288. /* Put EOI marker. */
  289. avio_write(jpeg->frame, buf, sizeof(buf));
  290. /* Prepare the JPEG packet. */
  291. av_init_packet(pkt);
  292. pkt->size = avio_close_dyn_buf(jpeg->frame, &pkt->data);
  293. if (pkt->size < 0) {
  294. av_log(ctx, AV_LOG_ERROR,
  295. "Error occured when getting frame buffer.\n");
  296. jpeg->frame = NULL;
  297. return pkt->size;
  298. }
  299. pkt->stream_index = st->index;
  300. pkt->destruct = av_destruct_packet;
  301. /* Re-init the frame buffer. */
  302. jpeg->frame = NULL;
  303. return 0;
  304. }
  305. return AVERROR(EAGAIN);
  306. }
  307. RTPDynamicProtocolHandler ff_jpeg_dynamic_handler = {
  308. .enc_name = "JPEG",
  309. .codec_type = AVMEDIA_TYPE_VIDEO,
  310. .codec_id = AV_CODEC_ID_MJPEG,
  311. .alloc = jpeg_new_context,
  312. .free = jpeg_free_context,
  313. .parse_packet = jpeg_parse_packet,
  314. .static_payload_id = 26,
  315. };