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.

376 lines
11KB

  1. /*
  2. * RTP JPEG-compressed Video Depacketizer, RFC 2435
  3. * Copyright (c) 2012 Samuel Pitoiset
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. /**
  26. * RTP/JPEG specific private data.
  27. */
  28. struct PayloadContext {
  29. AVIOContext *frame; ///< current frame buffer
  30. uint32_t timestamp; ///< current frame timestamp
  31. int hdr_size; ///< size of the current frame header
  32. };
  33. static const uint8_t default_quantizers[128] = {
  34. /* luma table */
  35. 16, 11, 12, 14, 12, 10, 16, 14,
  36. 13, 14, 18, 17, 16, 19, 24, 40,
  37. 26, 24, 22, 22, 24, 49, 35, 37,
  38. 29, 40, 58, 51, 61, 60, 57, 51,
  39. 56, 55, 64, 72, 92, 78, 64, 68,
  40. 87, 69, 55, 56, 80, 109, 81, 87,
  41. 95, 98, 103, 104, 103, 62, 77, 113,
  42. 121, 112, 100, 120, 92, 101, 103, 99,
  43. /* chroma table */
  44. 17, 18, 18, 24, 21, 24, 47, 26,
  45. 26, 47, 99, 66, 56, 66, 99, 99,
  46. 99, 99, 99, 99, 99, 99, 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. };
  53. static PayloadContext *jpeg_new_context(void)
  54. {
  55. return av_mallocz(sizeof(PayloadContext));
  56. }
  57. static inline void free_frame_if_needed(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_if_needed(jpeg);
  69. av_free(jpeg);
  70. }
  71. static void jpeg_create_huffman_table(PutBitContext *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. put_bits(p, 8, 0);
  77. put_bits(p, 4, table_class);
  78. put_bits(p, 4, table_id);
  79. for (i = 1; i <= 16; i++) {
  80. n += bits_table[i];
  81. put_bits(p, 8, bits_table[i]);
  82. }
  83. for (i = 0; i < n; i++) {
  84. put_bits(p, 8, value_table[i]);
  85. }
  86. }
  87. static int jpeg_create_header(uint8_t *buf, int size, uint32_t type, uint32_t w,
  88. uint32_t h, const uint8_t *qtable, int nb_qtable)
  89. {
  90. PutBitContext pbc;
  91. init_put_bits(&pbc, buf, size);
  92. /* Convert from blocks to pixels. */
  93. w <<= 3;
  94. h <<= 3;
  95. /* SOI */
  96. put_marker(&pbc, SOI);
  97. /* JFIF header */
  98. put_marker(&pbc, APP0);
  99. put_bits(&pbc, 16, 16);
  100. avpriv_put_string(&pbc, "JFIF", 1);
  101. put_bits(&pbc, 16, 0x0201);
  102. put_bits(&pbc, 8, 0);
  103. put_bits(&pbc, 16, 1);
  104. put_bits(&pbc, 16, 1);
  105. put_bits(&pbc, 8, 0);
  106. put_bits(&pbc, 8, 0);
  107. /* DQT */
  108. put_marker(&pbc, DQT);
  109. if (nb_qtable == 2) {
  110. put_bits(&pbc, 16, 2 + 2 * (1 + 64));
  111. } else {
  112. put_bits(&pbc, 16, 2 + 1 * (1 + 64));
  113. }
  114. put_bits(&pbc, 8, 0);
  115. /* Each table is an array of 64 values given in zig-zag
  116. * order, identical to the format used in a JFIF DQT
  117. * marker segment. */
  118. avpriv_copy_bits(&pbc, qtable, 64 * 8);
  119. if (nb_qtable == 2) {
  120. put_bits(&pbc, 8, 1);
  121. avpriv_copy_bits(&pbc, qtable + 64, 64 * 8);
  122. }
  123. /* DHT */
  124. put_marker(&pbc, DHT);
  125. jpeg_create_huffman_table(&pbc, 0, 0, avpriv_mjpeg_bits_dc_luminance,
  126. avpriv_mjpeg_val_dc);
  127. jpeg_create_huffman_table(&pbc, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  128. avpriv_mjpeg_val_dc);
  129. jpeg_create_huffman_table(&pbc, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  130. avpriv_mjpeg_val_ac_luminance);
  131. jpeg_create_huffman_table(&pbc, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  132. avpriv_mjpeg_val_ac_chrominance);
  133. /* SOF0 */
  134. put_marker(&pbc, SOF0);
  135. put_bits(&pbc, 16, 17);
  136. put_bits(&pbc, 8, 8);
  137. put_bits(&pbc, 8, h >> 8);
  138. put_bits(&pbc, 8, h);
  139. put_bits(&pbc, 8, w >> 8);
  140. put_bits(&pbc, 8, w);
  141. put_bits(&pbc, 8, 3);
  142. put_bits(&pbc, 8, 1);
  143. put_bits(&pbc, 8, type ? 34 : 33);
  144. put_bits(&pbc, 8, 0);
  145. put_bits(&pbc, 8, 2);
  146. put_bits(&pbc, 8, 17);
  147. put_bits(&pbc, 8, nb_qtable == 2 ? 1 : 0);
  148. put_bits(&pbc, 8, 3);
  149. put_bits(&pbc, 8, 17);
  150. put_bits(&pbc, 8, nb_qtable == 2 ? 1 : 0);
  151. /* SOS */
  152. put_marker(&pbc, SOS);
  153. put_bits(&pbc, 16, 12);
  154. put_bits(&pbc, 8, 3);
  155. put_bits(&pbc, 8, 1);
  156. put_bits(&pbc, 8, 0);
  157. put_bits(&pbc, 8, 2);
  158. put_bits(&pbc, 8, 17);
  159. put_bits(&pbc, 8, 3);
  160. put_bits(&pbc, 8, 17);
  161. put_bits(&pbc, 8, 0);
  162. put_bits(&pbc, 8, 63);
  163. put_bits(&pbc, 8, 0);
  164. /* Fill the buffer. */
  165. flush_put_bits(&pbc);
  166. /* Return the length in bytes of the JPEG header. */
  167. return put_bits_count(&pbc) / 8;
  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, int flags)
  188. {
  189. uint8_t type, q, width, height;
  190. const uint8_t *qtables = NULL;
  191. uint16_t qtable_len;
  192. uint32_t off;
  193. int ret;
  194. if (len < 8) {
  195. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  196. return AVERROR_INVALIDDATA;
  197. }
  198. /* Parse the main JPEG header. */
  199. off = AV_RB24(buf + 1); /* fragment byte offset */
  200. type = AV_RB8(buf + 4); /* id of jpeg decoder params */
  201. q = AV_RB8(buf + 5); /* quantization factor (or table id) */
  202. width = AV_RB8(buf + 6); /* frame width in 8 pixel blocks */
  203. height = AV_RB8(buf + 7); /* frame height in 8 pixel blocks */
  204. buf += 8;
  205. len -= 8;
  206. /* Parse the restart marker header. */
  207. if (type > 63) {
  208. av_log(ctx, AV_LOG_ERROR,
  209. "Unimplemented RTP/JPEG restart marker header.\n");
  210. return AVERROR_PATCHWELCOME;
  211. }
  212. /* Parse the quantization table header. */
  213. if (q > 127 && off == 0) {
  214. uint8_t precision;
  215. if (len < 4) {
  216. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  217. return AVERROR_INVALIDDATA;
  218. }
  219. /* The first byte is reserved for future use. */
  220. precision = AV_RB8(buf + 1); /* size of coefficients */
  221. qtable_len = AV_RB16(buf + 2); /* length in bytes */
  222. buf += 4;
  223. len -= 4;
  224. if (precision)
  225. av_log(ctx, AV_LOG_WARNING, "Only 8-bit precision is supported.\n");
  226. if (q == 255 && qtable_len == 0) {
  227. av_log(ctx, AV_LOG_ERROR,
  228. "Invalid RTP/JPEG packet. Quantization tables not found.\n");
  229. return AVERROR_INVALIDDATA;
  230. }
  231. if (qtable_len > 0) {
  232. if (len < qtable_len) {
  233. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  234. return AVERROR_INVALIDDATA;
  235. }
  236. qtables = buf;
  237. buf += qtable_len;
  238. len -= qtable_len;
  239. }
  240. }
  241. if (off == 0) {
  242. /* Start of JPEG data packet. */
  243. uint8_t new_qtables[128];
  244. uint8_t hdr[1024];
  245. /* Skip the current frame in case of the end packet
  246. * has been lost somewhere. */
  247. free_frame_if_needed(jpeg);
  248. if ((ret = avio_open_dyn_buf(&jpeg->frame)) < 0)
  249. return ret;
  250. jpeg->timestamp = *timestamp;
  251. if (!qtables) {
  252. create_default_qtables(new_qtables, q);
  253. qtables = new_qtables;
  254. qtable_len = sizeof(new_qtables);
  255. }
  256. /* Generate a frame and scan headers that can be prepended to the
  257. * RTP/JPEG data payload to produce a JPEG compressed image in
  258. * interchange format. */
  259. jpeg->hdr_size = jpeg_create_header(hdr, sizeof(hdr), type, width,
  260. height, qtables,
  261. qtable_len > 64 ? 2 : 1);
  262. /* Copy JPEG header to frame buffer. */
  263. avio_write(jpeg->frame, hdr, jpeg->hdr_size);
  264. }
  265. if (!jpeg->frame) {
  266. av_log(ctx, AV_LOG_ERROR,
  267. "Received packet without a start chunk; dropping frame.\n");
  268. return AVERROR(EAGAIN);
  269. }
  270. if (jpeg->timestamp != *timestamp) {
  271. /* Skip the current frame if timestamp is incorrect.
  272. * A start packet has been lost somewhere. */
  273. free_frame_if_needed(jpeg);
  274. av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match.\n");
  275. return AVERROR_INVALIDDATA;
  276. }
  277. if (off != avio_tell(jpeg->frame) - jpeg->hdr_size) {
  278. av_log(ctx, AV_LOG_ERROR,
  279. "Missing packets; dropping frame.\n");
  280. return AVERROR(EAGAIN);
  281. }
  282. /* Copy data to frame buffer. */
  283. avio_write(jpeg->frame, buf, len);
  284. if (flags & RTP_FLAG_MARKER) {
  285. /* End of JPEG data packet. */
  286. PutBitContext pbc;
  287. uint8_t buf[2];
  288. /* Put EOI marker. */
  289. init_put_bits(&pbc, buf, sizeof(buf));
  290. put_marker(&pbc, EOI);
  291. flush_put_bits(&pbc);
  292. avio_write(jpeg->frame, buf, sizeof(buf));
  293. /* Prepare the JPEG packet. */
  294. av_init_packet(pkt);
  295. pkt->size = avio_close_dyn_buf(jpeg->frame, &pkt->data);
  296. if (pkt->size < 0) {
  297. av_log(ctx, AV_LOG_ERROR,
  298. "Error occured when getting frame buffer.\n");
  299. jpeg->frame = NULL;
  300. return pkt->size;
  301. }
  302. pkt->stream_index = st->index;
  303. pkt->destruct = av_destruct_packet;
  304. /* Re-init the frame buffer. */
  305. jpeg->frame = NULL;
  306. return 0;
  307. }
  308. return AVERROR(EAGAIN);
  309. }
  310. RTPDynamicProtocolHandler ff_jpeg_dynamic_handler = {
  311. .enc_name = "JPEG",
  312. .codec_type = AVMEDIA_TYPE_VIDEO,
  313. .codec_id = AV_CODEC_ID_MJPEG,
  314. .alloc = jpeg_new_context,
  315. .free = jpeg_free_context,
  316. .parse_packet = jpeg_parse_packet,
  317. .static_payload_id = 26,
  318. };