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.

2503 lines
79KB

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