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.

2189 lines
66KB

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