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.

228 lines
7.8KB

  1. /*
  2. * RTP JPEG-compressed video Packetizer, 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 "libavcodec/bytestream.h"
  22. #include "libavcodec/mjpeg.h"
  23. #include "libavcodec/jpegtables.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "rtpenc.h"
  26. void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buf, int size)
  27. {
  28. RTPMuxContext *s = s1->priv_data;
  29. const uint8_t *qtables[4] = { NULL };
  30. int nb_qtables = 0;
  31. uint8_t type;
  32. uint8_t w, h;
  33. uint8_t *p;
  34. int off = 0; /* fragment offset of the current JPEG frame */
  35. int len;
  36. int i;
  37. int default_huffman_tables = 0;
  38. s->buf_ptr = s->buf;
  39. s->timestamp = s->cur_timestamp;
  40. /* convert video pixel dimensions from pixels to blocks */
  41. w = AV_CEIL_RSHIFT(s1->streams[0]->codec->width, 3);
  42. h = AV_CEIL_RSHIFT(s1->streams[0]->codec->height, 3);
  43. /* get the pixel format type or fail */
  44. if (s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUVJ422P ||
  45. (s1->streams[0]->codec->color_range == AVCOL_RANGE_JPEG &&
  46. s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV422P)) {
  47. type = 0;
  48. } else if (s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUVJ420P ||
  49. (s1->streams[0]->codec->color_range == AVCOL_RANGE_JPEG &&
  50. s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV420P)) {
  51. type = 1;
  52. } else {
  53. av_log(s1, AV_LOG_ERROR, "Unsupported pixel format\n");
  54. return;
  55. }
  56. /* preparse the header for getting some infos */
  57. for (i = 0; i < size; i++) {
  58. if (buf[i] != 0xff)
  59. continue;
  60. if (buf[i + 1] == DQT) {
  61. int tables, j;
  62. if (buf[i + 4] & 0xF0)
  63. av_log(s1, AV_LOG_WARNING,
  64. "Only 8-bit precision is supported.\n");
  65. /* a quantization table is 64 bytes long */
  66. tables = AV_RB16(&buf[i + 2]) / 65;
  67. if (i + 5 + tables * 65 > size) {
  68. av_log(s1, AV_LOG_ERROR, "Too short JPEG header. Aborted!\n");
  69. return;
  70. }
  71. if (nb_qtables + tables > 4) {
  72. av_log(s1, AV_LOG_ERROR, "Invalid number of quantisation tables\n");
  73. return;
  74. }
  75. for (j = 0; j < tables; j++)
  76. qtables[nb_qtables + j] = buf + i + 5 + j * 65;
  77. nb_qtables += tables;
  78. } else if (buf[i + 1] == SOF0) {
  79. if (buf[i + 14] != 17 || buf[i + 17] != 17) {
  80. av_log(s1, AV_LOG_ERROR,
  81. "Only 1x1 chroma blocks are supported. Aborted!\n");
  82. return;
  83. }
  84. } else if (buf[i + 1] == DHT) {
  85. int dht_size = AV_RB16(&buf[i + 2]);
  86. default_huffman_tables |= 1 << 4;
  87. i += 3;
  88. dht_size -= 2;
  89. if (i + dht_size >= size)
  90. continue;
  91. while (dht_size > 0)
  92. switch (buf[i + 1]) {
  93. case 0x00:
  94. if ( dht_size >= 29
  95. && !memcmp(buf + i + 2, avpriv_mjpeg_bits_dc_luminance + 1, 16)
  96. && !memcmp(buf + i + 18, avpriv_mjpeg_val_dc, 12)) {
  97. default_huffman_tables |= 1;
  98. i += 29;
  99. dht_size -= 29;
  100. } else {
  101. i += dht_size;
  102. dht_size = 0;
  103. }
  104. break;
  105. case 0x01:
  106. if ( dht_size >= 29
  107. && !memcmp(buf + i + 2, avpriv_mjpeg_bits_dc_chrominance + 1, 16)
  108. && !memcmp(buf + i + 18, avpriv_mjpeg_val_dc, 12)) {
  109. default_huffman_tables |= 1 << 1;
  110. i += 29;
  111. dht_size -= 29;
  112. } else {
  113. i += dht_size;
  114. dht_size = 0;
  115. }
  116. break;
  117. case 0x10:
  118. if ( dht_size >= 179
  119. && !memcmp(buf + i + 2, avpriv_mjpeg_bits_ac_luminance + 1, 16)
  120. && !memcmp(buf + i + 18, avpriv_mjpeg_val_ac_luminance, 162)) {
  121. default_huffman_tables |= 1 << 2;
  122. i += 179;
  123. dht_size -= 179;
  124. } else {
  125. i += dht_size;
  126. dht_size = 0;
  127. }
  128. break;
  129. case 0x11:
  130. if ( dht_size >= 179
  131. && !memcmp(buf + i + 2, avpriv_mjpeg_bits_ac_chrominance + 1, 16)
  132. && !memcmp(buf + i + 18, avpriv_mjpeg_val_ac_chrominance, 162)) {
  133. default_huffman_tables |= 1 << 3;
  134. i += 179;
  135. dht_size -= 179;
  136. } else {
  137. i += dht_size;
  138. dht_size = 0;
  139. }
  140. break;
  141. default:
  142. i += dht_size;
  143. dht_size = 0;
  144. continue;
  145. }
  146. } else if (buf[i + 1] == SOS) {
  147. /* SOS is last marker in the header */
  148. i += AV_RB16(&buf[i + 2]) + 2;
  149. if (i > size) {
  150. av_log(s1, AV_LOG_ERROR,
  151. "Insufficient data. Aborted!\n");
  152. return;
  153. }
  154. break;
  155. }
  156. }
  157. if (default_huffman_tables && default_huffman_tables != 31) {
  158. av_log(s1, AV_LOG_ERROR,
  159. "RFC 2435 requires standard Huffman tables for jpeg\n");
  160. return;
  161. }
  162. if (nb_qtables && nb_qtables != 2)
  163. av_log(s1, AV_LOG_WARNING,
  164. "RFC 2435 suggests two quantization tables, %d provided\n",
  165. nb_qtables);
  166. /* skip JPEG header */
  167. buf += i;
  168. size -= i;
  169. for (i = size - 2; i >= 0; i--) {
  170. if (buf[i] == 0xff && buf[i + 1] == EOI) {
  171. /* Remove the EOI marker */
  172. size = i;
  173. break;
  174. }
  175. }
  176. p = s->buf_ptr;
  177. while (size > 0) {
  178. int hdr_size = 8;
  179. if (off == 0 && nb_qtables)
  180. hdr_size += 4 + 64 * nb_qtables;
  181. /* payload max in one packet */
  182. len = FFMIN(size, s->max_payload_size - hdr_size);
  183. /* set main header */
  184. bytestream_put_byte(&p, 0);
  185. bytestream_put_be24(&p, off);
  186. bytestream_put_byte(&p, type);
  187. bytestream_put_byte(&p, 255);
  188. bytestream_put_byte(&p, w);
  189. bytestream_put_byte(&p, h);
  190. if (off == 0 && nb_qtables) {
  191. /* set quantization tables header */
  192. bytestream_put_byte(&p, 0);
  193. bytestream_put_byte(&p, 0);
  194. bytestream_put_be16(&p, 64 * nb_qtables);
  195. for (i = 0; i < nb_qtables; i++)
  196. bytestream_put_buffer(&p, qtables[i], 64);
  197. }
  198. /* copy payload data */
  199. memcpy(p, buf, len);
  200. /* marker bit is last packet in frame */
  201. ff_rtp_send_data(s1, s->buf, len + hdr_size, size == len);
  202. buf += len;
  203. size -= len;
  204. off += len;
  205. p = s->buf;
  206. }
  207. }