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.

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