parse.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (c) 2012 Andrew D'Addesio
  3. * Copyright (c) 2013-2014 Mozilla Corporation
  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. * @file
  23. * Opus decoder/parser shared code
  24. */
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/error.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/log.h"
  30. #include "libavutil/mem.h"
  31. #include "avcodec.h"
  32. #include "internal.h"
  33. #include "mathops.h"
  34. #include "opus.h"
  35. #include "parse.h"
  36. #include "vorbis_data.h"
  37. static const uint16_t opus_frame_duration[32] = {
  38. 480, 960, 1920, 2880,
  39. 480, 960, 1920, 2880,
  40. 480, 960, 1920, 2880,
  41. 480, 960,
  42. 480, 960,
  43. 120, 240, 480, 960,
  44. 120, 240, 480, 960,
  45. 120, 240, 480, 960,
  46. 120, 240, 480, 960,
  47. };
  48. /**
  49. * Read a 1- or 2-byte frame length
  50. */
  51. static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
  52. {
  53. int val;
  54. if (*ptr >= end)
  55. return AVERROR_INVALIDDATA;
  56. val = *(*ptr)++;
  57. if (val >= 252) {
  58. if (*ptr >= end)
  59. return AVERROR_INVALIDDATA;
  60. val += 4 * *(*ptr)++;
  61. }
  62. return val;
  63. }
  64. /**
  65. * Read a multi-byte length (used for code 3 packet padding size)
  66. */
  67. static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
  68. {
  69. int val = 0;
  70. int next;
  71. while (1) {
  72. if (*ptr >= end || val > INT_MAX - 254)
  73. return AVERROR_INVALIDDATA;
  74. next = *(*ptr)++;
  75. val += next;
  76. if (next < 255)
  77. break;
  78. else
  79. val--;
  80. }
  81. return val;
  82. }
  83. /**
  84. * Parse Opus packet info from raw packet data
  85. */
  86. int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
  87. int self_delimiting)
  88. {
  89. const uint8_t *ptr = buf;
  90. const uint8_t *end = buf + buf_size;
  91. int padding = 0;
  92. int frame_bytes, i;
  93. if (buf_size < 1)
  94. goto fail;
  95. /* TOC byte */
  96. i = *ptr++;
  97. pkt->code = (i ) & 0x3;
  98. pkt->stereo = (i >> 2) & 0x1;
  99. pkt->config = (i >> 3) & 0x1F;
  100. /* code 2 and code 3 packets have at least 1 byte after the TOC */
  101. if (pkt->code >= 2 && buf_size < 2)
  102. goto fail;
  103. switch (pkt->code) {
  104. case 0:
  105. /* 1 frame */
  106. pkt->frame_count = 1;
  107. pkt->vbr = 0;
  108. if (self_delimiting) {
  109. int len = xiph_lacing_16bit(&ptr, end);
  110. if (len < 0 || len > end - ptr)
  111. goto fail;
  112. end = ptr + len;
  113. buf_size = end - buf;
  114. }
  115. frame_bytes = end - ptr;
  116. if (frame_bytes > OPUS_MAX_FRAME_SIZE)
  117. goto fail;
  118. pkt->frame_offset[0] = ptr - buf;
  119. pkt->frame_size[0] = frame_bytes;
  120. break;
  121. case 1:
  122. /* 2 frames, equal size */
  123. pkt->frame_count = 2;
  124. pkt->vbr = 0;
  125. if (self_delimiting) {
  126. int len = xiph_lacing_16bit(&ptr, end);
  127. if (len < 0 || 2 * len > end - ptr)
  128. goto fail;
  129. end = ptr + 2 * len;
  130. buf_size = end - buf;
  131. }
  132. frame_bytes = end - ptr;
  133. if (frame_bytes & 1 || frame_bytes >> 1 > OPUS_MAX_FRAME_SIZE)
  134. goto fail;
  135. pkt->frame_offset[0] = ptr - buf;
  136. pkt->frame_size[0] = frame_bytes >> 1;
  137. pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
  138. pkt->frame_size[1] = frame_bytes >> 1;
  139. break;
  140. case 2:
  141. /* 2 frames, different sizes */
  142. pkt->frame_count = 2;
  143. pkt->vbr = 1;
  144. /* read 1st frame size */
  145. frame_bytes = xiph_lacing_16bit(&ptr, end);
  146. if (frame_bytes < 0)
  147. goto fail;
  148. if (self_delimiting) {
  149. int len = xiph_lacing_16bit(&ptr, end);
  150. if (len < 0 || len + frame_bytes > end - ptr)
  151. goto fail;
  152. end = ptr + frame_bytes + len;
  153. buf_size = end - buf;
  154. }
  155. pkt->frame_offset[0] = ptr - buf;
  156. pkt->frame_size[0] = frame_bytes;
  157. /* calculate 2nd frame size */
  158. frame_bytes = end - ptr - pkt->frame_size[0];
  159. if (frame_bytes < 0 || frame_bytes > OPUS_MAX_FRAME_SIZE)
  160. goto fail;
  161. pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
  162. pkt->frame_size[1] = frame_bytes;
  163. break;
  164. case 3:
  165. /* 1 to 48 frames, can be different sizes */
  166. i = *ptr++;
  167. pkt->frame_count = (i ) & 0x3F;
  168. padding = (i >> 6) & 0x01;
  169. pkt->vbr = (i >> 7) & 0x01;
  170. if (pkt->frame_count == 0 || pkt->frame_count > OPUS_MAX_FRAMES)
  171. goto fail;
  172. /* read padding size */
  173. if (padding) {
  174. padding = xiph_lacing_full(&ptr, end);
  175. if (padding < 0)
  176. goto fail;
  177. }
  178. /* read frame sizes */
  179. if (pkt->vbr) {
  180. /* for VBR, all frames except the final one have their size coded
  181. in the bitstream. the last frame size is implicit. */
  182. int total_bytes = 0;
  183. for (i = 0; i < pkt->frame_count - 1; i++) {
  184. frame_bytes = xiph_lacing_16bit(&ptr, end);
  185. if (frame_bytes < 0)
  186. goto fail;
  187. pkt->frame_size[i] = frame_bytes;
  188. total_bytes += frame_bytes;
  189. }
  190. if (self_delimiting) {
  191. int len = xiph_lacing_16bit(&ptr, end);
  192. if (len < 0 || len + total_bytes + padding > end - ptr)
  193. goto fail;
  194. end = ptr + total_bytes + len + padding;
  195. buf_size = end - buf;
  196. }
  197. frame_bytes = end - ptr - padding;
  198. if (total_bytes > frame_bytes)
  199. goto fail;
  200. pkt->frame_offset[0] = ptr - buf;
  201. for (i = 1; i < pkt->frame_count; i++)
  202. pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
  203. pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
  204. } else {
  205. /* for CBR, the remaining packet bytes are divided evenly between
  206. the frames */
  207. if (self_delimiting) {
  208. frame_bytes = xiph_lacing_16bit(&ptr, end);
  209. if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
  210. goto fail;
  211. end = ptr + pkt->frame_count * frame_bytes + padding;
  212. buf_size = end - buf;
  213. } else {
  214. frame_bytes = end - ptr - padding;
  215. if (frame_bytes % pkt->frame_count ||
  216. frame_bytes / pkt->frame_count > OPUS_MAX_FRAME_SIZE)
  217. goto fail;
  218. frame_bytes /= pkt->frame_count;
  219. }
  220. pkt->frame_offset[0] = ptr - buf;
  221. pkt->frame_size[0] = frame_bytes;
  222. for (i = 1; i < pkt->frame_count; i++) {
  223. pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
  224. pkt->frame_size[i] = frame_bytes;
  225. }
  226. }
  227. }
  228. pkt->packet_size = buf_size;
  229. pkt->data_size = pkt->packet_size - padding;
  230. /* total packet duration cannot be larger than 120ms */
  231. pkt->frame_duration = opus_frame_duration[pkt->config];
  232. if (pkt->frame_duration * pkt->frame_count > OPUS_MAX_PACKET_DUR)
  233. goto fail;
  234. /* set mode and bandwidth */
  235. if (pkt->config < 12) {
  236. pkt->mode = OPUS_MODE_SILK;
  237. pkt->bandwidth = pkt->config >> 2;
  238. } else if (pkt->config < 16) {
  239. pkt->mode = OPUS_MODE_HYBRID;
  240. pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
  241. } else {
  242. pkt->mode = OPUS_MODE_CELT;
  243. pkt->bandwidth = (pkt->config - 16) >> 2;
  244. /* skip medium band */
  245. if (pkt->bandwidth)
  246. pkt->bandwidth++;
  247. }
  248. return 0;
  249. fail:
  250. memset(pkt, 0, sizeof(*pkt));
  251. return AVERROR_INVALIDDATA;
  252. }
  253. static int channel_reorder_vorbis(int nb_channels, int channel_idx)
  254. {
  255. return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
  256. }
  257. static int channel_reorder_unknown(int nb_channels, int channel_idx)
  258. {
  259. return channel_idx;
  260. }
  261. av_cold int ff_opus_parse_extradata(AVCodecContext *avctx,
  262. OpusParseContext *s)
  263. {
  264. static const uint8_t default_channel_map[2] = { 0, 1 };
  265. int (*channel_reorder)(int, int) = channel_reorder_unknown;
  266. int channels = avctx->ch_layout.nb_channels;
  267. const uint8_t *extradata, *channel_map;
  268. int extradata_size;
  269. int version, map_type, streams, stereo_streams, i, j, ret;
  270. AVChannelLayout layout = { 0 };
  271. if (!avctx->extradata) {
  272. if (channels > 2) {
  273. av_log(avctx, AV_LOG_ERROR,
  274. "Multichannel configuration without extradata.\n");
  275. return AVERROR(EINVAL);
  276. }
  277. extradata = opus_default_extradata;
  278. extradata_size = sizeof(opus_default_extradata);
  279. } else {
  280. extradata = avctx->extradata;
  281. extradata_size = avctx->extradata_size;
  282. }
  283. if (extradata_size < 19) {
  284. av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
  285. extradata_size);
  286. return AVERROR_INVALIDDATA;
  287. }
  288. version = extradata[8];
  289. if (version > 15) {
  290. avpriv_request_sample(avctx, "Extradata version %d", version);
  291. return AVERROR_PATCHWELCOME;
  292. }
  293. avctx->delay = AV_RL16(extradata + 10);
  294. if (avctx->internal)
  295. avctx->internal->skip_samples = avctx->delay;
  296. channels = avctx->extradata ? extradata[9] : (channels == 1) ? 1 : 2;
  297. if (!channels) {
  298. av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
  299. return AVERROR_INVALIDDATA;
  300. }
  301. s->gain_i = AV_RL16(extradata + 16);
  302. map_type = extradata[18];
  303. if (!map_type) {
  304. if (channels > 2) {
  305. av_log(avctx, AV_LOG_ERROR,
  306. "Channel mapping 0 is only specified for up to 2 channels\n");
  307. ret = AVERROR_INVALIDDATA;
  308. goto fail;
  309. }
  310. layout = (channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
  311. (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
  312. streams = 1;
  313. stereo_streams = channels - 1;
  314. channel_map = default_channel_map;
  315. } else if (map_type == 1 || map_type == 2 || map_type == 255) {
  316. if (extradata_size < 21 + channels) {
  317. av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
  318. extradata_size);
  319. ret = AVERROR_INVALIDDATA;
  320. goto fail;
  321. }
  322. streams = extradata[19];
  323. stereo_streams = extradata[20];
  324. if (!streams || stereo_streams > streams ||
  325. streams + stereo_streams > 255) {
  326. av_log(avctx, AV_LOG_ERROR,
  327. "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
  328. ret = AVERROR_INVALIDDATA;
  329. goto fail;
  330. }
  331. if (map_type == 1) {
  332. if (channels > 8) {
  333. av_log(avctx, AV_LOG_ERROR,
  334. "Channel mapping 1 is only specified for up to 8 channels\n");
  335. ret = AVERROR_INVALIDDATA;
  336. goto fail;
  337. }
  338. av_channel_layout_copy(&layout, &ff_vorbis_ch_layouts[channels - 1]);
  339. channel_reorder = channel_reorder_vorbis;
  340. } else if (map_type == 2) {
  341. int ambisonic_order = ff_sqrt(channels) - 1;
  342. if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
  343. channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
  344. av_log(avctx, AV_LOG_ERROR,
  345. "Channel mapping 2 is only specified for channel counts"
  346. " which can be written as (n + 1)^2 or (n + 1)^2 + 2"
  347. " for nonnegative integer n\n");
  348. ret = AVERROR_INVALIDDATA;
  349. goto fail;
  350. }
  351. if (channels > 227) {
  352. av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
  353. ret = AVERROR_INVALIDDATA;
  354. goto fail;
  355. }
  356. layout.order = AV_CHANNEL_ORDER_AMBISONIC;
  357. layout.nb_channels = channels;
  358. if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)))
  359. layout.u.mask = AV_CH_LAYOUT_STEREO;
  360. } else {
  361. layout.order = AV_CHANNEL_ORDER_UNSPEC;
  362. layout.nb_channels = channels;
  363. }
  364. channel_map = extradata + 21;
  365. } else {
  366. avpriv_request_sample(avctx, "Mapping type %d", map_type);
  367. return AVERROR_PATCHWELCOME;
  368. }
  369. s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps));
  370. if (!s->channel_maps) {
  371. ret = AVERROR(ENOMEM);
  372. goto fail;
  373. }
  374. for (i = 0; i < channels; i++) {
  375. ChannelMap *map = &s->channel_maps[i];
  376. uint8_t idx = channel_map[channel_reorder(channels, i)];
  377. if (idx == 255) {
  378. map->silence = 1;
  379. continue;
  380. } else if (idx >= streams + stereo_streams) {
  381. av_log(avctx, AV_LOG_ERROR,
  382. "Invalid channel map for output channel %d: %d\n", i, idx);
  383. av_freep(&s->channel_maps);
  384. ret = AVERROR_INVALIDDATA;
  385. goto fail;
  386. }
  387. /* check that we did not see this index yet */
  388. map->copy = 0;
  389. for (j = 0; j < i; j++)
  390. if (channel_map[channel_reorder(channels, j)] == idx) {
  391. map->copy = 1;
  392. map->copy_idx = j;
  393. break;
  394. }
  395. if (idx < 2 * stereo_streams) {
  396. map->stream_idx = idx / 2;
  397. map->channel_idx = idx & 1;
  398. } else {
  399. map->stream_idx = idx - stereo_streams;
  400. map->channel_idx = 0;
  401. }
  402. }
  403. ret = av_channel_layout_copy(&avctx->ch_layout, &layout);
  404. if (ret < 0)
  405. goto fail;
  406. s->nb_streams = streams;
  407. s->nb_stereo_streams = stereo_streams;
  408. return 0;
  409. fail:
  410. av_channel_layout_uninit(&layout);
  411. return ret;
  412. }