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.

2145 lines
64KB

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