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.

2469 lines
77KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "imgconvert.h"
  40. #include "thread.h"
  41. #include "frame_thread_encoder.h"
  42. #include "audioconvert.h"
  43. #include "internal.h"
  44. #include "bytestream.h"
  45. #include <stdlib.h>
  46. #include <stdarg.h>
  47. #include <limits.h>
  48. #include <float.h>
  49. static int volatile entangled_thread_counter=0;
  50. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  51. static void *codec_mutex;
  52. static void *avformat_mutex;
  53. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  54. {
  55. if(min_size < *size)
  56. return ptr;
  57. min_size= FFMAX(17*min_size/16 + 32, min_size);
  58. ptr= av_realloc(ptr, min_size);
  59. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  60. min_size= 0;
  61. *size= min_size;
  62. return ptr;
  63. }
  64. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  65. {
  66. void **p = ptr;
  67. if (min_size < *size)
  68. return 0;
  69. min_size= FFMAX(17*min_size/16 + 32, min_size);
  70. av_free(*p);
  71. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  72. if (!*p) min_size = 0;
  73. *size= min_size;
  74. return 1;
  75. }
  76. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  77. {
  78. ff_fast_malloc(ptr, size, min_size, 0);
  79. }
  80. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  81. {
  82. uint8_t **p = ptr;
  83. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  84. av_freep(p);
  85. *size = 0;
  86. return;
  87. }
  88. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  89. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  90. }
  91. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  92. {
  93. uint8_t **p = ptr;
  94. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  95. av_freep(p);
  96. *size = 0;
  97. return;
  98. }
  99. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  100. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  101. }
  102. /* encoder management */
  103. static AVCodec *first_avcodec = NULL;
  104. AVCodec *av_codec_next(AVCodec *c){
  105. if(c) return c->next;
  106. else return first_avcodec;
  107. }
  108. static void avcodec_init(void)
  109. {
  110. static int initialized = 0;
  111. if (initialized != 0)
  112. return;
  113. initialized = 1;
  114. ff_dsputil_static_init();
  115. }
  116. int av_codec_is_encoder(AVCodec *codec)
  117. {
  118. return codec && (codec->encode || codec->encode2);
  119. }
  120. int av_codec_is_decoder(AVCodec *codec)
  121. {
  122. return codec && codec->decode;
  123. }
  124. void avcodec_register(AVCodec *codec)
  125. {
  126. AVCodec **p;
  127. avcodec_init();
  128. p = &first_avcodec;
  129. while (*p != NULL) p = &(*p)->next;
  130. *p = codec;
  131. codec->next = NULL;
  132. if (codec->init_static_data)
  133. codec->init_static_data(codec);
  134. }
  135. unsigned avcodec_get_edge_width(void)
  136. {
  137. return EDGE_WIDTH;
  138. }
  139. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  140. s->coded_width = width;
  141. s->coded_height= height;
  142. s->width = -((-width )>>s->lowres);
  143. s->height= -((-height)>>s->lowres);
  144. }
  145. #define INTERNAL_BUFFER_SIZE (32+1)
  146. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  147. int linesize_align[AV_NUM_DATA_POINTERS])
  148. {
  149. int i;
  150. int w_align= 1;
  151. int h_align= 1;
  152. switch(s->pix_fmt){
  153. case PIX_FMT_YUV420P:
  154. case PIX_FMT_YUYV422:
  155. case PIX_FMT_UYVY422:
  156. case PIX_FMT_YUV422P:
  157. case PIX_FMT_YUV440P:
  158. case PIX_FMT_YUV444P:
  159. case PIX_FMT_GBRP:
  160. case PIX_FMT_GRAY8:
  161. case PIX_FMT_GRAY16BE:
  162. case PIX_FMT_GRAY16LE:
  163. case PIX_FMT_YUVJ420P:
  164. case PIX_FMT_YUVJ422P:
  165. case PIX_FMT_YUVJ440P:
  166. case PIX_FMT_YUVJ444P:
  167. case PIX_FMT_YUVA420P:
  168. case PIX_FMT_YUVA422P:
  169. case PIX_FMT_YUVA444P:
  170. case PIX_FMT_YUV420P9LE:
  171. case PIX_FMT_YUV420P9BE:
  172. case PIX_FMT_YUV420P10LE:
  173. case PIX_FMT_YUV420P10BE:
  174. case PIX_FMT_YUV420P12LE:
  175. case PIX_FMT_YUV420P12BE:
  176. case PIX_FMT_YUV420P14LE:
  177. case PIX_FMT_YUV420P14BE:
  178. case PIX_FMT_YUV422P9LE:
  179. case PIX_FMT_YUV422P9BE:
  180. case PIX_FMT_YUV422P10LE:
  181. case PIX_FMT_YUV422P10BE:
  182. case PIX_FMT_YUV422P12LE:
  183. case PIX_FMT_YUV422P12BE:
  184. case PIX_FMT_YUV422P14LE:
  185. case PIX_FMT_YUV422P14BE:
  186. case PIX_FMT_YUV444P9LE:
  187. case PIX_FMT_YUV444P9BE:
  188. case PIX_FMT_YUV444P10LE:
  189. case PIX_FMT_YUV444P10BE:
  190. case PIX_FMT_YUV444P12LE:
  191. case PIX_FMT_YUV444P12BE:
  192. case PIX_FMT_YUV444P14LE:
  193. case PIX_FMT_YUV444P14BE:
  194. case PIX_FMT_GBRP9LE:
  195. case PIX_FMT_GBRP9BE:
  196. case PIX_FMT_GBRP10LE:
  197. case PIX_FMT_GBRP10BE:
  198. case PIX_FMT_GBRP12LE:
  199. case PIX_FMT_GBRP12BE:
  200. case PIX_FMT_GBRP14LE:
  201. case PIX_FMT_GBRP14BE:
  202. w_align = 16; //FIXME assume 16 pixel per macroblock
  203. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  204. break;
  205. case PIX_FMT_YUV411P:
  206. case PIX_FMT_UYYVYY411:
  207. w_align=32;
  208. h_align=8;
  209. break;
  210. case PIX_FMT_YUV410P:
  211. if(s->codec_id == CODEC_ID_SVQ1){
  212. w_align=64;
  213. h_align=64;
  214. }
  215. case PIX_FMT_RGB555:
  216. if(s->codec_id == CODEC_ID_RPZA){
  217. w_align=4;
  218. h_align=4;
  219. }
  220. case PIX_FMT_PAL8:
  221. case PIX_FMT_BGR8:
  222. case PIX_FMT_RGB8:
  223. if(s->codec_id == CODEC_ID_SMC){
  224. w_align=4;
  225. h_align=4;
  226. }
  227. break;
  228. case PIX_FMT_BGR24:
  229. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  230. w_align=4;
  231. h_align=4;
  232. }
  233. break;
  234. default:
  235. w_align= 1;
  236. h_align= 1;
  237. break;
  238. }
  239. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  240. w_align= FFMAX(w_align, 8);
  241. }
  242. *width = FFALIGN(*width , w_align);
  243. *height= FFALIGN(*height, h_align);
  244. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  245. *height+=2; // some of the optimized chroma MC reads one line too much
  246. // which is also done in mpeg decoders with lowres > 0
  247. for (i = 0; i < 4; i++)
  248. linesize_align[i] = STRIDE_ALIGN;
  249. }
  250. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  251. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  252. int linesize_align[AV_NUM_DATA_POINTERS];
  253. int align;
  254. avcodec_align_dimensions2(s, width, height, linesize_align);
  255. align = FFMAX(linesize_align[0], linesize_align[3]);
  256. linesize_align[1] <<= chroma_shift;
  257. linesize_align[2] <<= chroma_shift;
  258. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  259. *width=FFALIGN(*width, align);
  260. }
  261. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  262. {
  263. if (s->pkt) {
  264. pic->pkt_pts = s->pkt->pts;
  265. pic->pkt_pos = s->pkt->pos;
  266. pic->pkt_duration = s->pkt->duration;
  267. } else {
  268. pic->pkt_pts = AV_NOPTS_VALUE;
  269. pic->pkt_pos = -1;
  270. pic->pkt_duration = 0;
  271. }
  272. pic->reordered_opaque= s->reordered_opaque;
  273. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  274. pic->width = s->width;
  275. pic->height = s->height;
  276. pic->format = s->pix_fmt;
  277. }
  278. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  279. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  280. int buf_size, int align)
  281. {
  282. int ch, planar, needed_size, ret = 0;
  283. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  284. frame->nb_samples, sample_fmt,
  285. align);
  286. if (buf_size < needed_size)
  287. return AVERROR(EINVAL);
  288. planar = av_sample_fmt_is_planar(sample_fmt);
  289. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  290. if (!(frame->extended_data = av_mallocz(nb_channels *
  291. sizeof(*frame->extended_data))))
  292. return AVERROR(ENOMEM);
  293. } else {
  294. frame->extended_data = frame->data;
  295. }
  296. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  297. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  298. sample_fmt, align)) < 0) {
  299. if (frame->extended_data != frame->data)
  300. av_freep(&frame->extended_data);
  301. return ret;
  302. }
  303. if (frame->extended_data != frame->data) {
  304. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  305. frame->data[ch] = frame->extended_data[ch];
  306. }
  307. return ret;
  308. }
  309. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  310. {
  311. AVCodecInternal *avci = avctx->internal;
  312. InternalBuffer *buf;
  313. int buf_size, ret;
  314. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  315. frame->nb_samples, avctx->sample_fmt,
  316. 0);
  317. if (buf_size < 0)
  318. return AVERROR(EINVAL);
  319. /* allocate InternalBuffer if needed */
  320. if (!avci->buffer) {
  321. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  322. if (!avci->buffer)
  323. return AVERROR(ENOMEM);
  324. }
  325. buf = avci->buffer;
  326. /* if there is a previously-used internal buffer, check its size and
  327. channel count to see if we can reuse it */
  328. if (buf->extended_data) {
  329. /* if current buffer is too small, free it */
  330. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  331. av_free(buf->extended_data[0]);
  332. if (buf->extended_data != buf->data)
  333. av_freep(&buf->extended_data);
  334. buf->extended_data = NULL;
  335. buf->data[0] = NULL;
  336. }
  337. /* if number of channels has changed, reset and/or free extended data
  338. pointers but leave data buffer in buf->data[0] for reuse */
  339. if (buf->nb_channels != avctx->channels) {
  340. if (buf->extended_data != buf->data)
  341. av_free(buf->extended_data);
  342. buf->extended_data = NULL;
  343. }
  344. }
  345. /* if there is no previous buffer or the previous buffer cannot be used
  346. as-is, allocate a new buffer and/or rearrange the channel pointers */
  347. if (!buf->extended_data) {
  348. if (!buf->data[0]) {
  349. if (!(buf->data[0] = av_mallocz(buf_size)))
  350. return AVERROR(ENOMEM);
  351. buf->audio_data_size = buf_size;
  352. }
  353. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  354. avctx->sample_fmt, buf->data[0],
  355. buf->audio_data_size, 0)))
  356. return ret;
  357. if (frame->extended_data == frame->data)
  358. buf->extended_data = buf->data;
  359. else
  360. buf->extended_data = frame->extended_data;
  361. memcpy(buf->data, frame->data, sizeof(frame->data));
  362. buf->linesize[0] = frame->linesize[0];
  363. buf->nb_channels = avctx->channels;
  364. } else {
  365. /* copy InternalBuffer info to the AVFrame */
  366. frame->extended_data = buf->extended_data;
  367. frame->linesize[0] = buf->linesize[0];
  368. memcpy(frame->data, buf->data, sizeof(frame->data));
  369. }
  370. frame->type = FF_BUFFER_TYPE_INTERNAL;
  371. if (avctx->pkt) {
  372. frame->pkt_pts = avctx->pkt->pts;
  373. frame->pkt_pos = avctx->pkt->pos;
  374. frame->pkt_duration = avctx->pkt->duration;
  375. } else {
  376. frame->pkt_pts = AV_NOPTS_VALUE;
  377. frame->pkt_pos = -1;
  378. frame->pkt_duration = 0;
  379. }
  380. frame->reordered_opaque = avctx->reordered_opaque;
  381. frame->sample_rate = avctx->sample_rate;
  382. frame->format = avctx->sample_fmt;
  383. frame->channel_layout = avctx->channel_layout;
  384. if (avctx->debug & FF_DEBUG_BUFFERS)
  385. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  386. "internal audio buffer used\n", frame);
  387. return 0;
  388. }
  389. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  390. {
  391. int i;
  392. int w= s->width;
  393. int h= s->height;
  394. InternalBuffer *buf;
  395. AVCodecInternal *avci = s->internal;
  396. if(pic->data[0]!=NULL) {
  397. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  398. return -1;
  399. }
  400. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  401. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  402. return -1;
  403. }
  404. if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
  405. av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  406. return -1;
  407. }
  408. if (!avci->buffer) {
  409. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  410. sizeof(InternalBuffer));
  411. }
  412. buf = &avci->buffer[avci->buffer_count];
  413. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  414. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  415. av_freep(&buf->base[i]);
  416. buf->data[i]= NULL;
  417. }
  418. }
  419. if (!buf->base[0]) {
  420. int h_chroma_shift, v_chroma_shift;
  421. int size[4] = {0};
  422. int tmpsize;
  423. int unaligned;
  424. AVPicture picture;
  425. int stride_align[AV_NUM_DATA_POINTERS];
  426. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  427. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  428. avcodec_align_dimensions2(s, &w, &h, stride_align);
  429. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  430. w+= EDGE_WIDTH*2;
  431. h+= EDGE_WIDTH*2;
  432. }
  433. do {
  434. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  435. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  436. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  437. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  438. w += w & ~(w-1);
  439. unaligned = 0;
  440. for (i=0; i<4; i++){
  441. unaligned |= picture.linesize[i] % stride_align[i];
  442. }
  443. } while (unaligned);
  444. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  445. if (tmpsize < 0)
  446. return -1;
  447. for (i=0; i<3 && picture.data[i+1]; i++)
  448. size[i] = picture.data[i+1] - picture.data[i];
  449. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  450. memset(buf->base, 0, sizeof(buf->base));
  451. memset(buf->data, 0, sizeof(buf->data));
  452. for(i=0; i<4 && size[i]; i++){
  453. const int h_shift= i==0 ? 0 : h_chroma_shift;
  454. const int v_shift= i==0 ? 0 : v_chroma_shift;
  455. buf->linesize[i]= picture.linesize[i];
  456. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  457. if(buf->base[i]==NULL)
  458. return AVERROR(ENOMEM);
  459. memset(buf->base[i], 128, size[i]);
  460. // no edge if EDGE EMU or not planar YUV
  461. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  462. buf->data[i] = buf->base[i];
  463. else
  464. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  465. }
  466. for (; i < AV_NUM_DATA_POINTERS; i++) {
  467. buf->base[i] = buf->data[i] = NULL;
  468. buf->linesize[i] = 0;
  469. }
  470. if(size[1] && !size[2])
  471. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  472. buf->width = s->width;
  473. buf->height = s->height;
  474. buf->pix_fmt= s->pix_fmt;
  475. }
  476. pic->type= FF_BUFFER_TYPE_INTERNAL;
  477. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  478. pic->base[i]= buf->base[i];
  479. pic->data[i]= buf->data[i];
  480. pic->linesize[i]= buf->linesize[i];
  481. }
  482. pic->extended_data = pic->data;
  483. avci->buffer_count++;
  484. pic->width = buf->width;
  485. pic->height = buf->height;
  486. pic->format = buf->pix_fmt;
  487. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  488. if (s->pkt) {
  489. pic->pkt_pts = s->pkt->pts;
  490. pic->pkt_pos = s->pkt->pos;
  491. pic->pkt_duration = s->pkt->duration;
  492. } else {
  493. pic->pkt_pts = AV_NOPTS_VALUE;
  494. pic->pkt_pos = -1;
  495. pic->pkt_duration = 0;
  496. }
  497. pic->reordered_opaque= s->reordered_opaque;
  498. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  499. pic->width = s->width;
  500. pic->height = s->height;
  501. pic->format = s->pix_fmt;
  502. if(s->debug&FF_DEBUG_BUFFERS)
  503. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  504. "buffers used\n", pic, avci->buffer_count);
  505. return 0;
  506. }
  507. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  508. {
  509. switch (avctx->codec_type) {
  510. case AVMEDIA_TYPE_VIDEO:
  511. return video_get_buffer(avctx, frame);
  512. case AVMEDIA_TYPE_AUDIO:
  513. return audio_get_buffer(avctx, frame);
  514. default:
  515. return -1;
  516. }
  517. }
  518. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  519. int i;
  520. InternalBuffer *buf, *last;
  521. AVCodecInternal *avci = s->internal;
  522. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  523. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  524. assert(avci->buffer_count);
  525. if (avci->buffer) {
  526. buf = NULL; /* avoids warning */
  527. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  528. buf = &avci->buffer[i];
  529. if (buf->data[0] == pic->data[0])
  530. break;
  531. }
  532. av_assert0(i < avci->buffer_count);
  533. avci->buffer_count--;
  534. last = &avci->buffer[avci->buffer_count];
  535. if (buf != last)
  536. FFSWAP(InternalBuffer, *buf, *last);
  537. }
  538. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  539. pic->data[i]=NULL;
  540. // pic->base[i]=NULL;
  541. }
  542. //printf("R%X\n", pic->opaque);
  543. if(s->debug&FF_DEBUG_BUFFERS)
  544. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  545. "buffers used\n", pic, avci->buffer_count);
  546. }
  547. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  548. AVFrame temp_pic;
  549. int i;
  550. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  551. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  552. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  553. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  554. s->release_buffer(s, pic);
  555. }
  556. ff_init_buffer_info(s, pic);
  557. /* If no picture return a new buffer */
  558. if(pic->data[0] == NULL) {
  559. /* We will copy from buffer, so must be readable */
  560. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  561. return s->get_buffer(s, pic);
  562. }
  563. assert(s->pix_fmt == pic->format);
  564. /* If internal buffer type return the same buffer */
  565. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  566. return 0;
  567. }
  568. /*
  569. * Not internal type and reget_buffer not overridden, emulate cr buffer
  570. */
  571. temp_pic = *pic;
  572. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  573. pic->data[i] = pic->base[i] = NULL;
  574. pic->opaque = NULL;
  575. /* Allocate new frame */
  576. if (s->get_buffer(s, pic))
  577. return -1;
  578. /* Copy image data from old buffer to new buffer */
  579. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  580. s->height);
  581. s->release_buffer(s, &temp_pic); // Release old frame
  582. return 0;
  583. }
  584. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  585. int i;
  586. for(i=0; i<count; i++){
  587. int r= func(c, (char*)arg + i*size);
  588. if(ret) ret[i]= r;
  589. }
  590. return 0;
  591. }
  592. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  593. int i;
  594. for(i=0; i<count; i++){
  595. int r= func(c, arg, i, 0);
  596. if(ret) ret[i]= r;
  597. }
  598. return 0;
  599. }
  600. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  601. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  602. ++fmt;
  603. return fmt[0];
  604. }
  605. void avcodec_get_frame_defaults(AVFrame *pic){
  606. memset(pic, 0, sizeof(AVFrame));
  607. pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  608. pic->pkt_duration = 0;
  609. pic->pkt_pos = -1;
  610. pic->key_frame= 1;
  611. pic->sample_aspect_ratio = (AVRational){0, 1};
  612. pic->format = -1; /* unknown */
  613. }
  614. AVFrame *avcodec_alloc_frame(void){
  615. AVFrame *pic= av_malloc(sizeof(AVFrame));
  616. if(pic==NULL) return NULL;
  617. avcodec_get_frame_defaults(pic);
  618. return pic;
  619. }
  620. #define MAKE_ACCESSORS(str, name, type, field) \
  621. type av_##name##_get_##field(const str *s) { return s->field; } \
  622. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  623. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  624. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  625. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  626. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  627. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  628. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  629. {
  630. memset(sub, 0, sizeof(*sub));
  631. sub->pts = AV_NOPTS_VALUE;
  632. }
  633. static int get_bit_rate(AVCodecContext *ctx)
  634. {
  635. int bit_rate;
  636. int bits_per_sample;
  637. switch(ctx->codec_type) {
  638. case AVMEDIA_TYPE_VIDEO:
  639. case AVMEDIA_TYPE_DATA:
  640. case AVMEDIA_TYPE_SUBTITLE:
  641. case AVMEDIA_TYPE_ATTACHMENT:
  642. bit_rate = ctx->bit_rate;
  643. break;
  644. case AVMEDIA_TYPE_AUDIO:
  645. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  646. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  647. break;
  648. default:
  649. bit_rate = 0;
  650. break;
  651. }
  652. return bit_rate;
  653. }
  654. #if FF_API_AVCODEC_OPEN
  655. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  656. {
  657. return avcodec_open2(avctx, codec, NULL);
  658. }
  659. #endif
  660. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  661. {
  662. int ret = 0;
  663. AVDictionary *tmp = NULL;
  664. if (avcodec_is_open(avctx))
  665. return 0;
  666. if ((!codec && !avctx->codec)) {
  667. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  668. return AVERROR(EINVAL);
  669. }
  670. if ((codec && avctx->codec && codec != avctx->codec)) {
  671. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  672. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  673. return AVERROR(EINVAL);
  674. }
  675. if (!codec)
  676. codec = avctx->codec;
  677. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  678. return AVERROR(EINVAL);
  679. if (options)
  680. av_dict_copy(&tmp, *options, 0);
  681. /* If there is a user-supplied mutex locking routine, call it. */
  682. if (ff_lockmgr_cb) {
  683. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  684. return -1;
  685. }
  686. entangled_thread_counter++;
  687. if(entangled_thread_counter != 1){
  688. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  689. ret = -1;
  690. goto end;
  691. }
  692. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  693. if (!avctx->internal) {
  694. ret = AVERROR(ENOMEM);
  695. goto end;
  696. }
  697. if (codec->priv_data_size > 0) {
  698. if(!avctx->priv_data){
  699. avctx->priv_data = av_mallocz(codec->priv_data_size);
  700. if (!avctx->priv_data) {
  701. ret = AVERROR(ENOMEM);
  702. goto end;
  703. }
  704. if (codec->priv_class) {
  705. *(const AVClass**)avctx->priv_data= codec->priv_class;
  706. av_opt_set_defaults(avctx->priv_data);
  707. }
  708. }
  709. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  710. goto free_and_end;
  711. } else {
  712. avctx->priv_data = NULL;
  713. }
  714. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  715. goto free_and_end;
  716. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  717. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  718. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
  719. ret = -1;
  720. goto free_and_end;
  721. }
  722. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  723. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  724. if(avctx->coded_width && avctx->coded_height)
  725. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  726. else if(avctx->width && avctx->height)
  727. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  728. }
  729. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  730. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  731. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  732. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  733. avcodec_set_dimensions(avctx, 0, 0);
  734. }
  735. /* if the decoder init function was already called previously,
  736. free the already allocated subtitle_header before overwriting it */
  737. if (av_codec_is_decoder(codec))
  738. av_freep(&avctx->subtitle_header);
  739. #define SANE_NB_CHANNELS 128U
  740. if (avctx->channels > SANE_NB_CHANNELS) {
  741. ret = AVERROR(EINVAL);
  742. goto free_and_end;
  743. }
  744. avctx->codec = codec;
  745. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  746. avctx->codec_id == CODEC_ID_NONE) {
  747. avctx->codec_type = codec->type;
  748. avctx->codec_id = codec->id;
  749. }
  750. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  751. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  752. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  753. ret = AVERROR(EINVAL);
  754. goto free_and_end;
  755. }
  756. avctx->frame_number = 0;
  757. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  758. (!avctx->time_base.num || !avctx->time_base.den)) {
  759. avctx->time_base.num = 1;
  760. avctx->time_base.den = avctx->sample_rate;
  761. }
  762. if (!HAVE_THREADS)
  763. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  764. entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  765. ret = ff_frame_thread_encoder_init(avctx);
  766. entangled_thread_counter++;
  767. if (ret < 0)
  768. goto free_and_end;
  769. if (HAVE_THREADS && !avctx->thread_opaque
  770. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  771. ret = ff_thread_init(avctx);
  772. if (ret < 0) {
  773. goto free_and_end;
  774. }
  775. }
  776. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  777. avctx->thread_count = 1;
  778. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  779. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  780. avctx->codec->max_lowres);
  781. ret = AVERROR(EINVAL);
  782. goto free_and_end;
  783. }
  784. if (av_codec_is_encoder(avctx->codec)) {
  785. int i;
  786. if (avctx->codec->sample_fmts) {
  787. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  788. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  789. break;
  790. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  791. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  792. ret = AVERROR(EINVAL);
  793. goto free_and_end;
  794. }
  795. }
  796. if (avctx->codec->pix_fmts) {
  797. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  798. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  799. break;
  800. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE
  801. && !((avctx->codec_id == CODEC_ID_MJPEG || avctx->codec_id == CODEC_ID_LJPEG)
  802. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  803. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  804. ret = AVERROR(EINVAL);
  805. goto free_and_end;
  806. }
  807. }
  808. if (avctx->codec->supported_samplerates) {
  809. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  810. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  811. break;
  812. if (avctx->codec->supported_samplerates[i] == 0) {
  813. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  814. ret = AVERROR(EINVAL);
  815. goto free_and_end;
  816. }
  817. }
  818. if (avctx->codec->channel_layouts) {
  819. if (!avctx->channel_layout) {
  820. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  821. } else {
  822. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  823. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  824. break;
  825. if (avctx->codec->channel_layouts[i] == 0) {
  826. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  827. ret = AVERROR(EINVAL);
  828. goto free_and_end;
  829. }
  830. }
  831. }
  832. if (avctx->channel_layout && avctx->channels) {
  833. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  834. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  835. ret = AVERROR(EINVAL);
  836. goto free_and_end;
  837. }
  838. } else if (avctx->channel_layout) {
  839. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  840. }
  841. }
  842. avctx->pts_correction_num_faulty_pts =
  843. avctx->pts_correction_num_faulty_dts = 0;
  844. avctx->pts_correction_last_pts =
  845. avctx->pts_correction_last_dts = INT64_MIN;
  846. if(avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME) || avctx->internal->frame_thread_encoder)){
  847. ret = avctx->codec->init(avctx);
  848. if (ret < 0) {
  849. goto free_and_end;
  850. }
  851. }
  852. ret=0;
  853. if (av_codec_is_decoder(avctx->codec)) {
  854. if (!avctx->bit_rate)
  855. avctx->bit_rate = get_bit_rate(avctx);
  856. /* validate channel layout from the decoder */
  857. if (avctx->channel_layout &&
  858. av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  859. av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
  860. avctx->channel_layout = 0;
  861. }
  862. }
  863. end:
  864. entangled_thread_counter--;
  865. /* Release any user-supplied mutex. */
  866. if (ff_lockmgr_cb) {
  867. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  868. }
  869. if (options) {
  870. av_dict_free(options);
  871. *options = tmp;
  872. }
  873. return ret;
  874. free_and_end:
  875. av_dict_free(&tmp);
  876. av_freep(&avctx->priv_data);
  877. av_freep(&avctx->internal);
  878. avctx->codec= NULL;
  879. goto end;
  880. }
  881. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  882. {
  883. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  884. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  885. return AVERROR(EINVAL);
  886. }
  887. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  888. if (!avpkt->data || avpkt->size < size) {
  889. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  890. avpkt->data = avctx->internal->byte_buffer;
  891. avpkt->size = avctx->internal->byte_buffer_size;
  892. avpkt->destruct = NULL;
  893. }
  894. if (avpkt->data) {
  895. void *destruct = avpkt->destruct;
  896. if (avpkt->size < size) {
  897. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  898. return AVERROR(EINVAL);
  899. }
  900. av_init_packet(avpkt);
  901. avpkt->destruct = destruct;
  902. avpkt->size = size;
  903. return 0;
  904. } else {
  905. int ret = av_new_packet(avpkt, size);
  906. if (ret < 0)
  907. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  908. return ret;
  909. }
  910. }
  911. int ff_alloc_packet(AVPacket *avpkt, int size)
  912. {
  913. return ff_alloc_packet2(NULL, avpkt, size);
  914. }
  915. /**
  916. * Pad last frame with silence.
  917. */
  918. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  919. {
  920. AVFrame *frame = NULL;
  921. uint8_t *buf = NULL;
  922. int ret;
  923. if (!(frame = avcodec_alloc_frame()))
  924. return AVERROR(ENOMEM);
  925. *frame = *src;
  926. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  927. s->frame_size, s->sample_fmt, 0)) < 0)
  928. goto fail;
  929. if (!(buf = av_malloc(ret))) {
  930. ret = AVERROR(ENOMEM);
  931. goto fail;
  932. }
  933. frame->nb_samples = s->frame_size;
  934. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  935. buf, ret, 0)) < 0)
  936. goto fail;
  937. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  938. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  939. goto fail;
  940. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  941. frame->nb_samples - src->nb_samples,
  942. s->channels, s->sample_fmt)) < 0)
  943. goto fail;
  944. *dst = frame;
  945. return 0;
  946. fail:
  947. if (frame->extended_data != frame->data)
  948. av_freep(&frame->extended_data);
  949. av_freep(&buf);
  950. av_freep(&frame);
  951. return ret;
  952. }
  953. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  954. AVPacket *avpkt,
  955. const AVFrame *frame,
  956. int *got_packet_ptr)
  957. {
  958. AVFrame tmp;
  959. AVFrame *padded_frame = NULL;
  960. int ret;
  961. AVPacket user_pkt = *avpkt;
  962. int needs_realloc = !user_pkt.data;
  963. *got_packet_ptr = 0;
  964. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  965. av_free_packet(avpkt);
  966. av_init_packet(avpkt);
  967. return 0;
  968. }
  969. /* ensure that extended_data is properly set */
  970. if (frame && !frame->extended_data) {
  971. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  972. avctx->channels > AV_NUM_DATA_POINTERS) {
  973. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  974. "with more than %d channels, but extended_data is not set.\n",
  975. AV_NUM_DATA_POINTERS);
  976. return AVERROR(EINVAL);
  977. }
  978. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  979. tmp = *frame;
  980. tmp.extended_data = tmp.data;
  981. frame = &tmp;
  982. }
  983. /* check for valid frame size */
  984. if (frame) {
  985. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  986. if (frame->nb_samples > avctx->frame_size) {
  987. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  988. return AVERROR(EINVAL);
  989. }
  990. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  991. if (frame->nb_samples < avctx->frame_size &&
  992. !avctx->internal->last_audio_frame) {
  993. ret = pad_last_frame(avctx, &padded_frame, frame);
  994. if (ret < 0)
  995. return ret;
  996. frame = padded_frame;
  997. avctx->internal->last_audio_frame = 1;
  998. }
  999. if (frame->nb_samples != avctx->frame_size) {
  1000. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1001. return AVERROR(EINVAL);
  1002. }
  1003. }
  1004. }
  1005. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1006. if (!ret) {
  1007. if (*got_packet_ptr) {
  1008. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1009. if (avpkt->pts == AV_NOPTS_VALUE)
  1010. avpkt->pts = frame->pts;
  1011. if (!avpkt->duration)
  1012. avpkt->duration = ff_samples_to_time_base(avctx,
  1013. frame->nb_samples);
  1014. }
  1015. avpkt->dts = avpkt->pts;
  1016. } else {
  1017. avpkt->size = 0;
  1018. }
  1019. }
  1020. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1021. needs_realloc = 0;
  1022. if (user_pkt.data) {
  1023. if (user_pkt.size >= avpkt->size) {
  1024. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1025. } else {
  1026. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1027. avpkt->size = user_pkt.size;
  1028. ret = -1;
  1029. }
  1030. avpkt->data = user_pkt.data;
  1031. avpkt->destruct = user_pkt.destruct;
  1032. } else {
  1033. if (av_dup_packet(avpkt) < 0) {
  1034. ret = AVERROR(ENOMEM);
  1035. }
  1036. }
  1037. }
  1038. if (!ret) {
  1039. if (needs_realloc && avpkt->data) {
  1040. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1041. if (new_data)
  1042. avpkt->data = new_data;
  1043. }
  1044. avctx->frame_number++;
  1045. }
  1046. if (ret < 0 || !*got_packet_ptr) {
  1047. av_free_packet(avpkt);
  1048. av_init_packet(avpkt);
  1049. return ret;
  1050. }
  1051. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1052. this needs to be moved to the encoders, but for now we can do it
  1053. here to simplify things */
  1054. avpkt->flags |= AV_PKT_FLAG_KEY;
  1055. if (padded_frame) {
  1056. av_freep(&padded_frame->data[0]);
  1057. if (padded_frame->extended_data != padded_frame->data)
  1058. av_freep(&padded_frame->extended_data);
  1059. av_freep(&padded_frame);
  1060. }
  1061. return ret;
  1062. }
  1063. #if FF_API_OLD_DECODE_AUDIO
  1064. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1065. uint8_t *buf, int buf_size,
  1066. const short *samples)
  1067. {
  1068. AVPacket pkt;
  1069. AVFrame frame0;
  1070. AVFrame *frame;
  1071. int ret, samples_size, got_packet;
  1072. av_init_packet(&pkt);
  1073. pkt.data = buf;
  1074. pkt.size = buf_size;
  1075. if (samples) {
  1076. frame = &frame0;
  1077. avcodec_get_frame_defaults(frame);
  1078. if (avctx->frame_size) {
  1079. frame->nb_samples = avctx->frame_size;
  1080. } else {
  1081. /* if frame_size is not set, the number of samples must be
  1082. calculated from the buffer size */
  1083. int64_t nb_samples;
  1084. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1085. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1086. "support this codec\n");
  1087. return AVERROR(EINVAL);
  1088. }
  1089. nb_samples = (int64_t)buf_size * 8 /
  1090. (av_get_bits_per_sample(avctx->codec_id) *
  1091. avctx->channels);
  1092. if (nb_samples >= INT_MAX)
  1093. return AVERROR(EINVAL);
  1094. frame->nb_samples = nb_samples;
  1095. }
  1096. /* it is assumed that the samples buffer is large enough based on the
  1097. relevant parameters */
  1098. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1099. frame->nb_samples,
  1100. avctx->sample_fmt, 1);
  1101. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1102. avctx->sample_fmt,
  1103. (const uint8_t *) samples,
  1104. samples_size, 1)))
  1105. return ret;
  1106. /* fabricate frame pts from sample count.
  1107. this is needed because the avcodec_encode_audio() API does not have
  1108. a way for the user to provide pts */
  1109. if(avctx->sample_rate && avctx->time_base.num)
  1110. frame->pts = ff_samples_to_time_base(avctx,
  1111. avctx->internal->sample_count);
  1112. else
  1113. frame->pts = AV_NOPTS_VALUE;
  1114. avctx->internal->sample_count += frame->nb_samples;
  1115. } else {
  1116. frame = NULL;
  1117. }
  1118. got_packet = 0;
  1119. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1120. if (!ret && got_packet && avctx->coded_frame) {
  1121. avctx->coded_frame->pts = pkt.pts;
  1122. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1123. }
  1124. /* free any side data since we cannot return it */
  1125. ff_packet_free_side_data(&pkt);
  1126. if (frame && frame->extended_data != frame->data)
  1127. av_freep(&frame->extended_data);
  1128. return ret ? ret : pkt.size;
  1129. }
  1130. #endif
  1131. #if FF_API_OLD_ENCODE_VIDEO
  1132. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1133. const AVFrame *pict)
  1134. {
  1135. AVPacket pkt;
  1136. int ret, got_packet = 0;
  1137. if(buf_size < FF_MIN_BUFFER_SIZE){
  1138. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1139. return -1;
  1140. }
  1141. av_init_packet(&pkt);
  1142. pkt.data = buf;
  1143. pkt.size = buf_size;
  1144. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1145. if (!ret && got_packet && avctx->coded_frame) {
  1146. avctx->coded_frame->pts = pkt.pts;
  1147. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1148. }
  1149. /* free any side data since we cannot return it */
  1150. if (pkt.side_data_elems > 0) {
  1151. int i;
  1152. for (i = 0; i < pkt.side_data_elems; i++)
  1153. av_free(pkt.side_data[i].data);
  1154. av_freep(&pkt.side_data);
  1155. pkt.side_data_elems = 0;
  1156. }
  1157. return ret ? ret : pkt.size;
  1158. }
  1159. #endif
  1160. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1161. AVPacket *avpkt,
  1162. const AVFrame *frame,
  1163. int *got_packet_ptr)
  1164. {
  1165. int ret;
  1166. AVPacket user_pkt = *avpkt;
  1167. int needs_realloc = !user_pkt.data;
  1168. *got_packet_ptr = 0;
  1169. if(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1170. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1171. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1172. av_free_packet(avpkt);
  1173. av_init_packet(avpkt);
  1174. avpkt->size = 0;
  1175. return 0;
  1176. }
  1177. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1178. return AVERROR(EINVAL);
  1179. av_assert0(avctx->codec->encode2);
  1180. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1181. av_assert0(ret <= 0);
  1182. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1183. needs_realloc = 0;
  1184. if (user_pkt.data) {
  1185. if (user_pkt.size >= avpkt->size) {
  1186. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1187. } else {
  1188. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1189. avpkt->size = user_pkt.size;
  1190. ret = -1;
  1191. }
  1192. avpkt->data = user_pkt.data;
  1193. avpkt->destruct = user_pkt.destruct;
  1194. } else {
  1195. if (av_dup_packet(avpkt) < 0) {
  1196. ret = AVERROR(ENOMEM);
  1197. }
  1198. }
  1199. }
  1200. if (!ret) {
  1201. if (!*got_packet_ptr)
  1202. avpkt->size = 0;
  1203. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1204. avpkt->pts = avpkt->dts = frame->pts;
  1205. if (needs_realloc && avpkt->data &&
  1206. avpkt->destruct == av_destruct_packet) {
  1207. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1208. if (new_data)
  1209. avpkt->data = new_data;
  1210. }
  1211. avctx->frame_number++;
  1212. }
  1213. if (ret < 0 || !*got_packet_ptr)
  1214. av_free_packet(avpkt);
  1215. emms_c();
  1216. return ret;
  1217. }
  1218. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1219. const AVSubtitle *sub)
  1220. {
  1221. int ret;
  1222. if(sub->start_display_time) {
  1223. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1224. return -1;
  1225. }
  1226. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
  1227. avctx->frame_number++;
  1228. return ret;
  1229. }
  1230. /**
  1231. * Attempt to guess proper monotonic timestamps for decoded video frames
  1232. * which might have incorrect times. Input timestamps may wrap around, in
  1233. * which case the output will as well.
  1234. *
  1235. * @param pts the pts field of the decoded AVPacket, as passed through
  1236. * AVFrame.pkt_pts
  1237. * @param dts the dts field of the decoded AVPacket
  1238. * @return one of the input values, may be AV_NOPTS_VALUE
  1239. */
  1240. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1241. int64_t reordered_pts, int64_t dts)
  1242. {
  1243. int64_t pts = AV_NOPTS_VALUE;
  1244. if (dts != AV_NOPTS_VALUE) {
  1245. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1246. ctx->pts_correction_last_dts = dts;
  1247. }
  1248. if (reordered_pts != AV_NOPTS_VALUE) {
  1249. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1250. ctx->pts_correction_last_pts = reordered_pts;
  1251. }
  1252. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1253. && reordered_pts != AV_NOPTS_VALUE)
  1254. pts = reordered_pts;
  1255. else
  1256. pts = dts;
  1257. return pts;
  1258. }
  1259. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1260. {
  1261. int size = 0;
  1262. const uint8_t *data;
  1263. uint32_t flags;
  1264. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1265. return;
  1266. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1267. if (!data || size < 4)
  1268. return;
  1269. flags = bytestream_get_le32(&data);
  1270. size -= 4;
  1271. if (size < 4) /* Required for any of the changes */
  1272. return;
  1273. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1274. avctx->channels = bytestream_get_le32(&data);
  1275. size -= 4;
  1276. }
  1277. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1278. if (size < 8)
  1279. return;
  1280. avctx->channel_layout = bytestream_get_le64(&data);
  1281. size -= 8;
  1282. }
  1283. if (size < 4)
  1284. return;
  1285. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1286. avctx->sample_rate = bytestream_get_le32(&data);
  1287. size -= 4;
  1288. }
  1289. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1290. if (size < 8)
  1291. return;
  1292. avctx->width = bytestream_get_le32(&data);
  1293. avctx->height = bytestream_get_le32(&data);
  1294. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1295. size -= 8;
  1296. }
  1297. }
  1298. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1299. int *got_picture_ptr,
  1300. const AVPacket *avpkt)
  1301. {
  1302. int ret;
  1303. // copy to ensure we do not change avpkt
  1304. AVPacket tmp = *avpkt;
  1305. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1306. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1307. return AVERROR(EINVAL);
  1308. }
  1309. *got_picture_ptr= 0;
  1310. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1311. return AVERROR(EINVAL);
  1312. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1313. int did_split = av_packet_split_side_data(&tmp);
  1314. apply_param_change(avctx, &tmp);
  1315. avctx->pkt = &tmp;
  1316. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1317. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1318. &tmp);
  1319. else {
  1320. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1321. &tmp);
  1322. picture->pkt_dts= avpkt->dts;
  1323. if(!avctx->has_b_frames){
  1324. picture->pkt_pos= avpkt->pos;
  1325. }
  1326. //FIXME these should be under if(!avctx->has_b_frames)
  1327. if (!picture->sample_aspect_ratio.num)
  1328. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1329. if (!picture->width)
  1330. picture->width = avctx->width;
  1331. if (!picture->height)
  1332. picture->height = avctx->height;
  1333. if (picture->format == PIX_FMT_NONE)
  1334. picture->format = avctx->pix_fmt;
  1335. }
  1336. emms_c(); //needed to avoid an emms_c() call before every return;
  1337. avctx->pkt = NULL;
  1338. if (did_split) {
  1339. ff_packet_free_side_data(&tmp);
  1340. if(ret == tmp.size)
  1341. ret = avpkt->size;
  1342. }
  1343. if (*got_picture_ptr){
  1344. avctx->frame_number++;
  1345. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1346. picture->pkt_pts,
  1347. picture->pkt_dts);
  1348. }
  1349. }else
  1350. ret= 0;
  1351. return ret;
  1352. }
  1353. #if FF_API_OLD_DECODE_AUDIO
  1354. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1355. int *frame_size_ptr,
  1356. AVPacket *avpkt)
  1357. {
  1358. AVFrame frame;
  1359. int ret, got_frame = 0;
  1360. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1361. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1362. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1363. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1364. "avcodec_decode_audio4()\n");
  1365. avctx->get_buffer = avcodec_default_get_buffer;
  1366. avctx->release_buffer = avcodec_default_release_buffer;
  1367. }
  1368. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1369. if (ret >= 0 && got_frame) {
  1370. int ch, plane_size;
  1371. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1372. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1373. frame.nb_samples,
  1374. avctx->sample_fmt, 1);
  1375. if (*frame_size_ptr < data_size) {
  1376. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1377. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1378. return AVERROR(EINVAL);
  1379. }
  1380. memcpy(samples, frame.extended_data[0], plane_size);
  1381. if (planar && avctx->channels > 1) {
  1382. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1383. for (ch = 1; ch < avctx->channels; ch++) {
  1384. memcpy(out, frame.extended_data[ch], plane_size);
  1385. out += plane_size;
  1386. }
  1387. }
  1388. *frame_size_ptr = data_size;
  1389. } else {
  1390. *frame_size_ptr = 0;
  1391. }
  1392. return ret;
  1393. }
  1394. #endif
  1395. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1396. AVFrame *frame,
  1397. int *got_frame_ptr,
  1398. const AVPacket *avpkt)
  1399. {
  1400. int ret = 0;
  1401. *got_frame_ptr = 0;
  1402. if (!avpkt->data && avpkt->size) {
  1403. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1404. return AVERROR(EINVAL);
  1405. }
  1406. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1407. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1408. return AVERROR(EINVAL);
  1409. }
  1410. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1411. // copy to ensure we do not change avpkt
  1412. AVPacket tmp = *avpkt;
  1413. int did_split = av_packet_split_side_data(&tmp);
  1414. apply_param_change(avctx, &tmp);
  1415. avctx->pkt = &tmp;
  1416. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1417. if (ret >= 0 && *got_frame_ptr) {
  1418. avctx->frame_number++;
  1419. frame->pkt_dts = avpkt->dts;
  1420. if (frame->format == AV_SAMPLE_FMT_NONE)
  1421. frame->format = avctx->sample_fmt;
  1422. if (!frame->channel_layout)
  1423. frame->channel_layout = avctx->channel_layout;
  1424. if (!frame->sample_rate)
  1425. frame->sample_rate = avctx->sample_rate;
  1426. }
  1427. avctx->pkt = NULL;
  1428. if (did_split) {
  1429. ff_packet_free_side_data(&tmp);
  1430. if(ret == tmp.size)
  1431. ret = avpkt->size;
  1432. }
  1433. }
  1434. return ret;
  1435. }
  1436. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1437. int *got_sub_ptr,
  1438. AVPacket *avpkt)
  1439. {
  1440. int ret;
  1441. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1442. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1443. return AVERROR(EINVAL);
  1444. }
  1445. avctx->pkt = avpkt;
  1446. *got_sub_ptr = 0;
  1447. avcodec_get_subtitle_defaults(sub);
  1448. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1449. if (*got_sub_ptr)
  1450. avctx->frame_number++;
  1451. return ret;
  1452. }
  1453. void avsubtitle_free(AVSubtitle *sub)
  1454. {
  1455. int i;
  1456. for (i = 0; i < sub->num_rects; i++)
  1457. {
  1458. av_freep(&sub->rects[i]->pict.data[0]);
  1459. av_freep(&sub->rects[i]->pict.data[1]);
  1460. av_freep(&sub->rects[i]->pict.data[2]);
  1461. av_freep(&sub->rects[i]->pict.data[3]);
  1462. av_freep(&sub->rects[i]->text);
  1463. av_freep(&sub->rects[i]->ass);
  1464. av_freep(&sub->rects[i]);
  1465. }
  1466. av_freep(&sub->rects);
  1467. memset(sub, 0, sizeof(AVSubtitle));
  1468. }
  1469. av_cold int avcodec_close(AVCodecContext *avctx)
  1470. {
  1471. /* If there is a user-supplied mutex locking routine, call it. */
  1472. if (ff_lockmgr_cb) {
  1473. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1474. return -1;
  1475. }
  1476. entangled_thread_counter++;
  1477. if(entangled_thread_counter != 1){
  1478. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1479. entangled_thread_counter--;
  1480. return -1;
  1481. }
  1482. if (avcodec_is_open(avctx)) {
  1483. if (avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1484. entangled_thread_counter --;
  1485. ff_frame_thread_encoder_free(avctx);
  1486. entangled_thread_counter ++;
  1487. }
  1488. if (HAVE_THREADS && avctx->thread_opaque)
  1489. ff_thread_free(avctx);
  1490. if (avctx->codec && avctx->codec->close)
  1491. avctx->codec->close(avctx);
  1492. avcodec_default_free_buffers(avctx);
  1493. avctx->coded_frame = NULL;
  1494. avctx->internal->byte_buffer_size = 0;
  1495. av_freep(&avctx->internal->byte_buffer);
  1496. av_freep(&avctx->internal);
  1497. }
  1498. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1499. av_opt_free(avctx->priv_data);
  1500. av_opt_free(avctx);
  1501. av_freep(&avctx->priv_data);
  1502. if (av_codec_is_encoder(avctx->codec))
  1503. av_freep(&avctx->extradata);
  1504. avctx->codec = NULL;
  1505. avctx->active_thread_type = 0;
  1506. entangled_thread_counter--;
  1507. /* Release any user-supplied mutex. */
  1508. if (ff_lockmgr_cb) {
  1509. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1510. }
  1511. return 0;
  1512. }
  1513. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1514. {
  1515. switch(id){
  1516. //This is for future deprecatec codec ids, its empty since
  1517. //last major bump but will fill up again over time, please don't remove it
  1518. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1519. default : return id;
  1520. }
  1521. }
  1522. AVCodec *avcodec_find_encoder(enum CodecID id)
  1523. {
  1524. AVCodec *p, *experimental=NULL;
  1525. p = first_avcodec;
  1526. id= remap_deprecated_codec_id(id);
  1527. while (p) {
  1528. if (av_codec_is_encoder(p) && p->id == id) {
  1529. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1530. experimental = p;
  1531. } else
  1532. return p;
  1533. }
  1534. p = p->next;
  1535. }
  1536. return experimental;
  1537. }
  1538. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1539. {
  1540. AVCodec *p;
  1541. if (!name)
  1542. return NULL;
  1543. p = first_avcodec;
  1544. while (p) {
  1545. if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1546. return p;
  1547. p = p->next;
  1548. }
  1549. return NULL;
  1550. }
  1551. AVCodec *avcodec_find_decoder(enum CodecID id)
  1552. {
  1553. AVCodec *p, *experimental=NULL;
  1554. p = first_avcodec;
  1555. id= remap_deprecated_codec_id(id);
  1556. while (p) {
  1557. if (av_codec_is_decoder(p) && p->id == id) {
  1558. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1559. experimental = p;
  1560. } else
  1561. return p;
  1562. }
  1563. p = p->next;
  1564. }
  1565. return experimental;
  1566. }
  1567. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1568. {
  1569. AVCodec *p;
  1570. if (!name)
  1571. return NULL;
  1572. p = first_avcodec;
  1573. while (p) {
  1574. if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1575. return p;
  1576. p = p->next;
  1577. }
  1578. return NULL;
  1579. }
  1580. const char *avcodec_get_name(enum CodecID id)
  1581. {
  1582. AVCodec *codec;
  1583. #if !CONFIG_SMALL
  1584. switch (id) {
  1585. #include "libavcodec/codec_names.h"
  1586. }
  1587. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1588. #endif
  1589. codec = avcodec_find_decoder(id);
  1590. if (codec)
  1591. return codec->name;
  1592. codec = avcodec_find_encoder(id);
  1593. if (codec)
  1594. return codec->name;
  1595. return "unknown_codec";
  1596. }
  1597. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1598. {
  1599. int i, len, ret = 0;
  1600. #define IS_PRINT(x) \
  1601. (((x) >= '0' && (x) <= '9') || \
  1602. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1603. ((x) == '.' || (x) == ' ' || (x) == '-'))
  1604. for (i = 0; i < 4; i++) {
  1605. len = snprintf(buf, buf_size,
  1606. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1607. buf += len;
  1608. buf_size = buf_size > len ? buf_size - len : 0;
  1609. ret += len;
  1610. codec_tag>>=8;
  1611. }
  1612. return ret;
  1613. }
  1614. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1615. {
  1616. const char *codec_type;
  1617. const char *codec_name;
  1618. const char *profile = NULL;
  1619. AVCodec *p;
  1620. int bitrate;
  1621. AVRational display_aspect_ratio;
  1622. if (!buf || buf_size <= 0)
  1623. return;
  1624. codec_type = av_get_media_type_string(enc->codec_type);
  1625. codec_name = avcodec_get_name(enc->codec_id);
  1626. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1627. if (enc->codec)
  1628. p = enc->codec;
  1629. else
  1630. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1631. avcodec_find_decoder(enc->codec_id);
  1632. if (p)
  1633. profile = av_get_profile_name(p, enc->profile);
  1634. }
  1635. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1636. codec_name, enc->mb_decision ? " (hq)" : "");
  1637. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1638. if (profile)
  1639. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1640. if (enc->codec_tag) {
  1641. char tag_buf[32];
  1642. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1643. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1644. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1645. }
  1646. switch(enc->codec_type) {
  1647. case AVMEDIA_TYPE_VIDEO:
  1648. if (enc->pix_fmt != PIX_FMT_NONE) {
  1649. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1650. ", %s",
  1651. av_get_pix_fmt_name(enc->pix_fmt));
  1652. }
  1653. if (enc->width) {
  1654. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1655. ", %dx%d",
  1656. enc->width, enc->height);
  1657. if (enc->sample_aspect_ratio.num) {
  1658. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1659. enc->width*enc->sample_aspect_ratio.num,
  1660. enc->height*enc->sample_aspect_ratio.den,
  1661. 1024*1024);
  1662. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1663. " [SAR %d:%d DAR %d:%d]",
  1664. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1665. display_aspect_ratio.num, display_aspect_ratio.den);
  1666. }
  1667. if(av_log_get_level() >= AV_LOG_DEBUG){
  1668. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1669. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1670. ", %d/%d",
  1671. enc->time_base.num/g, enc->time_base.den/g);
  1672. }
  1673. }
  1674. if (encode) {
  1675. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1676. ", q=%d-%d", enc->qmin, enc->qmax);
  1677. }
  1678. break;
  1679. case AVMEDIA_TYPE_AUDIO:
  1680. if (enc->sample_rate) {
  1681. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1682. ", %d Hz", enc->sample_rate);
  1683. }
  1684. av_strlcat(buf, ", ", buf_size);
  1685. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1686. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1687. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1688. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1689. }
  1690. break;
  1691. default:
  1692. return;
  1693. }
  1694. if (encode) {
  1695. if (enc->flags & CODEC_FLAG_PASS1)
  1696. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1697. ", pass 1");
  1698. if (enc->flags & CODEC_FLAG_PASS2)
  1699. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1700. ", pass 2");
  1701. }
  1702. bitrate = get_bit_rate(enc);
  1703. if (bitrate != 0) {
  1704. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1705. ", %d kb/s", bitrate / 1000);
  1706. }
  1707. }
  1708. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1709. {
  1710. const AVProfile *p;
  1711. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1712. return NULL;
  1713. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1714. if (p->profile == profile)
  1715. return p->name;
  1716. return NULL;
  1717. }
  1718. unsigned avcodec_version( void )
  1719. {
  1720. // av_assert0(CODEC_ID_V410==164);
  1721. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1722. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1723. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1724. av_assert0(CODEC_ID_SRT==94216);
  1725. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1726. return LIBAVCODEC_VERSION_INT;
  1727. }
  1728. const char *avcodec_configuration(void)
  1729. {
  1730. return FFMPEG_CONFIGURATION;
  1731. }
  1732. const char *avcodec_license(void)
  1733. {
  1734. #define LICENSE_PREFIX "libavcodec license: "
  1735. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1736. }
  1737. void avcodec_flush_buffers(AVCodecContext *avctx)
  1738. {
  1739. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1740. ff_thread_flush(avctx);
  1741. else if(avctx->codec->flush)
  1742. avctx->codec->flush(avctx);
  1743. avctx->pts_correction_last_pts =
  1744. avctx->pts_correction_last_dts = INT64_MIN;
  1745. }
  1746. static void video_free_buffers(AVCodecContext *s)
  1747. {
  1748. AVCodecInternal *avci = s->internal;
  1749. int i, j;
  1750. if (!avci->buffer)
  1751. return;
  1752. if (avci->buffer_count)
  1753. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1754. avci->buffer_count);
  1755. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1756. InternalBuffer *buf = &avci->buffer[i];
  1757. for(j=0; j<4; j++){
  1758. av_freep(&buf->base[j]);
  1759. buf->data[j]= NULL;
  1760. }
  1761. }
  1762. av_freep(&avci->buffer);
  1763. avci->buffer_count=0;
  1764. }
  1765. static void audio_free_buffers(AVCodecContext *avctx)
  1766. {
  1767. AVCodecInternal *avci = avctx->internal;
  1768. InternalBuffer *buf;
  1769. if (!avci->buffer)
  1770. return;
  1771. buf = avci->buffer;
  1772. if (buf->extended_data) {
  1773. av_free(buf->extended_data[0]);
  1774. if (buf->extended_data != buf->data)
  1775. av_freep(&buf->extended_data);
  1776. }
  1777. av_freep(&avci->buffer);
  1778. }
  1779. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1780. {
  1781. switch (avctx->codec_type) {
  1782. case AVMEDIA_TYPE_VIDEO:
  1783. video_free_buffers(avctx);
  1784. break;
  1785. case AVMEDIA_TYPE_AUDIO:
  1786. audio_free_buffers(avctx);
  1787. break;
  1788. default:
  1789. break;
  1790. }
  1791. }
  1792. int av_get_exact_bits_per_sample(enum CodecID codec_id)
  1793. {
  1794. switch(codec_id){
  1795. case CODEC_ID_ADPCM_CT:
  1796. case CODEC_ID_ADPCM_IMA_APC:
  1797. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1798. case CODEC_ID_ADPCM_IMA_WS:
  1799. case CODEC_ID_ADPCM_G722:
  1800. case CODEC_ID_ADPCM_YAMAHA:
  1801. return 4;
  1802. case CODEC_ID_PCM_ALAW:
  1803. case CODEC_ID_PCM_MULAW:
  1804. case CODEC_ID_PCM_S8:
  1805. case CODEC_ID_PCM_U8:
  1806. case CODEC_ID_PCM_ZORK:
  1807. return 8;
  1808. case CODEC_ID_PCM_S16BE:
  1809. case CODEC_ID_PCM_S16LE:
  1810. case CODEC_ID_PCM_S16LE_PLANAR:
  1811. case CODEC_ID_PCM_U16BE:
  1812. case CODEC_ID_PCM_U16LE:
  1813. return 16;
  1814. case CODEC_ID_PCM_S24DAUD:
  1815. case CODEC_ID_PCM_S24BE:
  1816. case CODEC_ID_PCM_S24LE:
  1817. case CODEC_ID_PCM_U24BE:
  1818. case CODEC_ID_PCM_U24LE:
  1819. return 24;
  1820. case CODEC_ID_PCM_S32BE:
  1821. case CODEC_ID_PCM_S32LE:
  1822. case CODEC_ID_PCM_U32BE:
  1823. case CODEC_ID_PCM_U32LE:
  1824. case CODEC_ID_PCM_F32BE:
  1825. case CODEC_ID_PCM_F32LE:
  1826. return 32;
  1827. case CODEC_ID_PCM_F64BE:
  1828. case CODEC_ID_PCM_F64LE:
  1829. return 64;
  1830. default:
  1831. return 0;
  1832. }
  1833. }
  1834. enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1835. {
  1836. static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
  1837. [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1838. [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1839. [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1840. [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1841. [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1842. [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1843. [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1844. [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1845. [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1846. [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1847. };
  1848. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1849. return CODEC_ID_NONE;
  1850. if (be < 0 || be > 1)
  1851. be = AV_NE(1, 0);
  1852. return map[fmt][be];
  1853. }
  1854. int av_get_bits_per_sample(enum CodecID codec_id)
  1855. {
  1856. switch (codec_id) {
  1857. case CODEC_ID_ADPCM_SBPRO_2:
  1858. return 2;
  1859. case CODEC_ID_ADPCM_SBPRO_3:
  1860. return 3;
  1861. case CODEC_ID_ADPCM_SBPRO_4:
  1862. case CODEC_ID_ADPCM_IMA_WAV:
  1863. case CODEC_ID_ADPCM_IMA_QT:
  1864. case CODEC_ID_ADPCM_SWF:
  1865. case CODEC_ID_ADPCM_MS:
  1866. return 4;
  1867. default:
  1868. return av_get_exact_bits_per_sample(codec_id);
  1869. }
  1870. }
  1871. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1872. {
  1873. int id, sr, ch, ba, tag, bps;
  1874. id = avctx->codec_id;
  1875. sr = avctx->sample_rate;
  1876. ch = avctx->channels;
  1877. ba = avctx->block_align;
  1878. tag = avctx->codec_tag;
  1879. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1880. /* codecs with an exact constant bits per sample */
  1881. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1882. return (frame_bytes * 8LL) / (bps * ch);
  1883. bps = avctx->bits_per_coded_sample;
  1884. /* codecs with a fixed packet duration */
  1885. switch (id) {
  1886. case CODEC_ID_ADPCM_ADX: return 32;
  1887. case CODEC_ID_ADPCM_IMA_QT: return 64;
  1888. case CODEC_ID_ADPCM_EA_XAS: return 128;
  1889. case CODEC_ID_AMR_NB:
  1890. case CODEC_ID_GSM:
  1891. case CODEC_ID_QCELP:
  1892. case CODEC_ID_RA_144:
  1893. case CODEC_ID_RA_288: return 160;
  1894. case CODEC_ID_IMC: return 256;
  1895. case CODEC_ID_AMR_WB:
  1896. case CODEC_ID_GSM_MS: return 320;
  1897. case CODEC_ID_MP1: return 384;
  1898. case CODEC_ID_ATRAC1: return 512;
  1899. case CODEC_ID_ATRAC3: return 1024;
  1900. case CODEC_ID_MP2:
  1901. case CODEC_ID_MUSEPACK7: return 1152;
  1902. case CODEC_ID_AC3: return 1536;
  1903. }
  1904. if (sr > 0) {
  1905. /* calc from sample rate */
  1906. if (id == CODEC_ID_TTA)
  1907. return 256 * sr / 245;
  1908. if (ch > 0) {
  1909. /* calc from sample rate and channels */
  1910. if (id == CODEC_ID_BINKAUDIO_DCT)
  1911. return (480 << (sr / 22050)) / ch;
  1912. }
  1913. }
  1914. if (ba > 0) {
  1915. /* calc from block_align */
  1916. if (id == CODEC_ID_SIPR) {
  1917. switch (ba) {
  1918. case 20: return 160;
  1919. case 19: return 144;
  1920. case 29: return 288;
  1921. case 37: return 480;
  1922. }
  1923. } else if (id == CODEC_ID_ILBC) {
  1924. switch (ba) {
  1925. case 38: return 160;
  1926. case 50: return 240;
  1927. }
  1928. }
  1929. }
  1930. if (frame_bytes > 0) {
  1931. /* calc from frame_bytes only */
  1932. if (id == CODEC_ID_TRUESPEECH)
  1933. return 240 * (frame_bytes / 32);
  1934. if (id == CODEC_ID_NELLYMOSER)
  1935. return 256 * (frame_bytes / 64);
  1936. if (bps > 0) {
  1937. /* calc from frame_bytes and bits_per_coded_sample */
  1938. if (id == CODEC_ID_ADPCM_G726)
  1939. return frame_bytes * 8 / bps;
  1940. }
  1941. if (ch > 0) {
  1942. /* calc from frame_bytes and channels */
  1943. switch (id) {
  1944. case CODEC_ID_ADPCM_4XM:
  1945. case CODEC_ID_ADPCM_IMA_ISS:
  1946. return (frame_bytes - 4 * ch) * 2 / ch;
  1947. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1948. return (frame_bytes - 4) * 2 / ch;
  1949. case CODEC_ID_ADPCM_IMA_AMV:
  1950. return (frame_bytes - 8) * 2 / ch;
  1951. case CODEC_ID_ADPCM_XA:
  1952. return (frame_bytes / 128) * 224 / ch;
  1953. case CODEC_ID_INTERPLAY_DPCM:
  1954. return (frame_bytes - 6 - ch) / ch;
  1955. case CODEC_ID_ROQ_DPCM:
  1956. return (frame_bytes - 8) / ch;
  1957. case CODEC_ID_XAN_DPCM:
  1958. return (frame_bytes - 2 * ch) / ch;
  1959. case CODEC_ID_MACE3:
  1960. return 3 * frame_bytes / ch;
  1961. case CODEC_ID_MACE6:
  1962. return 6 * frame_bytes / ch;
  1963. case CODEC_ID_PCM_LXF:
  1964. return 2 * (frame_bytes / (5 * ch));
  1965. }
  1966. if (tag) {
  1967. /* calc from frame_bytes, channels, and codec_tag */
  1968. if (id == CODEC_ID_SOL_DPCM) {
  1969. if (tag == 3)
  1970. return frame_bytes / ch;
  1971. else
  1972. return frame_bytes * 2 / ch;
  1973. }
  1974. }
  1975. if (ba > 0) {
  1976. /* calc from frame_bytes, channels, and block_align */
  1977. int blocks = frame_bytes / ba;
  1978. switch (avctx->codec_id) {
  1979. case CODEC_ID_ADPCM_IMA_WAV:
  1980. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1981. case CODEC_ID_ADPCM_IMA_DK3:
  1982. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1983. case CODEC_ID_ADPCM_IMA_DK4:
  1984. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1985. case CODEC_ID_ADPCM_MS:
  1986. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1987. }
  1988. }
  1989. if (bps > 0) {
  1990. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1991. switch (avctx->codec_id) {
  1992. case CODEC_ID_PCM_DVD:
  1993. if(bps<4)
  1994. return 0;
  1995. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1996. case CODEC_ID_PCM_BLURAY:
  1997. if(bps<4)
  1998. return 0;
  1999. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2000. case CODEC_ID_S302M:
  2001. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2002. }
  2003. }
  2004. }
  2005. }
  2006. return 0;
  2007. }
  2008. #if !HAVE_THREADS
  2009. int ff_thread_init(AVCodecContext *s){
  2010. return -1;
  2011. }
  2012. #endif
  2013. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2014. {
  2015. unsigned int n = 0;
  2016. while(v >= 0xff) {
  2017. *s++ = 0xff;
  2018. v -= 0xff;
  2019. n++;
  2020. }
  2021. *s = v;
  2022. n++;
  2023. return n;
  2024. }
  2025. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  2026. int i;
  2027. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  2028. return i;
  2029. }
  2030. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2031. {
  2032. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  2033. "version to the newest one from Git. If the problem still "
  2034. "occurs, it means that your file has a feature which has not "
  2035. "been implemented.\n", feature);
  2036. if(want_sample)
  2037. av_log_ask_for_sample(avc, NULL);
  2038. }
  2039. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2040. {
  2041. va_list argument_list;
  2042. va_start(argument_list, msg);
  2043. if (msg)
  2044. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2045. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2046. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2047. "and contact the ffmpeg-devel mailing list.\n");
  2048. va_end(argument_list);
  2049. }
  2050. static AVHWAccel *first_hwaccel = NULL;
  2051. void av_register_hwaccel(AVHWAccel *hwaccel)
  2052. {
  2053. AVHWAccel **p = &first_hwaccel;
  2054. while (*p)
  2055. p = &(*p)->next;
  2056. *p = hwaccel;
  2057. hwaccel->next = NULL;
  2058. }
  2059. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2060. {
  2061. return hwaccel ? hwaccel->next : first_hwaccel;
  2062. }
  2063. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  2064. {
  2065. AVHWAccel *hwaccel=NULL;
  2066. while((hwaccel= av_hwaccel_next(hwaccel))){
  2067. if ( hwaccel->id == codec_id
  2068. && hwaccel->pix_fmt == pix_fmt)
  2069. return hwaccel;
  2070. }
  2071. return NULL;
  2072. }
  2073. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2074. {
  2075. if (ff_lockmgr_cb) {
  2076. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2077. return -1;
  2078. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2079. return -1;
  2080. }
  2081. ff_lockmgr_cb = cb;
  2082. if (ff_lockmgr_cb) {
  2083. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2084. return -1;
  2085. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2086. return -1;
  2087. }
  2088. return 0;
  2089. }
  2090. int avpriv_lock_avformat(void)
  2091. {
  2092. if (ff_lockmgr_cb) {
  2093. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2094. return -1;
  2095. }
  2096. return 0;
  2097. }
  2098. int avpriv_unlock_avformat(void)
  2099. {
  2100. if (ff_lockmgr_cb) {
  2101. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2102. return -1;
  2103. }
  2104. return 0;
  2105. }
  2106. unsigned int avpriv_toupper4(unsigned int x)
  2107. {
  2108. return toupper( x &0xFF)
  2109. + (toupper((x>>8 )&0xFF)<<8 )
  2110. + (toupper((x>>16)&0xFF)<<16)
  2111. + (toupper((x>>24)&0xFF)<<24);
  2112. }
  2113. #if !HAVE_THREADS
  2114. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2115. {
  2116. f->owner = avctx;
  2117. ff_init_buffer_info(avctx, f);
  2118. return avctx->get_buffer(avctx, f);
  2119. }
  2120. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2121. {
  2122. f->owner->release_buffer(f->owner, f);
  2123. }
  2124. void ff_thread_finish_setup(AVCodecContext *avctx)
  2125. {
  2126. }
  2127. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2128. {
  2129. }
  2130. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2131. {
  2132. }
  2133. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2134. {
  2135. return 1;
  2136. }
  2137. #endif
  2138. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  2139. {
  2140. AVCodec *c= avcodec_find_decoder(codec_id);
  2141. if(!c)
  2142. c= avcodec_find_encoder(codec_id);
  2143. if(c)
  2144. return c->type;
  2145. if (codec_id <= CODEC_ID_NONE)
  2146. return AVMEDIA_TYPE_UNKNOWN;
  2147. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  2148. return AVMEDIA_TYPE_VIDEO;
  2149. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  2150. return AVMEDIA_TYPE_AUDIO;
  2151. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  2152. return AVMEDIA_TYPE_SUBTITLE;
  2153. return AVMEDIA_TYPE_UNKNOWN;
  2154. }
  2155. int avcodec_is_open(AVCodecContext *s)
  2156. {
  2157. return !!s->internal;
  2158. }