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.

220 lines
7.5KB

  1. /*
  2. * RTP Depacketization of QCELP/PureVoice, RFC 2658
  3. * Copyright (c) 2010 Martin Storsjo
  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 "rtpdec_formats.h"
  22. static const uint8_t frame_sizes[] = {
  23. 1, 4, 8, 17, 35
  24. };
  25. typedef struct InterleavePacket {
  26. int pos;
  27. int size;
  28. /* The largest frame is 35 bytes, only 10 frames are allowed per
  29. * packet, and we return the first one immediately, so allocate
  30. * space for 9 frames */
  31. uint8_t data[35*9];
  32. } InterleavePacket;
  33. struct PayloadContext {
  34. int interleave_size;
  35. int interleave_index;
  36. InterleavePacket group[6];
  37. int group_finished;
  38. /* The maximum packet size, 10 frames of 35 bytes each, and one
  39. * packet header byte. */
  40. uint8_t next_data[1 + 35*10];
  41. int next_size;
  42. uint32_t next_timestamp;
  43. };
  44. static int return_stored_frame(AVFormatContext *ctx, PayloadContext *data,
  45. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  46. const uint8_t *buf, int len);
  47. static int store_packet(AVFormatContext *ctx, PayloadContext *data,
  48. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  49. const uint8_t *buf, int len)
  50. {
  51. int interleave_size, interleave_index;
  52. int frame_size, ret;
  53. InterleavePacket* ip;
  54. if (len < 2)
  55. return AVERROR_INVALIDDATA;
  56. interleave_size = buf[0] >> 3 & 7;
  57. interleave_index = buf[0] & 7;
  58. if (interleave_size > 5) {
  59. av_log(ctx, AV_LOG_ERROR, "Invalid interleave size %d\n",
  60. interleave_size);
  61. return AVERROR_INVALIDDATA;
  62. }
  63. if (interleave_index > interleave_size) {
  64. av_log(ctx, AV_LOG_ERROR, "Invalid interleave index %d/%d\n",
  65. interleave_index, interleave_size);
  66. return AVERROR_INVALIDDATA;
  67. }
  68. if (interleave_size != data->interleave_size) {
  69. int i;
  70. /* First packet, or changed interleave size */
  71. data->interleave_size = interleave_size;
  72. data->interleave_index = 0;
  73. for (i = 0; i < 6; i++)
  74. data->group[i].size = 0;
  75. }
  76. if (interleave_index < data->interleave_index) {
  77. /* Wrapped around - missed the last packet of the previous group. */
  78. if (data->group_finished) {
  79. /* No more data in the packets in this interleaving group, just
  80. * start processing the next one */
  81. data->interleave_index = 0;
  82. } else {
  83. /* Stash away the current packet, emit everything we have of the
  84. * previous group. */
  85. for (; data->interleave_index <= interleave_size;
  86. data->interleave_index++)
  87. data->group[data->interleave_index].size = 0;
  88. if (len > sizeof(data->next_data))
  89. return AVERROR_INVALIDDATA;
  90. memcpy(data->next_data, buf, len);
  91. data->next_size = len;
  92. data->next_timestamp = *timestamp;
  93. *timestamp = RTP_NOTS_VALUE;
  94. data->interleave_index = 0;
  95. return return_stored_frame(ctx, data, st, pkt, timestamp, buf, len);
  96. }
  97. }
  98. if (interleave_index > data->interleave_index) {
  99. /* We missed a packet */
  100. for (; data->interleave_index < interleave_index;
  101. data->interleave_index++)
  102. data->group[data->interleave_index].size = 0;
  103. }
  104. data->interleave_index = interleave_index;
  105. if (buf[1] >= FF_ARRAY_ELEMS(frame_sizes))
  106. return AVERROR_INVALIDDATA;
  107. frame_size = frame_sizes[buf[1]];
  108. if (1 + frame_size > len)
  109. return AVERROR_INVALIDDATA;
  110. if (len - 1 - frame_size > sizeof(data->group[0].data))
  111. return AVERROR_INVALIDDATA;
  112. if ((ret = av_new_packet(pkt, frame_size)) < 0)
  113. return ret;
  114. memcpy(pkt->data, &buf[1], frame_size);
  115. pkt->stream_index = st->index;
  116. ip = &data->group[data->interleave_index];
  117. ip->size = len - 1 - frame_size;
  118. ip->pos = 0;
  119. memcpy(ip->data, &buf[1 + frame_size], ip->size);
  120. /* Each packet must contain the same number of frames according to the
  121. * RFC. If there's no data left in this packet, there shouldn't be any
  122. * in any of the other frames in the interleaving group either. */
  123. data->group_finished = ip->size == 0;
  124. if (interleave_index == interleave_size) {
  125. data->interleave_index = 0;
  126. return !data->group_finished;
  127. } else {
  128. data->interleave_index++;
  129. return 0;
  130. }
  131. }
  132. static int return_stored_frame(AVFormatContext *ctx, PayloadContext *data,
  133. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  134. const uint8_t *buf, int len)
  135. {
  136. InterleavePacket* ip = &data->group[data->interleave_index];
  137. int frame_size, ret;
  138. if (data->group_finished && data->interleave_index == 0) {
  139. *timestamp = data->next_timestamp;
  140. ret = store_packet(ctx, data, st, pkt, timestamp, data->next_data,
  141. data->next_size);
  142. data->next_size = 0;
  143. return ret;
  144. }
  145. if (ip->size == 0) {
  146. /* No stored data for this interleave block, output an empty packet */
  147. if ((ret = av_new_packet(pkt, 1)) < 0)
  148. return ret;
  149. pkt->data[0] = 0; // Blank - could also be 14, Erasure
  150. } else {
  151. if (ip->pos >= ip->size)
  152. return AVERROR_INVALIDDATA;
  153. if (ip->data[ip->pos] >= FF_ARRAY_ELEMS(frame_sizes))
  154. return AVERROR_INVALIDDATA;
  155. frame_size = frame_sizes[ip->data[ip->pos]];
  156. if (ip->pos + frame_size > ip->size)
  157. return AVERROR_INVALIDDATA;
  158. if ((ret = av_new_packet(pkt, frame_size)) < 0)
  159. return ret;
  160. memcpy(pkt->data, &ip->data[ip->pos], frame_size);
  161. ip->pos += frame_size;
  162. data->group_finished = ip->pos >= ip->size;
  163. }
  164. pkt->stream_index = st->index;
  165. if (data->interleave_index == data->interleave_size) {
  166. data->interleave_index = 0;
  167. if (!data->group_finished)
  168. return 1;
  169. else
  170. return data->next_size > 0;
  171. } else {
  172. data->interleave_index++;
  173. return 1;
  174. }
  175. }
  176. static int qcelp_parse_packet(AVFormatContext *ctx, PayloadContext *data,
  177. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  178. const uint8_t *buf, int len, uint16_t seq,
  179. int flags)
  180. {
  181. if (buf)
  182. return store_packet(ctx, data, st, pkt, timestamp, buf, len);
  183. else
  184. return return_stored_frame(ctx, data, st, pkt, timestamp, buf, len);
  185. }
  186. RTPDynamicProtocolHandler ff_qcelp_dynamic_handler = {
  187. .enc_name = "x-Purevoice",
  188. .codec_type = AVMEDIA_TYPE_AUDIO,
  189. .codec_id = AV_CODEC_ID_QCELP,
  190. .priv_data_size = sizeof(PayloadContext),
  191. .static_payload_id = 12,
  192. .parse_packet = qcelp_parse_packet,
  193. };