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.

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