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.

2036 lines
63KB

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