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.

2162 lines
65KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "avcodec.h"
  36. #include "dsputil.h"
  37. #include "libavutil/opt.h"
  38. #include "imgconvert.h"
  39. #include "thread.h"
  40. #include "audioconvert.h"
  41. #include "internal.h"
  42. #include "bytestream.h"
  43. #include <stdlib.h>
  44. #include <stdarg.h>
  45. #include <limits.h>
  46. #include <float.h>
  47. static int volatile entangled_thread_counter = 0;
  48. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  49. static void *codec_mutex;
  50. static void *avformat_mutex;
  51. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  52. {
  53. if (min_size < *size)
  54. return ptr;
  55. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  56. ptr = av_realloc(ptr, min_size);
  57. /* we could set this to the unmodified min_size but this is safer
  58. * if the user lost the ptr and uses NULL now
  59. */
  60. if (!ptr)
  61. min_size = 0;
  62. *size = min_size;
  63. return ptr;
  64. }
  65. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  66. {
  67. void **p = ptr;
  68. if (min_size < *size)
  69. return;
  70. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  71. av_free(*p);
  72. *p = av_malloc(min_size);
  73. if (!*p)
  74. min_size = 0;
  75. *size = min_size;
  76. }
  77. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  78. {
  79. void **p = ptr;
  80. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  81. av_freep(p);
  82. *size = 0;
  83. return;
  84. }
  85. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  86. if (*size)
  87. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  88. }
  89. /* encoder management */
  90. static AVCodec *first_avcodec = NULL;
  91. AVCodec *av_codec_next(const AVCodec *c)
  92. {
  93. if (c)
  94. return c->next;
  95. else
  96. return first_avcodec;
  97. }
  98. static void avcodec_init(void)
  99. {
  100. static int initialized = 0;
  101. if (initialized != 0)
  102. return;
  103. initialized = 1;
  104. ff_dsputil_static_init();
  105. }
  106. int av_codec_is_encoder(const AVCodec *codec)
  107. {
  108. return codec && (codec->encode_sub || codec->encode2);
  109. }
  110. int av_codec_is_decoder(const AVCodec *codec)
  111. {
  112. return codec && codec->decode;
  113. }
  114. void avcodec_register(AVCodec *codec)
  115. {
  116. AVCodec **p;
  117. avcodec_init();
  118. p = &first_avcodec;
  119. while (*p != NULL)
  120. p = &(*p)->next;
  121. *p = codec;
  122. codec->next = NULL;
  123. if (codec->init_static_data)
  124. codec->init_static_data(codec);
  125. }
  126. unsigned avcodec_get_edge_width(void)
  127. {
  128. return EDGE_WIDTH;
  129. }
  130. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  131. {
  132. s->coded_width = width;
  133. s->coded_height = height;
  134. s->width = width;
  135. s->height = height;
  136. }
  137. #define INTERNAL_BUFFER_SIZE (32 + 1)
  138. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  139. int linesize_align[AV_NUM_DATA_POINTERS])
  140. {
  141. int i;
  142. int w_align = 1;
  143. int h_align = 1;
  144. switch (s->pix_fmt) {
  145. case PIX_FMT_YUV420P:
  146. case PIX_FMT_YUYV422:
  147. case PIX_FMT_UYVY422:
  148. case PIX_FMT_YUV422P:
  149. case PIX_FMT_YUV440P:
  150. case PIX_FMT_YUV444P:
  151. case PIX_FMT_GBRP:
  152. case PIX_FMT_GRAY8:
  153. case PIX_FMT_GRAY16BE:
  154. case PIX_FMT_GRAY16LE:
  155. case PIX_FMT_YUVJ420P:
  156. case PIX_FMT_YUVJ422P:
  157. case PIX_FMT_YUVJ440P:
  158. case PIX_FMT_YUVJ444P:
  159. case PIX_FMT_YUVA420P:
  160. case PIX_FMT_YUV420P9LE:
  161. case PIX_FMT_YUV420P9BE:
  162. case PIX_FMT_YUV420P10LE:
  163. case PIX_FMT_YUV420P10BE:
  164. case PIX_FMT_YUV422P9LE:
  165. case PIX_FMT_YUV422P9BE:
  166. case PIX_FMT_YUV422P10LE:
  167. case PIX_FMT_YUV422P10BE:
  168. case PIX_FMT_YUV444P9LE:
  169. case PIX_FMT_YUV444P9BE:
  170. case PIX_FMT_YUV444P10LE:
  171. case PIX_FMT_YUV444P10BE:
  172. case PIX_FMT_GBRP9LE:
  173. case PIX_FMT_GBRP9BE:
  174. case PIX_FMT_GBRP10LE:
  175. case PIX_FMT_GBRP10BE:
  176. w_align = 16; //FIXME assume 16 pixel per macroblock
  177. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  178. break;
  179. case PIX_FMT_YUV411P:
  180. case PIX_FMT_UYYVYY411:
  181. w_align = 32;
  182. h_align = 8;
  183. break;
  184. case PIX_FMT_YUV410P:
  185. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  186. w_align = 64;
  187. h_align = 64;
  188. }
  189. case PIX_FMT_RGB555:
  190. if (s->codec_id == AV_CODEC_ID_RPZA) {
  191. w_align = 4;
  192. h_align = 4;
  193. }
  194. case PIX_FMT_PAL8:
  195. case PIX_FMT_BGR8:
  196. case PIX_FMT_RGB8:
  197. if (s->codec_id == AV_CODEC_ID_SMC) {
  198. w_align = 4;
  199. h_align = 4;
  200. }
  201. break;
  202. case PIX_FMT_BGR24:
  203. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  204. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  205. w_align = 4;
  206. h_align = 4;
  207. }
  208. break;
  209. default:
  210. w_align = 1;
  211. h_align = 1;
  212. break;
  213. }
  214. *width = FFALIGN(*width, w_align);
  215. *height = FFALIGN(*height, h_align);
  216. if (s->codec_id == AV_CODEC_ID_H264)
  217. // some of the optimized chroma MC reads one line too much
  218. *height += 2;
  219. for (i = 0; i < 4; i++)
  220. linesize_align[i] = STRIDE_ALIGN;
  221. }
  222. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  223. {
  224. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  225. int linesize_align[AV_NUM_DATA_POINTERS];
  226. int align;
  227. avcodec_align_dimensions2(s, width, height, linesize_align);
  228. align = FFMAX(linesize_align[0], linesize_align[3]);
  229. linesize_align[1] <<= chroma_shift;
  230. linesize_align[2] <<= chroma_shift;
  231. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  232. *width = FFALIGN(*width, align);
  233. }
  234. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  235. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  236. int buf_size, int align)
  237. {
  238. int ch, planar, needed_size, ret = 0;
  239. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  240. frame->nb_samples, sample_fmt,
  241. align);
  242. if (buf_size < needed_size)
  243. return AVERROR(EINVAL);
  244. planar = av_sample_fmt_is_planar(sample_fmt);
  245. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  246. if (!(frame->extended_data = av_mallocz(nb_channels *
  247. sizeof(*frame->extended_data))))
  248. return AVERROR(ENOMEM);
  249. } else {
  250. frame->extended_data = frame->data;
  251. }
  252. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  253. buf, nb_channels, frame->nb_samples,
  254. sample_fmt, align)) < 0) {
  255. if (frame->extended_data != frame->data)
  256. av_free(frame->extended_data);
  257. return ret;
  258. }
  259. if (frame->extended_data != frame->data) {
  260. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  261. frame->data[ch] = frame->extended_data[ch];
  262. }
  263. return ret;
  264. }
  265. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  266. {
  267. AVCodecInternal *avci = avctx->internal;
  268. InternalBuffer *buf;
  269. int buf_size, ret;
  270. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  271. frame->nb_samples, avctx->sample_fmt,
  272. 0);
  273. if (buf_size < 0)
  274. return AVERROR(EINVAL);
  275. /* allocate InternalBuffer if needed */
  276. if (!avci->buffer) {
  277. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  278. if (!avci->buffer)
  279. return AVERROR(ENOMEM);
  280. }
  281. buf = avci->buffer;
  282. /* if there is a previously-used internal buffer, check its size and
  283. * channel count to see if we can reuse it */
  284. if (buf->extended_data) {
  285. /* if current buffer is too small, free it */
  286. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  287. av_free(buf->extended_data[0]);
  288. if (buf->extended_data != buf->data)
  289. av_free(&buf->extended_data);
  290. buf->extended_data = NULL;
  291. buf->data[0] = NULL;
  292. }
  293. /* if number of channels has changed, reset and/or free extended data
  294. * pointers but leave data buffer in buf->data[0] for reuse */
  295. if (buf->nb_channels != avctx->channels) {
  296. if (buf->extended_data != buf->data)
  297. av_free(buf->extended_data);
  298. buf->extended_data = NULL;
  299. }
  300. }
  301. /* if there is no previous buffer or the previous buffer cannot be used
  302. * as-is, allocate a new buffer and/or rearrange the channel pointers */
  303. if (!buf->extended_data) {
  304. if (!buf->data[0]) {
  305. if (!(buf->data[0] = av_mallocz(buf_size)))
  306. return AVERROR(ENOMEM);
  307. buf->audio_data_size = buf_size;
  308. }
  309. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  310. avctx->sample_fmt, buf->data[0],
  311. buf->audio_data_size, 0)))
  312. return ret;
  313. if (frame->extended_data == frame->data)
  314. buf->extended_data = buf->data;
  315. else
  316. buf->extended_data = frame->extended_data;
  317. memcpy(buf->data, frame->data, sizeof(frame->data));
  318. buf->linesize[0] = frame->linesize[0];
  319. buf->nb_channels = avctx->channels;
  320. } else {
  321. /* copy InternalBuffer info to the AVFrame */
  322. frame->extended_data = buf->extended_data;
  323. frame->linesize[0] = buf->linesize[0];
  324. memcpy(frame->data, buf->data, sizeof(frame->data));
  325. }
  326. frame->type = FF_BUFFER_TYPE_INTERNAL;
  327. if (avctx->pkt)
  328. frame->pkt_pts = avctx->pkt->pts;
  329. else
  330. frame->pkt_pts = AV_NOPTS_VALUE;
  331. frame->reordered_opaque = avctx->reordered_opaque;
  332. frame->sample_rate = avctx->sample_rate;
  333. frame->format = avctx->sample_fmt;
  334. frame->channel_layout = avctx->channel_layout;
  335. if (avctx->debug & FF_DEBUG_BUFFERS)
  336. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  337. "internal audio buffer used\n", frame);
  338. return 0;
  339. }
  340. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  341. {
  342. int i;
  343. int w = s->width;
  344. int h = s->height;
  345. InternalBuffer *buf;
  346. AVCodecInternal *avci = s->internal;
  347. if (pic->data[0] != NULL) {
  348. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  349. return -1;
  350. }
  351. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  352. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  353. return -1;
  354. }
  355. if (av_image_check_size(w, h, 0, s))
  356. return -1;
  357. if (!avci->buffer) {
  358. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  359. sizeof(InternalBuffer));
  360. }
  361. buf = &avci->buffer[avci->buffer_count];
  362. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  363. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  364. av_freep(&buf->base[i]);
  365. buf->data[i] = NULL;
  366. }
  367. }
  368. if (!buf->base[0]) {
  369. int h_chroma_shift, v_chroma_shift;
  370. int size[4] = { 0 };
  371. int tmpsize;
  372. int unaligned;
  373. AVPicture picture;
  374. int stride_align[AV_NUM_DATA_POINTERS];
  375. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1 + 1;
  376. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  377. avcodec_align_dimensions2(s, &w, &h, stride_align);
  378. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  379. w += EDGE_WIDTH * 2;
  380. h += EDGE_WIDTH * 2;
  381. }
  382. do {
  383. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  384. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  385. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  386. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  387. w += w & ~(w - 1);
  388. unaligned = 0;
  389. for (i = 0; i < 4; i++)
  390. unaligned |= picture.linesize[i] % stride_align[i];
  391. } while (unaligned);
  392. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  393. if (tmpsize < 0)
  394. return -1;
  395. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  396. size[i] = picture.data[i + 1] - picture.data[i];
  397. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  398. memset(buf->base, 0, sizeof(buf->base));
  399. memset(buf->data, 0, sizeof(buf->data));
  400. for (i = 0; i < 4 && size[i]; i++) {
  401. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  402. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  403. buf->linesize[i] = picture.linesize[i];
  404. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  405. if (buf->base[i] == NULL)
  406. return -1;
  407. memset(buf->base[i], 128, size[i]);
  408. // no edge if EDGE EMU or not planar YUV
  409. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  410. buf->data[i] = buf->base[i];
  411. else
  412. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  413. }
  414. for (; i < AV_NUM_DATA_POINTERS; i++) {
  415. buf->base[i] = buf->data[i] = NULL;
  416. buf->linesize[i] = 0;
  417. }
  418. if (size[1] && !size[2])
  419. ff_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  420. buf->width = s->width;
  421. buf->height = s->height;
  422. buf->pix_fmt = s->pix_fmt;
  423. }
  424. pic->type = FF_BUFFER_TYPE_INTERNAL;
  425. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  426. pic->base[i] = buf->base[i];
  427. pic->data[i] = buf->data[i];
  428. pic->linesize[i] = buf->linesize[i];
  429. }
  430. pic->extended_data = pic->data;
  431. avci->buffer_count++;
  432. pic->width = buf->width;
  433. pic->height = buf->height;
  434. pic->format = buf->pix_fmt;
  435. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  436. if (s->pkt)
  437. pic->pkt_pts = s->pkt->pts;
  438. else
  439. pic->pkt_pts = AV_NOPTS_VALUE;
  440. pic->reordered_opaque = s->reordered_opaque;
  441. if (s->debug & FF_DEBUG_BUFFERS)
  442. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  443. "buffers used\n", pic, avci->buffer_count);
  444. return 0;
  445. }
  446. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  447. {
  448. switch (avctx->codec_type) {
  449. case AVMEDIA_TYPE_VIDEO:
  450. return video_get_buffer(avctx, frame);
  451. case AVMEDIA_TYPE_AUDIO:
  452. return audio_get_buffer(avctx, frame);
  453. default:
  454. return -1;
  455. }
  456. }
  457. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  458. {
  459. int i;
  460. InternalBuffer *buf, *last;
  461. AVCodecInternal *avci = s->internal;
  462. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  463. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  464. assert(avci->buffer_count);
  465. if (avci->buffer) {
  466. buf = NULL; /* avoids warning */
  467. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  468. buf = &avci->buffer[i];
  469. if (buf->data[0] == pic->data[0])
  470. break;
  471. }
  472. assert(i < avci->buffer_count);
  473. avci->buffer_count--;
  474. last = &avci->buffer[avci->buffer_count];
  475. if (buf != last)
  476. FFSWAP(InternalBuffer, *buf, *last);
  477. }
  478. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  479. pic->data[i] = NULL;
  480. // pic->base[i]=NULL;
  481. //printf("R%X\n", pic->opaque);
  482. if (s->debug & FF_DEBUG_BUFFERS)
  483. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  484. "buffers used\n", pic, avci->buffer_count);
  485. }
  486. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  487. {
  488. AVFrame temp_pic;
  489. int i;
  490. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  491. /* If no picture return a new buffer */
  492. if (pic->data[0] == NULL) {
  493. /* We will copy from buffer, so must be readable */
  494. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  495. return s->get_buffer(s, pic);
  496. }
  497. assert(s->pix_fmt == pic->format);
  498. /* If internal buffer type return the same buffer */
  499. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  500. if (s->pkt)
  501. pic->pkt_pts = s->pkt->pts;
  502. else
  503. pic->pkt_pts = AV_NOPTS_VALUE;
  504. pic->reordered_opaque = s->reordered_opaque;
  505. return 0;
  506. }
  507. /*
  508. * Not internal type and reget_buffer not overridden, emulate cr buffer
  509. */
  510. temp_pic = *pic;
  511. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  512. pic->data[i] = pic->base[i] = NULL;
  513. pic->opaque = NULL;
  514. /* Allocate new frame */
  515. if (s->get_buffer(s, pic))
  516. return -1;
  517. /* Copy image data from old buffer to new buffer */
  518. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  519. s->height);
  520. s->release_buffer(s, &temp_pic); // Release old frame
  521. return 0;
  522. }
  523. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  524. {
  525. int i;
  526. for (i = 0; i < count; i++) {
  527. int r = func(c, (char *)arg + i * size);
  528. if (ret)
  529. ret[i] = r;
  530. }
  531. return 0;
  532. }
  533. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  534. {
  535. int i;
  536. for (i = 0; i < count; i++) {
  537. int r = func(c, arg, i, 0);
  538. if (ret)
  539. ret[i] = r;
  540. }
  541. return 0;
  542. }
  543. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt)
  544. {
  545. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  546. ++fmt;
  547. return fmt[0];
  548. }
  549. void avcodec_get_frame_defaults(AVFrame *frame)
  550. {
  551. if (frame->extended_data != frame->data)
  552. av_freep(&frame->extended_data);
  553. memset(frame, 0, sizeof(AVFrame));
  554. frame->pts = AV_NOPTS_VALUE;
  555. frame->key_frame = 1;
  556. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  557. frame->format = -1; /* unknown */
  558. frame->extended_data = frame->data;
  559. }
  560. AVFrame *avcodec_alloc_frame(void)
  561. {
  562. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  563. if (frame == NULL)
  564. return NULL;
  565. avcodec_get_frame_defaults(frame);
  566. return frame;
  567. }
  568. void avcodec_free_frame(AVFrame **frame)
  569. {
  570. AVFrame *f;
  571. if (!frame || !*frame)
  572. return;
  573. f = *frame;
  574. if (f->extended_data != f->data)
  575. av_freep(&f->extended_data);
  576. av_freep(frame);
  577. }
  578. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  579. {
  580. int ret = 0;
  581. AVDictionary *tmp = NULL;
  582. if (avcodec_is_open(avctx))
  583. return 0;
  584. if ((!codec && !avctx->codec)) {
  585. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  586. return AVERROR(EINVAL);
  587. }
  588. if ((codec && avctx->codec && codec != avctx->codec)) {
  589. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  590. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  591. return AVERROR(EINVAL);
  592. }
  593. if (!codec)
  594. codec = avctx->codec;
  595. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  596. return AVERROR(EINVAL);
  597. if (options)
  598. av_dict_copy(&tmp, *options, 0);
  599. /* If there is a user-supplied mutex locking routine, call it. */
  600. if (ff_lockmgr_cb) {
  601. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  602. return -1;
  603. }
  604. entangled_thread_counter++;
  605. if (entangled_thread_counter != 1) {
  606. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  607. ret = -1;
  608. goto end;
  609. }
  610. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  611. if (!avctx->internal) {
  612. ret = AVERROR(ENOMEM);
  613. goto end;
  614. }
  615. if (codec->priv_data_size > 0) {
  616. if (!avctx->priv_data) {
  617. avctx->priv_data = av_mallocz(codec->priv_data_size);
  618. if (!avctx->priv_data) {
  619. ret = AVERROR(ENOMEM);
  620. goto end;
  621. }
  622. if (codec->priv_class) {
  623. *(const AVClass **)avctx->priv_data = codec->priv_class;
  624. av_opt_set_defaults(avctx->priv_data);
  625. }
  626. }
  627. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  628. goto free_and_end;
  629. } else {
  630. avctx->priv_data = NULL;
  631. }
  632. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  633. goto free_and_end;
  634. if (avctx->coded_width && avctx->coded_height)
  635. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  636. else if (avctx->width && avctx->height)
  637. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  638. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  639. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  640. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  641. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  642. avcodec_set_dimensions(avctx, 0, 0);
  643. }
  644. /* if the decoder init function was already called previously,
  645. * free the already allocated subtitle_header before overwriting it */
  646. if (av_codec_is_decoder(codec))
  647. av_freep(&avctx->subtitle_header);
  648. #define SANE_NB_CHANNELS 128U
  649. if (avctx->channels > SANE_NB_CHANNELS) {
  650. ret = AVERROR(EINVAL);
  651. goto free_and_end;
  652. }
  653. avctx->codec = codec;
  654. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  655. avctx->codec_id == AV_CODEC_ID_NONE) {
  656. avctx->codec_type = codec->type;
  657. avctx->codec_id = codec->id;
  658. }
  659. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  660. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  661. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  662. ret = AVERROR(EINVAL);
  663. goto free_and_end;
  664. }
  665. avctx->frame_number = 0;
  666. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  667. (!avctx->time_base.num || !avctx->time_base.den)) {
  668. avctx->time_base.num = 1;
  669. avctx->time_base.den = avctx->sample_rate;
  670. }
  671. if (HAVE_THREADS && !avctx->thread_opaque) {
  672. ret = ff_thread_init(avctx);
  673. if (ret < 0) {
  674. goto free_and_end;
  675. }
  676. }
  677. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  678. avctx->thread_count = 1;
  679. if (av_codec_is_encoder(avctx->codec)) {
  680. int i;
  681. if (avctx->codec->sample_fmts) {
  682. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  683. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  684. break;
  685. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  686. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  687. ret = AVERROR(EINVAL);
  688. goto free_and_end;
  689. }
  690. }
  691. if (avctx->codec->pix_fmts) {
  692. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  693. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  694. break;
  695. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
  696. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  697. ret = AVERROR(EINVAL);
  698. goto free_and_end;
  699. }
  700. }
  701. if (avctx->codec->supported_samplerates) {
  702. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  703. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  704. break;
  705. if (avctx->codec->supported_samplerates[i] == 0) {
  706. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  707. ret = AVERROR(EINVAL);
  708. goto free_and_end;
  709. }
  710. }
  711. if (avctx->codec->channel_layouts) {
  712. if (!avctx->channel_layout) {
  713. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  714. } else {
  715. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  716. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  717. break;
  718. if (avctx->codec->channel_layouts[i] == 0) {
  719. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  720. ret = AVERROR(EINVAL);
  721. goto free_and_end;
  722. }
  723. }
  724. }
  725. if (avctx->channel_layout && avctx->channels) {
  726. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  727. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  728. ret = AVERROR(EINVAL);
  729. goto free_and_end;
  730. }
  731. } else if (avctx->channel_layout) {
  732. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  733. }
  734. }
  735. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  736. ret = avctx->codec->init(avctx);
  737. if (ret < 0) {
  738. goto free_and_end;
  739. }
  740. }
  741. if (av_codec_is_decoder(avctx->codec)) {
  742. /* validate channel layout from the decoder */
  743. if (avctx->channel_layout &&
  744. av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  745. av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
  746. avctx->channel_layout = 0;
  747. }
  748. }
  749. end:
  750. entangled_thread_counter--;
  751. /* Release any user-supplied mutex. */
  752. if (ff_lockmgr_cb) {
  753. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  754. }
  755. if (options) {
  756. av_dict_free(options);
  757. *options = tmp;
  758. }
  759. return ret;
  760. free_and_end:
  761. av_dict_free(&tmp);
  762. av_freep(&avctx->priv_data);
  763. av_freep(&avctx->internal);
  764. avctx->codec = NULL;
  765. goto end;
  766. }
  767. int ff_alloc_packet(AVPacket *avpkt, int size)
  768. {
  769. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  770. return AVERROR(EINVAL);
  771. if (avpkt->data) {
  772. void *destruct = avpkt->destruct;
  773. if (avpkt->size < size)
  774. return AVERROR(EINVAL);
  775. av_init_packet(avpkt);
  776. avpkt->destruct = destruct;
  777. avpkt->size = size;
  778. return 0;
  779. } else {
  780. return av_new_packet(avpkt, size);
  781. }
  782. }
  783. /**
  784. * Pad last frame with silence.
  785. */
  786. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  787. {
  788. AVFrame *frame = NULL;
  789. uint8_t *buf = NULL;
  790. int ret;
  791. if (!(frame = avcodec_alloc_frame()))
  792. return AVERROR(ENOMEM);
  793. *frame = *src;
  794. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  795. s->frame_size, s->sample_fmt, 0)) < 0)
  796. goto fail;
  797. if (!(buf = av_malloc(ret))) {
  798. ret = AVERROR(ENOMEM);
  799. goto fail;
  800. }
  801. frame->nb_samples = s->frame_size;
  802. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  803. buf, ret, 0)) < 0)
  804. goto fail;
  805. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  806. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  807. goto fail;
  808. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  809. frame->nb_samples - src->nb_samples,
  810. s->channels, s->sample_fmt)) < 0)
  811. goto fail;
  812. *dst = frame;
  813. return 0;
  814. fail:
  815. if (frame->extended_data != frame->data)
  816. av_freep(&frame->extended_data);
  817. av_freep(&buf);
  818. av_freep(&frame);
  819. return ret;
  820. }
  821. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  822. AVPacket *avpkt,
  823. const AVFrame *frame,
  824. int *got_packet_ptr)
  825. {
  826. AVFrame tmp;
  827. AVFrame *padded_frame = NULL;
  828. int ret;
  829. int user_packet = !!avpkt->data;
  830. *got_packet_ptr = 0;
  831. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  832. av_free_packet(avpkt);
  833. av_init_packet(avpkt);
  834. return 0;
  835. }
  836. /* ensure that extended_data is properly set */
  837. if (frame && !frame->extended_data) {
  838. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  839. avctx->channels > AV_NUM_DATA_POINTERS) {
  840. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  841. "with more than %d channels, but extended_data is not set.\n",
  842. AV_NUM_DATA_POINTERS);
  843. return AVERROR(EINVAL);
  844. }
  845. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  846. tmp = *frame;
  847. tmp.extended_data = tmp.data;
  848. frame = &tmp;
  849. }
  850. /* check for valid frame size */
  851. if (frame) {
  852. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  853. if (frame->nb_samples > avctx->frame_size)
  854. return AVERROR(EINVAL);
  855. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  856. if (frame->nb_samples < avctx->frame_size &&
  857. !avctx->internal->last_audio_frame) {
  858. ret = pad_last_frame(avctx, &padded_frame, frame);
  859. if (ret < 0)
  860. return ret;
  861. frame = padded_frame;
  862. avctx->internal->last_audio_frame = 1;
  863. }
  864. if (frame->nb_samples != avctx->frame_size) {
  865. ret = AVERROR(EINVAL);
  866. goto end;
  867. }
  868. }
  869. }
  870. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  871. if (!ret) {
  872. if (*got_packet_ptr) {
  873. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  874. if (avpkt->pts == AV_NOPTS_VALUE)
  875. avpkt->pts = frame->pts;
  876. if (!avpkt->duration)
  877. avpkt->duration = ff_samples_to_time_base(avctx,
  878. frame->nb_samples);
  879. }
  880. avpkt->dts = avpkt->pts;
  881. } else {
  882. avpkt->size = 0;
  883. }
  884. if (!user_packet && avpkt->size) {
  885. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  886. if (new_data)
  887. avpkt->data = new_data;
  888. }
  889. avctx->frame_number++;
  890. }
  891. if (ret < 0 || !*got_packet_ptr) {
  892. av_free_packet(avpkt);
  893. av_init_packet(avpkt);
  894. goto end;
  895. }
  896. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  897. * this needs to be moved to the encoders, but for now we can do it
  898. * here to simplify things */
  899. avpkt->flags |= AV_PKT_FLAG_KEY;
  900. end:
  901. if (padded_frame) {
  902. av_freep(&padded_frame->data[0]);
  903. if (padded_frame->extended_data != padded_frame->data)
  904. av_freep(&padded_frame->extended_data);
  905. av_freep(&padded_frame);
  906. }
  907. return ret;
  908. }
  909. #if FF_API_OLD_DECODE_AUDIO
  910. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  911. uint8_t *buf, int buf_size,
  912. const short *samples)
  913. {
  914. AVPacket pkt;
  915. AVFrame frame0;
  916. AVFrame *frame;
  917. int ret, samples_size, got_packet;
  918. av_init_packet(&pkt);
  919. pkt.data = buf;
  920. pkt.size = buf_size;
  921. if (samples) {
  922. frame = &frame0;
  923. avcodec_get_frame_defaults(frame);
  924. if (avctx->frame_size) {
  925. frame->nb_samples = avctx->frame_size;
  926. } else {
  927. /* if frame_size is not set, the number of samples must be
  928. * calculated from the buffer size */
  929. int64_t nb_samples;
  930. if (!av_get_bits_per_sample(avctx->codec_id)) {
  931. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  932. "support this codec\n");
  933. return AVERROR(EINVAL);
  934. }
  935. nb_samples = (int64_t)buf_size * 8 /
  936. (av_get_bits_per_sample(avctx->codec_id) *
  937. avctx->channels);
  938. if (nb_samples >= INT_MAX)
  939. return AVERROR(EINVAL);
  940. frame->nb_samples = nb_samples;
  941. }
  942. /* it is assumed that the samples buffer is large enough based on the
  943. * relevant parameters */
  944. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  945. frame->nb_samples,
  946. avctx->sample_fmt, 1);
  947. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  948. avctx->sample_fmt,
  949. (const uint8_t *)samples,
  950. samples_size, 1)))
  951. return ret;
  952. /* fabricate frame pts from sample count.
  953. * this is needed because the avcodec_encode_audio() API does not have
  954. * a way for the user to provide pts */
  955. frame->pts = ff_samples_to_time_base(avctx,
  956. avctx->internal->sample_count);
  957. avctx->internal->sample_count += frame->nb_samples;
  958. } else {
  959. frame = NULL;
  960. }
  961. got_packet = 0;
  962. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  963. if (!ret && got_packet && avctx->coded_frame) {
  964. avctx->coded_frame->pts = pkt.pts;
  965. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  966. }
  967. /* free any side data since we cannot return it */
  968. if (pkt.side_data_elems > 0) {
  969. int i;
  970. for (i = 0; i < pkt.side_data_elems; i++)
  971. av_free(pkt.side_data[i].data);
  972. av_freep(&pkt.side_data);
  973. pkt.side_data_elems = 0;
  974. }
  975. if (frame && frame->extended_data != frame->data)
  976. av_free(frame->extended_data);
  977. return ret ? ret : pkt.size;
  978. }
  979. #endif
  980. #if FF_API_OLD_ENCODE_VIDEO
  981. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  982. const AVFrame *pict)
  983. {
  984. AVPacket pkt;
  985. int ret, got_packet = 0;
  986. if (buf_size < FF_MIN_BUFFER_SIZE) {
  987. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  988. return -1;
  989. }
  990. av_init_packet(&pkt);
  991. pkt.data = buf;
  992. pkt.size = buf_size;
  993. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  994. if (!ret && got_packet && avctx->coded_frame) {
  995. avctx->coded_frame->pts = pkt.pts;
  996. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  997. }
  998. /* free any side data since we cannot return it */
  999. if (pkt.side_data_elems > 0) {
  1000. int i;
  1001. for (i = 0; i < pkt.side_data_elems; i++)
  1002. av_free(pkt.side_data[i].data);
  1003. av_freep(&pkt.side_data);
  1004. pkt.side_data_elems = 0;
  1005. }
  1006. return ret ? ret : pkt.size;
  1007. }
  1008. #endif
  1009. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1010. AVPacket *avpkt,
  1011. const AVFrame *frame,
  1012. int *got_packet_ptr)
  1013. {
  1014. int ret;
  1015. int user_packet = !!avpkt->data;
  1016. *got_packet_ptr = 0;
  1017. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1018. av_free_packet(avpkt);
  1019. av_init_packet(avpkt);
  1020. avpkt->size = 0;
  1021. return 0;
  1022. }
  1023. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1024. return AVERROR(EINVAL);
  1025. av_assert0(avctx->codec->encode2);
  1026. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1027. if (!ret) {
  1028. if (!*got_packet_ptr)
  1029. avpkt->size = 0;
  1030. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1031. avpkt->pts = avpkt->dts = frame->pts;
  1032. if (!user_packet && avpkt->size) {
  1033. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  1034. if (new_data)
  1035. avpkt->data = new_data;
  1036. }
  1037. avctx->frame_number++;
  1038. }
  1039. if (ret < 0 || !*got_packet_ptr)
  1040. av_free_packet(avpkt);
  1041. emms_c();
  1042. return ret;
  1043. }
  1044. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1045. const AVSubtitle *sub)
  1046. {
  1047. int ret;
  1048. if (sub->start_display_time) {
  1049. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1050. return -1;
  1051. }
  1052. if (sub->num_rects == 0 || !sub->rects)
  1053. return -1;
  1054. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1055. avctx->frame_number++;
  1056. return ret;
  1057. }
  1058. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1059. {
  1060. int size = 0;
  1061. const uint8_t *data;
  1062. uint32_t flags;
  1063. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1064. return;
  1065. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1066. if (!data || size < 4)
  1067. return;
  1068. flags = bytestream_get_le32(&data);
  1069. size -= 4;
  1070. if (size < 4) /* Required for any of the changes */
  1071. return;
  1072. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1073. avctx->channels = bytestream_get_le32(&data);
  1074. size -= 4;
  1075. }
  1076. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1077. if (size < 8)
  1078. return;
  1079. avctx->channel_layout = bytestream_get_le64(&data);
  1080. size -= 8;
  1081. }
  1082. if (size < 4)
  1083. return;
  1084. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1085. avctx->sample_rate = bytestream_get_le32(&data);
  1086. size -= 4;
  1087. }
  1088. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1089. if (size < 8)
  1090. return;
  1091. avctx->width = bytestream_get_le32(&data);
  1092. avctx->height = bytestream_get_le32(&data);
  1093. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1094. size -= 8;
  1095. }
  1096. }
  1097. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1098. int *got_picture_ptr,
  1099. AVPacket *avpkt)
  1100. {
  1101. int ret;
  1102. *got_picture_ptr = 0;
  1103. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1104. return -1;
  1105. avctx->pkt = avpkt;
  1106. apply_param_change(avctx, avpkt);
  1107. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1108. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1109. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1110. avpkt);
  1111. else {
  1112. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1113. avpkt);
  1114. picture->pkt_dts = avpkt->dts;
  1115. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1116. picture->width = avctx->width;
  1117. picture->height = avctx->height;
  1118. picture->format = avctx->pix_fmt;
  1119. }
  1120. emms_c(); //needed to avoid an emms_c() call before every return;
  1121. if (*got_picture_ptr)
  1122. avctx->frame_number++;
  1123. } else
  1124. ret = 0;
  1125. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1126. * make sure it's set correctly */
  1127. picture->extended_data = picture->data;
  1128. return ret;
  1129. }
  1130. #if FF_API_OLD_DECODE_AUDIO
  1131. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1132. int *frame_size_ptr,
  1133. AVPacket *avpkt)
  1134. {
  1135. AVFrame frame;
  1136. int ret, got_frame = 0;
  1137. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1138. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1139. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1140. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1141. "avcodec_decode_audio4()\n");
  1142. avctx->get_buffer = avcodec_default_get_buffer;
  1143. }
  1144. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1145. if (ret >= 0 && got_frame) {
  1146. int ch, plane_size;
  1147. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1148. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1149. frame.nb_samples,
  1150. avctx->sample_fmt, 1);
  1151. if (*frame_size_ptr < data_size) {
  1152. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1153. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1154. return AVERROR(EINVAL);
  1155. }
  1156. memcpy(samples, frame.extended_data[0], plane_size);
  1157. if (planar && avctx->channels > 1) {
  1158. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1159. for (ch = 1; ch < avctx->channels; ch++) {
  1160. memcpy(out, frame.extended_data[ch], plane_size);
  1161. out += plane_size;
  1162. }
  1163. }
  1164. *frame_size_ptr = data_size;
  1165. } else {
  1166. *frame_size_ptr = 0;
  1167. }
  1168. return ret;
  1169. }
  1170. #endif
  1171. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1172. AVFrame *frame,
  1173. int *got_frame_ptr,
  1174. AVPacket *avpkt)
  1175. {
  1176. int planar, channels;
  1177. int ret = 0;
  1178. *got_frame_ptr = 0;
  1179. avctx->pkt = avpkt;
  1180. if (!avpkt->data && avpkt->size) {
  1181. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1182. return AVERROR(EINVAL);
  1183. }
  1184. apply_param_change(avctx, avpkt);
  1185. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1186. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1187. if (ret >= 0 && *got_frame_ptr) {
  1188. avctx->frame_number++;
  1189. frame->pkt_dts = avpkt->dts;
  1190. if (frame->format == AV_SAMPLE_FMT_NONE)
  1191. frame->format = avctx->sample_fmt;
  1192. }
  1193. }
  1194. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1195. * make sure it's set correctly; assume decoders that actually use
  1196. * extended_data are doing it correctly */
  1197. planar = av_sample_fmt_is_planar(frame->format);
  1198. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1199. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1200. frame->extended_data = frame->data;
  1201. return ret;
  1202. }
  1203. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1204. int *got_sub_ptr,
  1205. AVPacket *avpkt)
  1206. {
  1207. int ret;
  1208. avctx->pkt = avpkt;
  1209. *got_sub_ptr = 0;
  1210. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1211. if (*got_sub_ptr)
  1212. avctx->frame_number++;
  1213. return ret;
  1214. }
  1215. void avsubtitle_free(AVSubtitle *sub)
  1216. {
  1217. int i;
  1218. for (i = 0; i < sub->num_rects; i++) {
  1219. av_freep(&sub->rects[i]->pict.data[0]);
  1220. av_freep(&sub->rects[i]->pict.data[1]);
  1221. av_freep(&sub->rects[i]->pict.data[2]);
  1222. av_freep(&sub->rects[i]->pict.data[3]);
  1223. av_freep(&sub->rects[i]->text);
  1224. av_freep(&sub->rects[i]->ass);
  1225. av_freep(&sub->rects[i]);
  1226. }
  1227. av_freep(&sub->rects);
  1228. memset(sub, 0, sizeof(AVSubtitle));
  1229. }
  1230. av_cold int avcodec_close(AVCodecContext *avctx)
  1231. {
  1232. /* If there is a user-supplied mutex locking routine, call it. */
  1233. if (ff_lockmgr_cb) {
  1234. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1235. return -1;
  1236. }
  1237. entangled_thread_counter++;
  1238. if (entangled_thread_counter != 1) {
  1239. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1240. entangled_thread_counter--;
  1241. return -1;
  1242. }
  1243. if (avcodec_is_open(avctx)) {
  1244. if (HAVE_THREADS && avctx->thread_opaque)
  1245. ff_thread_free(avctx);
  1246. if (avctx->codec && avctx->codec->close)
  1247. avctx->codec->close(avctx);
  1248. avcodec_default_free_buffers(avctx);
  1249. avctx->coded_frame = NULL;
  1250. av_freep(&avctx->internal);
  1251. }
  1252. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1253. av_opt_free(avctx->priv_data);
  1254. av_opt_free(avctx);
  1255. av_freep(&avctx->priv_data);
  1256. if (av_codec_is_encoder(avctx->codec))
  1257. av_freep(&avctx->extradata);
  1258. avctx->codec = NULL;
  1259. avctx->active_thread_type = 0;
  1260. entangled_thread_counter--;
  1261. /* Release any user-supplied mutex. */
  1262. if (ff_lockmgr_cb) {
  1263. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1264. }
  1265. return 0;
  1266. }
  1267. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1268. {
  1269. AVCodec *p, *experimental = NULL;
  1270. p = first_avcodec;
  1271. while (p) {
  1272. if (av_codec_is_encoder(p) && p->id == id) {
  1273. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1274. experimental = p;
  1275. } else
  1276. return p;
  1277. }
  1278. p = p->next;
  1279. }
  1280. return experimental;
  1281. }
  1282. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1283. {
  1284. AVCodec *p;
  1285. if (!name)
  1286. return NULL;
  1287. p = first_avcodec;
  1288. while (p) {
  1289. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1290. return p;
  1291. p = p->next;
  1292. }
  1293. return NULL;
  1294. }
  1295. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1296. {
  1297. AVCodec *p;
  1298. p = first_avcodec;
  1299. while (p) {
  1300. if (av_codec_is_decoder(p) && p->id == id)
  1301. return p;
  1302. p = p->next;
  1303. }
  1304. return NULL;
  1305. }
  1306. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1307. {
  1308. AVCodec *p;
  1309. if (!name)
  1310. return NULL;
  1311. p = first_avcodec;
  1312. while (p) {
  1313. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1314. return p;
  1315. p = p->next;
  1316. }
  1317. return NULL;
  1318. }
  1319. static int get_bit_rate(AVCodecContext *ctx)
  1320. {
  1321. int bit_rate;
  1322. int bits_per_sample;
  1323. switch (ctx->codec_type) {
  1324. case AVMEDIA_TYPE_VIDEO:
  1325. case AVMEDIA_TYPE_DATA:
  1326. case AVMEDIA_TYPE_SUBTITLE:
  1327. case AVMEDIA_TYPE_ATTACHMENT:
  1328. bit_rate = ctx->bit_rate;
  1329. break;
  1330. case AVMEDIA_TYPE_AUDIO:
  1331. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1332. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1333. break;
  1334. default:
  1335. bit_rate = 0;
  1336. break;
  1337. }
  1338. return bit_rate;
  1339. }
  1340. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1341. {
  1342. int i, len, ret = 0;
  1343. for (i = 0; i < 4; i++) {
  1344. len = snprintf(buf, buf_size,
  1345. isprint(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1346. buf += len;
  1347. buf_size = buf_size > len ? buf_size - len : 0;
  1348. ret += len;
  1349. codec_tag >>= 8;
  1350. }
  1351. return ret;
  1352. }
  1353. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1354. {
  1355. const char *codec_name;
  1356. const char *profile = NULL;
  1357. const AVCodec *p;
  1358. char buf1[32];
  1359. int bitrate;
  1360. AVRational display_aspect_ratio;
  1361. if (enc->codec)
  1362. p = enc->codec;
  1363. else if (encode)
  1364. p = avcodec_find_encoder(enc->codec_id);
  1365. else
  1366. p = avcodec_find_decoder(enc->codec_id);
  1367. if (p) {
  1368. codec_name = p->name;
  1369. profile = av_get_profile_name(p, enc->profile);
  1370. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1371. /* fake mpeg2 transport stream codec (currently not
  1372. * registered) */
  1373. codec_name = "mpeg2ts";
  1374. } else if (enc->codec_name[0] != '\0') {
  1375. codec_name = enc->codec_name;
  1376. } else {
  1377. /* output avi tags */
  1378. char tag_buf[32];
  1379. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1380. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1381. codec_name = buf1;
  1382. }
  1383. switch (enc->codec_type) {
  1384. case AVMEDIA_TYPE_VIDEO:
  1385. snprintf(buf, buf_size,
  1386. "Video: %s%s",
  1387. codec_name, enc->mb_decision ? " (hq)" : "");
  1388. if (profile)
  1389. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1390. " (%s)", profile);
  1391. if (enc->pix_fmt != PIX_FMT_NONE) {
  1392. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1393. ", %s",
  1394. av_get_pix_fmt_name(enc->pix_fmt));
  1395. }
  1396. if (enc->width) {
  1397. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1398. ", %dx%d",
  1399. enc->width, enc->height);
  1400. if (enc->sample_aspect_ratio.num) {
  1401. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1402. enc->width * enc->sample_aspect_ratio.num,
  1403. enc->height * enc->sample_aspect_ratio.den,
  1404. 1024 * 1024);
  1405. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1406. " [PAR %d:%d DAR %d:%d]",
  1407. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1408. display_aspect_ratio.num, display_aspect_ratio.den);
  1409. }
  1410. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1411. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1412. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1413. ", %d/%d",
  1414. enc->time_base.num / g, enc->time_base.den / g);
  1415. }
  1416. }
  1417. if (encode) {
  1418. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1419. ", q=%d-%d", enc->qmin, enc->qmax);
  1420. }
  1421. break;
  1422. case AVMEDIA_TYPE_AUDIO:
  1423. snprintf(buf, buf_size,
  1424. "Audio: %s",
  1425. codec_name);
  1426. if (profile)
  1427. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1428. " (%s)", profile);
  1429. if (enc->sample_rate) {
  1430. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1431. ", %d Hz", enc->sample_rate);
  1432. }
  1433. av_strlcat(buf, ", ", buf_size);
  1434. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1435. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1436. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1437. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1438. }
  1439. break;
  1440. case AVMEDIA_TYPE_DATA:
  1441. snprintf(buf, buf_size, "Data: %s", codec_name);
  1442. break;
  1443. case AVMEDIA_TYPE_SUBTITLE:
  1444. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1445. break;
  1446. case AVMEDIA_TYPE_ATTACHMENT:
  1447. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1448. break;
  1449. default:
  1450. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1451. return;
  1452. }
  1453. if (encode) {
  1454. if (enc->flags & CODEC_FLAG_PASS1)
  1455. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1456. ", pass 1");
  1457. if (enc->flags & CODEC_FLAG_PASS2)
  1458. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1459. ", pass 2");
  1460. }
  1461. bitrate = get_bit_rate(enc);
  1462. if (bitrate != 0) {
  1463. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1464. ", %d kb/s", bitrate / 1000);
  1465. }
  1466. }
  1467. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1468. {
  1469. const AVProfile *p;
  1470. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1471. return NULL;
  1472. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1473. if (p->profile == profile)
  1474. return p->name;
  1475. return NULL;
  1476. }
  1477. unsigned avcodec_version(void)
  1478. {
  1479. return LIBAVCODEC_VERSION_INT;
  1480. }
  1481. const char *avcodec_configuration(void)
  1482. {
  1483. return LIBAV_CONFIGURATION;
  1484. }
  1485. const char *avcodec_license(void)
  1486. {
  1487. #define LICENSE_PREFIX "libavcodec license: "
  1488. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1489. }
  1490. void avcodec_flush_buffers(AVCodecContext *avctx)
  1491. {
  1492. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1493. ff_thread_flush(avctx);
  1494. else if (avctx->codec->flush)
  1495. avctx->codec->flush(avctx);
  1496. }
  1497. static void video_free_buffers(AVCodecContext *s)
  1498. {
  1499. AVCodecInternal *avci = s->internal;
  1500. int i, j;
  1501. if (!avci->buffer)
  1502. return;
  1503. if (avci->buffer_count)
  1504. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1505. avci->buffer_count);
  1506. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1507. InternalBuffer *buf = &avci->buffer[i];
  1508. for (j = 0; j < 4; j++) {
  1509. av_freep(&buf->base[j]);
  1510. buf->data[j] = NULL;
  1511. }
  1512. }
  1513. av_freep(&avci->buffer);
  1514. avci->buffer_count = 0;
  1515. }
  1516. static void audio_free_buffers(AVCodecContext *avctx)
  1517. {
  1518. AVCodecInternal *avci = avctx->internal;
  1519. InternalBuffer *buf;
  1520. if (!avci->buffer)
  1521. return;
  1522. buf = avci->buffer;
  1523. if (buf->extended_data) {
  1524. av_free(buf->extended_data[0]);
  1525. if (buf->extended_data != buf->data)
  1526. av_free(buf->extended_data);
  1527. }
  1528. av_freep(&avci->buffer);
  1529. }
  1530. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1531. {
  1532. switch (avctx->codec_type) {
  1533. case AVMEDIA_TYPE_VIDEO:
  1534. video_free_buffers(avctx);
  1535. break;
  1536. case AVMEDIA_TYPE_AUDIO:
  1537. audio_free_buffers(avctx);
  1538. break;
  1539. default:
  1540. break;
  1541. }
  1542. }
  1543. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1544. {
  1545. switch (codec_id) {
  1546. case AV_CODEC_ID_ADPCM_CT:
  1547. case AV_CODEC_ID_ADPCM_IMA_APC:
  1548. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1549. case AV_CODEC_ID_ADPCM_IMA_WS:
  1550. case AV_CODEC_ID_ADPCM_G722:
  1551. case AV_CODEC_ID_ADPCM_YAMAHA:
  1552. return 4;
  1553. case AV_CODEC_ID_PCM_ALAW:
  1554. case AV_CODEC_ID_PCM_MULAW:
  1555. case AV_CODEC_ID_PCM_S8:
  1556. case AV_CODEC_ID_PCM_U8:
  1557. case AV_CODEC_ID_PCM_ZORK:
  1558. return 8;
  1559. case AV_CODEC_ID_PCM_S16BE:
  1560. case AV_CODEC_ID_PCM_S16LE:
  1561. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1562. case AV_CODEC_ID_PCM_U16BE:
  1563. case AV_CODEC_ID_PCM_U16LE:
  1564. return 16;
  1565. case AV_CODEC_ID_PCM_S24DAUD:
  1566. case AV_CODEC_ID_PCM_S24BE:
  1567. case AV_CODEC_ID_PCM_S24LE:
  1568. case AV_CODEC_ID_PCM_U24BE:
  1569. case AV_CODEC_ID_PCM_U24LE:
  1570. return 24;
  1571. case AV_CODEC_ID_PCM_S32BE:
  1572. case AV_CODEC_ID_PCM_S32LE:
  1573. case AV_CODEC_ID_PCM_U32BE:
  1574. case AV_CODEC_ID_PCM_U32LE:
  1575. case AV_CODEC_ID_PCM_F32BE:
  1576. case AV_CODEC_ID_PCM_F32LE:
  1577. return 32;
  1578. case AV_CODEC_ID_PCM_F64BE:
  1579. case AV_CODEC_ID_PCM_F64LE:
  1580. return 64;
  1581. default:
  1582. return 0;
  1583. }
  1584. }
  1585. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1586. {
  1587. switch (codec_id) {
  1588. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1589. return 2;
  1590. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1591. return 3;
  1592. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1593. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1594. case AV_CODEC_ID_ADPCM_IMA_QT:
  1595. case AV_CODEC_ID_ADPCM_SWF:
  1596. case AV_CODEC_ID_ADPCM_MS:
  1597. return 4;
  1598. default:
  1599. return av_get_exact_bits_per_sample(codec_id);
  1600. }
  1601. }
  1602. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1603. {
  1604. int id, sr, ch, ba, tag, bps;
  1605. id = avctx->codec_id;
  1606. sr = avctx->sample_rate;
  1607. ch = avctx->channels;
  1608. ba = avctx->block_align;
  1609. tag = avctx->codec_tag;
  1610. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1611. /* codecs with an exact constant bits per sample */
  1612. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1613. return (frame_bytes * 8) / (bps * ch);
  1614. bps = avctx->bits_per_coded_sample;
  1615. /* codecs with a fixed packet duration */
  1616. switch (id) {
  1617. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1618. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1619. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1620. case AV_CODEC_ID_AMR_NB:
  1621. case AV_CODEC_ID_GSM:
  1622. case AV_CODEC_ID_QCELP:
  1623. case AV_CODEC_ID_RA_144:
  1624. case AV_CODEC_ID_RA_288: return 160;
  1625. case AV_CODEC_ID_IMC: return 256;
  1626. case AV_CODEC_ID_AMR_WB:
  1627. case AV_CODEC_ID_GSM_MS: return 320;
  1628. case AV_CODEC_ID_MP1: return 384;
  1629. case AV_CODEC_ID_ATRAC1: return 512;
  1630. case AV_CODEC_ID_ATRAC3: return 1024;
  1631. case AV_CODEC_ID_MP2:
  1632. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1633. case AV_CODEC_ID_AC3: return 1536;
  1634. }
  1635. if (sr > 0) {
  1636. /* calc from sample rate */
  1637. if (id == AV_CODEC_ID_TTA)
  1638. return 256 * sr / 245;
  1639. if (ch > 0) {
  1640. /* calc from sample rate and channels */
  1641. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1642. return (480 << (sr / 22050)) / ch;
  1643. }
  1644. }
  1645. if (ba > 0) {
  1646. /* calc from block_align */
  1647. if (id == AV_CODEC_ID_SIPR) {
  1648. switch (ba) {
  1649. case 20: return 160;
  1650. case 19: return 144;
  1651. case 29: return 288;
  1652. case 37: return 480;
  1653. }
  1654. } else if (id == AV_CODEC_ID_ILBC) {
  1655. switch (ba) {
  1656. case 38: return 160;
  1657. case 50: return 240;
  1658. }
  1659. }
  1660. }
  1661. if (frame_bytes > 0) {
  1662. /* calc from frame_bytes only */
  1663. if (id == AV_CODEC_ID_TRUESPEECH)
  1664. return 240 * (frame_bytes / 32);
  1665. if (id == AV_CODEC_ID_NELLYMOSER)
  1666. return 256 * (frame_bytes / 64);
  1667. if (bps > 0) {
  1668. /* calc from frame_bytes and bits_per_coded_sample */
  1669. if (id == AV_CODEC_ID_ADPCM_G726)
  1670. return frame_bytes * 8 / bps;
  1671. }
  1672. if (ch > 0) {
  1673. /* calc from frame_bytes and channels */
  1674. switch (id) {
  1675. case AV_CODEC_ID_ADPCM_4XM:
  1676. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1677. return (frame_bytes - 4 * ch) * 2 / ch;
  1678. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1679. return (frame_bytes - 4) * 2 / ch;
  1680. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1681. return (frame_bytes - 8) * 2 / ch;
  1682. case AV_CODEC_ID_ADPCM_XA:
  1683. return (frame_bytes / 128) * 224 / ch;
  1684. case AV_CODEC_ID_INTERPLAY_DPCM:
  1685. return (frame_bytes - 6 - ch) / ch;
  1686. case AV_CODEC_ID_ROQ_DPCM:
  1687. return (frame_bytes - 8) / ch;
  1688. case AV_CODEC_ID_XAN_DPCM:
  1689. return (frame_bytes - 2 * ch) / ch;
  1690. case AV_CODEC_ID_MACE3:
  1691. return 3 * frame_bytes / ch;
  1692. case AV_CODEC_ID_MACE6:
  1693. return 6 * frame_bytes / ch;
  1694. case AV_CODEC_ID_PCM_LXF:
  1695. return 2 * (frame_bytes / (5 * ch));
  1696. }
  1697. if (tag) {
  1698. /* calc from frame_bytes, channels, and codec_tag */
  1699. if (id == AV_CODEC_ID_SOL_DPCM) {
  1700. if (tag == 3)
  1701. return frame_bytes / ch;
  1702. else
  1703. return frame_bytes * 2 / ch;
  1704. }
  1705. }
  1706. if (ba > 0) {
  1707. /* calc from frame_bytes, channels, and block_align */
  1708. int blocks = frame_bytes / ba;
  1709. switch (avctx->codec_id) {
  1710. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1711. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1712. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1713. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1714. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1715. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1716. case AV_CODEC_ID_ADPCM_MS:
  1717. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1718. }
  1719. }
  1720. if (bps > 0) {
  1721. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1722. switch (avctx->codec_id) {
  1723. case AV_CODEC_ID_PCM_DVD:
  1724. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1725. case AV_CODEC_ID_PCM_BLURAY:
  1726. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1727. case AV_CODEC_ID_S302M:
  1728. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1729. }
  1730. }
  1731. }
  1732. }
  1733. return 0;
  1734. }
  1735. #if !HAVE_THREADS
  1736. int ff_thread_init(AVCodecContext *s)
  1737. {
  1738. return -1;
  1739. }
  1740. #endif
  1741. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1742. {
  1743. unsigned int n = 0;
  1744. while (v >= 0xff) {
  1745. *s++ = 0xff;
  1746. v -= 0xff;
  1747. n++;
  1748. }
  1749. *s = v;
  1750. n++;
  1751. return n;
  1752. }
  1753. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1754. {
  1755. int i;
  1756. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1757. return i;
  1758. }
  1759. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1760. {
  1761. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1762. "version to the newest one from Git. If the problem still "
  1763. "occurs, it means that your file has a feature which has not "
  1764. "been implemented.\n", feature);
  1765. if(want_sample)
  1766. av_log_ask_for_sample(avc, NULL);
  1767. }
  1768. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1769. {
  1770. va_list argument_list;
  1771. va_start(argument_list, msg);
  1772. if (msg)
  1773. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1774. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1775. "of this file to ftp://upload.libav.org/incoming/ "
  1776. "and contact the libav-devel mailing list.\n");
  1777. va_end(argument_list);
  1778. }
  1779. static AVHWAccel *first_hwaccel = NULL;
  1780. void av_register_hwaccel(AVHWAccel *hwaccel)
  1781. {
  1782. AVHWAccel **p = &first_hwaccel;
  1783. while (*p)
  1784. p = &(*p)->next;
  1785. *p = hwaccel;
  1786. hwaccel->next = NULL;
  1787. }
  1788. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1789. {
  1790. return hwaccel ? hwaccel->next : first_hwaccel;
  1791. }
  1792. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum PixelFormat pix_fmt)
  1793. {
  1794. AVHWAccel *hwaccel = NULL;
  1795. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1796. if (hwaccel->id == codec_id
  1797. && hwaccel->pix_fmt == pix_fmt)
  1798. return hwaccel;
  1799. return NULL;
  1800. }
  1801. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1802. {
  1803. if (ff_lockmgr_cb) {
  1804. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1805. return -1;
  1806. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1807. return -1;
  1808. }
  1809. ff_lockmgr_cb = cb;
  1810. if (ff_lockmgr_cb) {
  1811. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1812. return -1;
  1813. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1814. return -1;
  1815. }
  1816. return 0;
  1817. }
  1818. int avpriv_lock_avformat(void)
  1819. {
  1820. if (ff_lockmgr_cb) {
  1821. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1822. return -1;
  1823. }
  1824. return 0;
  1825. }
  1826. int avpriv_unlock_avformat(void)
  1827. {
  1828. if (ff_lockmgr_cb) {
  1829. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1830. return -1;
  1831. }
  1832. return 0;
  1833. }
  1834. unsigned int avpriv_toupper4(unsigned int x)
  1835. {
  1836. return toupper(x & 0xFF)
  1837. + (toupper((x >> 8) & 0xFF) << 8)
  1838. + (toupper((x >> 16) & 0xFF) << 16)
  1839. + (toupper((x >> 24) & 0xFF) << 24);
  1840. }
  1841. #if !HAVE_THREADS
  1842. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1843. {
  1844. f->owner = avctx;
  1845. return avctx->get_buffer(avctx, f);
  1846. }
  1847. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1848. {
  1849. f->owner->release_buffer(f->owner, f);
  1850. }
  1851. void ff_thread_finish_setup(AVCodecContext *avctx)
  1852. {
  1853. }
  1854. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1855. {
  1856. }
  1857. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1858. {
  1859. }
  1860. #endif
  1861. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1862. {
  1863. if (codec_id <= AV_CODEC_ID_NONE)
  1864. return AVMEDIA_TYPE_UNKNOWN;
  1865. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1866. return AVMEDIA_TYPE_VIDEO;
  1867. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1868. return AVMEDIA_TYPE_AUDIO;
  1869. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1870. return AVMEDIA_TYPE_SUBTITLE;
  1871. return AVMEDIA_TYPE_UNKNOWN;
  1872. }
  1873. int avcodec_is_open(AVCodecContext *s)
  1874. {
  1875. return !!s->internal;
  1876. }