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