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.

2500 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 == CODEC_ID_SVQ1){
  212. w_align=64;
  213. h_align=64;
  214. }
  215. case PIX_FMT_RGB555:
  216. if(s->codec_id == CODEC_ID_RPZA){
  217. w_align=4;
  218. h_align=4;
  219. }
  220. case PIX_FMT_PAL8:
  221. case PIX_FMT_BGR8:
  222. case PIX_FMT_RGB8:
  223. if(s->codec_id == CODEC_ID_SMC){
  224. w_align=4;
  225. h_align=4;
  226. }
  227. break;
  228. case PIX_FMT_BGR24:
  229. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  230. w_align=4;
  231. h_align=4;
  232. }
  233. break;
  234. default:
  235. w_align= 1;
  236. h_align= 1;
  237. break;
  238. }
  239. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  240. w_align= FFMAX(w_align, 8);
  241. }
  242. *width = FFALIGN(*width , w_align);
  243. *height= FFALIGN(*height, h_align);
  244. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  245. *height+=2; // some of the optimized chroma MC reads one line too much
  246. // which is also done in mpeg decoders with lowres > 0
  247. for (i = 0; i < 4; i++)
  248. linesize_align[i] = STRIDE_ALIGN;
  249. }
  250. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  251. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  252. int linesize_align[AV_NUM_DATA_POINTERS];
  253. int align;
  254. avcodec_align_dimensions2(s, width, height, linesize_align);
  255. align = FFMAX(linesize_align[0], linesize_align[3]);
  256. linesize_align[1] <<= chroma_shift;
  257. linesize_align[2] <<= chroma_shift;
  258. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  259. *width=FFALIGN(*width, align);
  260. }
  261. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  262. {
  263. if (s->pkt) {
  264. pic->pkt_pts = s->pkt->pts;
  265. pic->pkt_pos = s->pkt->pos;
  266. pic->pkt_duration = s->pkt->duration;
  267. } else {
  268. pic->pkt_pts = AV_NOPTS_VALUE;
  269. pic->pkt_pos = -1;
  270. pic->pkt_duration = 0;
  271. }
  272. pic->reordered_opaque= s->reordered_opaque;
  273. switch (s->codec->type) {
  274. case AVMEDIA_TYPE_VIDEO:
  275. pic->width = s->width;
  276. pic->height = s->height;
  277. pic->format = s->pix_fmt;
  278. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  279. break;
  280. case AVMEDIA_TYPE_AUDIO:
  281. pic->sample_rate = s->sample_rate;
  282. pic->format = s->sample_fmt;
  283. pic->channel_layout = s->channel_layout;
  284. pic->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. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  617. {
  618. memset(sub, 0, sizeof(*sub));
  619. sub->pts = AV_NOPTS_VALUE;
  620. }
  621. static int get_bit_rate(AVCodecContext *ctx)
  622. {
  623. int bit_rate;
  624. int bits_per_sample;
  625. switch(ctx->codec_type) {
  626. case AVMEDIA_TYPE_VIDEO:
  627. case AVMEDIA_TYPE_DATA:
  628. case AVMEDIA_TYPE_SUBTITLE:
  629. case AVMEDIA_TYPE_ATTACHMENT:
  630. bit_rate = ctx->bit_rate;
  631. break;
  632. case AVMEDIA_TYPE_AUDIO:
  633. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  634. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  635. break;
  636. default:
  637. bit_rate = 0;
  638. break;
  639. }
  640. return bit_rate;
  641. }
  642. #if FF_API_AVCODEC_OPEN
  643. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  644. {
  645. return avcodec_open2(avctx, codec, NULL);
  646. }
  647. #endif
  648. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  649. {
  650. int ret = 0;
  651. AVDictionary *tmp = NULL;
  652. if (avcodec_is_open(avctx))
  653. return 0;
  654. if ((!codec && !avctx->codec)) {
  655. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  656. return AVERROR(EINVAL);
  657. }
  658. if ((codec && avctx->codec && codec != avctx->codec)) {
  659. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  660. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  661. return AVERROR(EINVAL);
  662. }
  663. if (!codec)
  664. codec = avctx->codec;
  665. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  666. return AVERROR(EINVAL);
  667. if (options)
  668. av_dict_copy(&tmp, *options, 0);
  669. /* If there is a user-supplied mutex locking routine, call it. */
  670. if (ff_lockmgr_cb) {
  671. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  672. return -1;
  673. }
  674. entangled_thread_counter++;
  675. if(entangled_thread_counter != 1){
  676. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  677. ret = -1;
  678. goto end;
  679. }
  680. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  681. if (!avctx->internal) {
  682. ret = AVERROR(ENOMEM);
  683. goto end;
  684. }
  685. if (codec->priv_data_size > 0) {
  686. if(!avctx->priv_data){
  687. avctx->priv_data = av_mallocz(codec->priv_data_size);
  688. if (!avctx->priv_data) {
  689. ret = AVERROR(ENOMEM);
  690. goto end;
  691. }
  692. if (codec->priv_class) {
  693. *(const AVClass**)avctx->priv_data= codec->priv_class;
  694. av_opt_set_defaults(avctx->priv_data);
  695. }
  696. }
  697. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  698. goto free_and_end;
  699. } else {
  700. avctx->priv_data = NULL;
  701. }
  702. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  703. goto free_and_end;
  704. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  705. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  706. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
  707. ret = -1;
  708. goto free_and_end;
  709. }
  710. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  711. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  712. if(avctx->coded_width && avctx->coded_height)
  713. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  714. else if(avctx->width && avctx->height)
  715. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  716. }
  717. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  718. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  719. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  720. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  721. avcodec_set_dimensions(avctx, 0, 0);
  722. }
  723. /* if the decoder init function was already called previously,
  724. free the already allocated subtitle_header before overwriting it */
  725. if (av_codec_is_decoder(codec))
  726. av_freep(&avctx->subtitle_header);
  727. #define SANE_NB_CHANNELS 128U
  728. if (avctx->channels > SANE_NB_CHANNELS) {
  729. ret = AVERROR(EINVAL);
  730. goto free_and_end;
  731. }
  732. avctx->codec = codec;
  733. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  734. avctx->codec_id == CODEC_ID_NONE) {
  735. avctx->codec_type = codec->type;
  736. avctx->codec_id = codec->id;
  737. }
  738. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  739. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  740. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  741. ret = AVERROR(EINVAL);
  742. goto free_and_end;
  743. }
  744. avctx->frame_number = 0;
  745. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  746. (!avctx->time_base.num || !avctx->time_base.den)) {
  747. avctx->time_base.num = 1;
  748. avctx->time_base.den = avctx->sample_rate;
  749. }
  750. if (!HAVE_THREADS)
  751. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  752. entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  753. ret = ff_frame_thread_encoder_init(avctx);
  754. entangled_thread_counter++;
  755. if (ret < 0)
  756. goto free_and_end;
  757. if (HAVE_THREADS && !avctx->thread_opaque
  758. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  759. ret = ff_thread_init(avctx);
  760. if (ret < 0) {
  761. goto free_and_end;
  762. }
  763. }
  764. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  765. avctx->thread_count = 1;
  766. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  767. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  768. avctx->codec->max_lowres);
  769. ret = AVERROR(EINVAL);
  770. goto free_and_end;
  771. }
  772. if (av_codec_is_encoder(avctx->codec)) {
  773. int i;
  774. if (avctx->codec->sample_fmts) {
  775. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  776. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  777. break;
  778. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  779. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  780. ret = AVERROR(EINVAL);
  781. goto free_and_end;
  782. }
  783. }
  784. if (avctx->codec->pix_fmts) {
  785. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  786. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  787. break;
  788. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE
  789. && !((avctx->codec_id == CODEC_ID_MJPEG || avctx->codec_id == CODEC_ID_LJPEG)
  790. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  791. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  792. ret = AVERROR(EINVAL);
  793. goto free_and_end;
  794. }
  795. }
  796. if (avctx->codec->supported_samplerates) {
  797. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  798. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  799. break;
  800. if (avctx->codec->supported_samplerates[i] == 0) {
  801. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  802. ret = AVERROR(EINVAL);
  803. goto free_and_end;
  804. }
  805. }
  806. if (avctx->codec->channel_layouts) {
  807. if (!avctx->channel_layout) {
  808. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  809. } else {
  810. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  811. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  812. break;
  813. if (avctx->codec->channel_layouts[i] == 0) {
  814. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  815. ret = AVERROR(EINVAL);
  816. goto free_and_end;
  817. }
  818. }
  819. }
  820. if (avctx->channel_layout && avctx->channels) {
  821. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  822. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  823. ret = AVERROR(EINVAL);
  824. goto free_and_end;
  825. }
  826. } else if (avctx->channel_layout) {
  827. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  828. }
  829. }
  830. avctx->pts_correction_num_faulty_pts =
  831. avctx->pts_correction_num_faulty_dts = 0;
  832. avctx->pts_correction_last_pts =
  833. avctx->pts_correction_last_dts = INT64_MIN;
  834. if(avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME) || avctx->internal->frame_thread_encoder)){
  835. ret = avctx->codec->init(avctx);
  836. if (ret < 0) {
  837. goto free_and_end;
  838. }
  839. }
  840. ret=0;
  841. if (av_codec_is_decoder(avctx->codec)) {
  842. if (!avctx->bit_rate)
  843. avctx->bit_rate = get_bit_rate(avctx);
  844. /* validate channel layout from the decoder */
  845. if (avctx->channel_layout &&
  846. av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  847. av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
  848. avctx->channel_layout = 0;
  849. }
  850. }
  851. end:
  852. entangled_thread_counter--;
  853. /* Release any user-supplied mutex. */
  854. if (ff_lockmgr_cb) {
  855. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  856. }
  857. if (options) {
  858. av_dict_free(options);
  859. *options = tmp;
  860. }
  861. return ret;
  862. free_and_end:
  863. av_dict_free(&tmp);
  864. av_freep(&avctx->priv_data);
  865. av_freep(&avctx->internal);
  866. avctx->codec= NULL;
  867. goto end;
  868. }
  869. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  870. {
  871. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  872. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  873. return AVERROR(EINVAL);
  874. }
  875. if (avctx) {
  876. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  877. if (!avpkt->data || avpkt->size < size) {
  878. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  879. avpkt->data = avctx->internal->byte_buffer;
  880. avpkt->size = avctx->internal->byte_buffer_size;
  881. avpkt->destruct = NULL;
  882. }
  883. }
  884. if (avpkt->data) {
  885. void *destruct = avpkt->destruct;
  886. if (avpkt->size < size) {
  887. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  888. return AVERROR(EINVAL);
  889. }
  890. av_init_packet(avpkt);
  891. avpkt->destruct = destruct;
  892. avpkt->size = size;
  893. return 0;
  894. } else {
  895. int ret = av_new_packet(avpkt, size);
  896. if (ret < 0)
  897. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  898. return ret;
  899. }
  900. }
  901. int ff_alloc_packet(AVPacket *avpkt, int size)
  902. {
  903. return ff_alloc_packet2(NULL, avpkt, size);
  904. }
  905. /**
  906. * Pad last frame with silence.
  907. */
  908. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  909. {
  910. AVFrame *frame = NULL;
  911. uint8_t *buf = NULL;
  912. int ret;
  913. if (!(frame = avcodec_alloc_frame()))
  914. return AVERROR(ENOMEM);
  915. *frame = *src;
  916. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  917. s->frame_size, s->sample_fmt, 0)) < 0)
  918. goto fail;
  919. if (!(buf = av_malloc(ret))) {
  920. ret = AVERROR(ENOMEM);
  921. goto fail;
  922. }
  923. frame->nb_samples = s->frame_size;
  924. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  925. buf, ret, 0)) < 0)
  926. goto fail;
  927. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  928. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  929. goto fail;
  930. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  931. frame->nb_samples - src->nb_samples,
  932. s->channels, s->sample_fmt)) < 0)
  933. goto fail;
  934. *dst = frame;
  935. return 0;
  936. fail:
  937. if (frame->extended_data != frame->data)
  938. av_freep(&frame->extended_data);
  939. av_freep(&buf);
  940. av_freep(&frame);
  941. return ret;
  942. }
  943. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  944. AVPacket *avpkt,
  945. const AVFrame *frame,
  946. int *got_packet_ptr)
  947. {
  948. AVFrame tmp;
  949. AVFrame *padded_frame = NULL;
  950. int ret;
  951. AVPacket user_pkt = *avpkt;
  952. int needs_realloc = !user_pkt.data;
  953. *got_packet_ptr = 0;
  954. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  955. av_free_packet(avpkt);
  956. av_init_packet(avpkt);
  957. return 0;
  958. }
  959. /* ensure that extended_data is properly set */
  960. if (frame && !frame->extended_data) {
  961. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  962. avctx->channels > AV_NUM_DATA_POINTERS) {
  963. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  964. "with more than %d channels, but extended_data is not set.\n",
  965. AV_NUM_DATA_POINTERS);
  966. return AVERROR(EINVAL);
  967. }
  968. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  969. tmp = *frame;
  970. tmp.extended_data = tmp.data;
  971. frame = &tmp;
  972. }
  973. /* check for valid frame size */
  974. if (frame) {
  975. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  976. if (frame->nb_samples > avctx->frame_size) {
  977. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  978. return AVERROR(EINVAL);
  979. }
  980. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  981. if (frame->nb_samples < avctx->frame_size &&
  982. !avctx->internal->last_audio_frame) {
  983. ret = pad_last_frame(avctx, &padded_frame, frame);
  984. if (ret < 0)
  985. return ret;
  986. frame = padded_frame;
  987. avctx->internal->last_audio_frame = 1;
  988. }
  989. if (frame->nb_samples != avctx->frame_size) {
  990. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  991. return AVERROR(EINVAL);
  992. }
  993. }
  994. }
  995. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  996. if (!ret) {
  997. if (*got_packet_ptr) {
  998. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  999. if (avpkt->pts == AV_NOPTS_VALUE)
  1000. avpkt->pts = frame->pts;
  1001. if (!avpkt->duration)
  1002. avpkt->duration = ff_samples_to_time_base(avctx,
  1003. frame->nb_samples);
  1004. }
  1005. avpkt->dts = avpkt->pts;
  1006. } else {
  1007. avpkt->size = 0;
  1008. }
  1009. }
  1010. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1011. needs_realloc = 0;
  1012. if (user_pkt.data) {
  1013. if (user_pkt.size >= avpkt->size) {
  1014. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1015. } else {
  1016. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1017. avpkt->size = user_pkt.size;
  1018. ret = -1;
  1019. }
  1020. avpkt->data = user_pkt.data;
  1021. avpkt->destruct = user_pkt.destruct;
  1022. } else {
  1023. if (av_dup_packet(avpkt) < 0) {
  1024. ret = AVERROR(ENOMEM);
  1025. }
  1026. }
  1027. }
  1028. if (!ret) {
  1029. if (needs_realloc && avpkt->data) {
  1030. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1031. if (new_data)
  1032. avpkt->data = new_data;
  1033. }
  1034. avctx->frame_number++;
  1035. }
  1036. if (ret < 0 || !*got_packet_ptr) {
  1037. av_free_packet(avpkt);
  1038. av_init_packet(avpkt);
  1039. return ret;
  1040. }
  1041. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1042. this needs to be moved to the encoders, but for now we can do it
  1043. here to simplify things */
  1044. avpkt->flags |= AV_PKT_FLAG_KEY;
  1045. if (padded_frame) {
  1046. av_freep(&padded_frame->data[0]);
  1047. if (padded_frame->extended_data != padded_frame->data)
  1048. av_freep(&padded_frame->extended_data);
  1049. av_freep(&padded_frame);
  1050. }
  1051. return ret;
  1052. }
  1053. #if FF_API_OLD_DECODE_AUDIO
  1054. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1055. uint8_t *buf, int buf_size,
  1056. const short *samples)
  1057. {
  1058. AVPacket pkt;
  1059. AVFrame frame0;
  1060. AVFrame *frame;
  1061. int ret, samples_size, got_packet;
  1062. av_init_packet(&pkt);
  1063. pkt.data = buf;
  1064. pkt.size = buf_size;
  1065. if (samples) {
  1066. frame = &frame0;
  1067. avcodec_get_frame_defaults(frame);
  1068. if (avctx->frame_size) {
  1069. frame->nb_samples = avctx->frame_size;
  1070. } else {
  1071. /* if frame_size is not set, the number of samples must be
  1072. calculated from the buffer size */
  1073. int64_t nb_samples;
  1074. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1075. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1076. "support this codec\n");
  1077. return AVERROR(EINVAL);
  1078. }
  1079. nb_samples = (int64_t)buf_size * 8 /
  1080. (av_get_bits_per_sample(avctx->codec_id) *
  1081. avctx->channels);
  1082. if (nb_samples >= INT_MAX)
  1083. return AVERROR(EINVAL);
  1084. frame->nb_samples = nb_samples;
  1085. }
  1086. /* it is assumed that the samples buffer is large enough based on the
  1087. relevant parameters */
  1088. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1089. frame->nb_samples,
  1090. avctx->sample_fmt, 1);
  1091. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1092. avctx->sample_fmt,
  1093. (const uint8_t *) samples,
  1094. samples_size, 1)))
  1095. return ret;
  1096. /* fabricate frame pts from sample count.
  1097. this is needed because the avcodec_encode_audio() API does not have
  1098. a way for the user to provide pts */
  1099. if(avctx->sample_rate && avctx->time_base.num)
  1100. frame->pts = ff_samples_to_time_base(avctx,
  1101. avctx->internal->sample_count);
  1102. else
  1103. frame->pts = AV_NOPTS_VALUE;
  1104. avctx->internal->sample_count += frame->nb_samples;
  1105. } else {
  1106. frame = NULL;
  1107. }
  1108. got_packet = 0;
  1109. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1110. if (!ret && got_packet && avctx->coded_frame) {
  1111. avctx->coded_frame->pts = pkt.pts;
  1112. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1113. }
  1114. /* free any side data since we cannot return it */
  1115. ff_packet_free_side_data(&pkt);
  1116. if (frame && frame->extended_data != frame->data)
  1117. av_freep(&frame->extended_data);
  1118. return ret ? ret : pkt.size;
  1119. }
  1120. #endif
  1121. #if FF_API_OLD_ENCODE_VIDEO
  1122. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1123. const AVFrame *pict)
  1124. {
  1125. AVPacket pkt;
  1126. int ret, got_packet = 0;
  1127. if(buf_size < FF_MIN_BUFFER_SIZE){
  1128. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1129. return -1;
  1130. }
  1131. av_init_packet(&pkt);
  1132. pkt.data = buf;
  1133. pkt.size = buf_size;
  1134. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1135. if (!ret && got_packet && avctx->coded_frame) {
  1136. avctx->coded_frame->pts = pkt.pts;
  1137. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1138. }
  1139. /* free any side data since we cannot return it */
  1140. if (pkt.side_data_elems > 0) {
  1141. int i;
  1142. for (i = 0; i < pkt.side_data_elems; i++)
  1143. av_free(pkt.side_data[i].data);
  1144. av_freep(&pkt.side_data);
  1145. pkt.side_data_elems = 0;
  1146. }
  1147. return ret ? ret : pkt.size;
  1148. }
  1149. #endif
  1150. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1151. AVPacket *avpkt,
  1152. const AVFrame *frame,
  1153. int *got_packet_ptr)
  1154. {
  1155. int ret;
  1156. AVPacket user_pkt = *avpkt;
  1157. int needs_realloc = !user_pkt.data;
  1158. *got_packet_ptr = 0;
  1159. if(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1160. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1161. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1162. av_free_packet(avpkt);
  1163. av_init_packet(avpkt);
  1164. avpkt->size = 0;
  1165. return 0;
  1166. }
  1167. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1168. return AVERROR(EINVAL);
  1169. av_assert0(avctx->codec->encode2);
  1170. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1171. av_assert0(ret <= 0);
  1172. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1173. needs_realloc = 0;
  1174. if (user_pkt.data) {
  1175. if (user_pkt.size >= avpkt->size) {
  1176. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1177. } else {
  1178. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1179. avpkt->size = user_pkt.size;
  1180. ret = -1;
  1181. }
  1182. avpkt->data = user_pkt.data;
  1183. avpkt->destruct = user_pkt.destruct;
  1184. } else {
  1185. if (av_dup_packet(avpkt) < 0) {
  1186. ret = AVERROR(ENOMEM);
  1187. }
  1188. }
  1189. }
  1190. if (!ret) {
  1191. if (!*got_packet_ptr)
  1192. avpkt->size = 0;
  1193. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1194. avpkt->pts = avpkt->dts = frame->pts;
  1195. if (needs_realloc && avpkt->data &&
  1196. avpkt->destruct == av_destruct_packet) {
  1197. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1198. if (new_data)
  1199. avpkt->data = new_data;
  1200. }
  1201. avctx->frame_number++;
  1202. }
  1203. if (ret < 0 || !*got_packet_ptr)
  1204. av_free_packet(avpkt);
  1205. emms_c();
  1206. return ret;
  1207. }
  1208. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1209. const AVSubtitle *sub)
  1210. {
  1211. int ret;
  1212. if(sub->start_display_time) {
  1213. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1214. return -1;
  1215. }
  1216. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
  1217. avctx->frame_number++;
  1218. return ret;
  1219. }
  1220. /**
  1221. * Attempt to guess proper monotonic timestamps for decoded video frames
  1222. * which might have incorrect times. Input timestamps may wrap around, in
  1223. * which case the output will as well.
  1224. *
  1225. * @param pts the pts field of the decoded AVPacket, as passed through
  1226. * AVFrame.pkt_pts
  1227. * @param dts the dts field of the decoded AVPacket
  1228. * @return one of the input values, may be AV_NOPTS_VALUE
  1229. */
  1230. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1231. int64_t reordered_pts, int64_t dts)
  1232. {
  1233. int64_t pts = AV_NOPTS_VALUE;
  1234. if (dts != AV_NOPTS_VALUE) {
  1235. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1236. ctx->pts_correction_last_dts = dts;
  1237. }
  1238. if (reordered_pts != AV_NOPTS_VALUE) {
  1239. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1240. ctx->pts_correction_last_pts = reordered_pts;
  1241. }
  1242. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1243. && reordered_pts != AV_NOPTS_VALUE)
  1244. pts = reordered_pts;
  1245. else
  1246. pts = dts;
  1247. return pts;
  1248. }
  1249. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1250. {
  1251. int size = 0;
  1252. const uint8_t *data;
  1253. uint32_t flags;
  1254. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1255. return;
  1256. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1257. if (!data || size < 4)
  1258. return;
  1259. flags = bytestream_get_le32(&data);
  1260. size -= 4;
  1261. if (size < 4) /* Required for any of the changes */
  1262. return;
  1263. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1264. avctx->channels = bytestream_get_le32(&data);
  1265. size -= 4;
  1266. }
  1267. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1268. if (size < 8)
  1269. return;
  1270. avctx->channel_layout = bytestream_get_le64(&data);
  1271. size -= 8;
  1272. }
  1273. if (size < 4)
  1274. return;
  1275. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1276. avctx->sample_rate = bytestream_get_le32(&data);
  1277. size -= 4;
  1278. }
  1279. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1280. if (size < 8)
  1281. return;
  1282. avctx->width = bytestream_get_le32(&data);
  1283. avctx->height = bytestream_get_le32(&data);
  1284. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1285. size -= 8;
  1286. }
  1287. }
  1288. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1289. int *got_picture_ptr,
  1290. const AVPacket *avpkt)
  1291. {
  1292. int ret;
  1293. // copy to ensure we do not change avpkt
  1294. AVPacket tmp = *avpkt;
  1295. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1296. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1297. return AVERROR(EINVAL);
  1298. }
  1299. *got_picture_ptr= 0;
  1300. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1301. return AVERROR(EINVAL);
  1302. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1303. int did_split = av_packet_split_side_data(&tmp);
  1304. apply_param_change(avctx, &tmp);
  1305. avctx->pkt = &tmp;
  1306. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1307. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1308. &tmp);
  1309. else {
  1310. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1311. &tmp);
  1312. picture->pkt_dts= avpkt->dts;
  1313. if(!avctx->has_b_frames){
  1314. picture->pkt_pos= avpkt->pos;
  1315. }
  1316. //FIXME these should be under if(!avctx->has_b_frames)
  1317. if (!picture->sample_aspect_ratio.num)
  1318. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1319. if (!picture->width)
  1320. picture->width = avctx->width;
  1321. if (!picture->height)
  1322. picture->height = avctx->height;
  1323. if (picture->format == PIX_FMT_NONE)
  1324. picture->format = avctx->pix_fmt;
  1325. }
  1326. emms_c(); //needed to avoid an emms_c() call before every return;
  1327. avctx->pkt = NULL;
  1328. if (did_split) {
  1329. ff_packet_free_side_data(&tmp);
  1330. if(ret == tmp.size)
  1331. ret = avpkt->size;
  1332. }
  1333. if (*got_picture_ptr){
  1334. avctx->frame_number++;
  1335. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1336. picture->pkt_pts,
  1337. picture->pkt_dts);
  1338. }
  1339. }else
  1340. ret= 0;
  1341. return ret;
  1342. }
  1343. #if FF_API_OLD_DECODE_AUDIO
  1344. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1345. int *frame_size_ptr,
  1346. AVPacket *avpkt)
  1347. {
  1348. AVFrame frame;
  1349. int ret, got_frame = 0;
  1350. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1351. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1352. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1353. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1354. "avcodec_decode_audio4()\n");
  1355. avctx->get_buffer = avcodec_default_get_buffer;
  1356. avctx->release_buffer = avcodec_default_release_buffer;
  1357. }
  1358. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1359. if (ret >= 0 && got_frame) {
  1360. int ch, plane_size;
  1361. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1362. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1363. frame.nb_samples,
  1364. avctx->sample_fmt, 1);
  1365. if (*frame_size_ptr < data_size) {
  1366. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1367. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1368. return AVERROR(EINVAL);
  1369. }
  1370. memcpy(samples, frame.extended_data[0], plane_size);
  1371. if (planar && avctx->channels > 1) {
  1372. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1373. for (ch = 1; ch < avctx->channels; ch++) {
  1374. memcpy(out, frame.extended_data[ch], plane_size);
  1375. out += plane_size;
  1376. }
  1377. }
  1378. *frame_size_ptr = data_size;
  1379. } else {
  1380. *frame_size_ptr = 0;
  1381. }
  1382. return ret;
  1383. }
  1384. #endif
  1385. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1386. AVFrame *frame,
  1387. int *got_frame_ptr,
  1388. const AVPacket *avpkt)
  1389. {
  1390. int ret = 0;
  1391. *got_frame_ptr = 0;
  1392. if (!avpkt->data && avpkt->size) {
  1393. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1394. return AVERROR(EINVAL);
  1395. }
  1396. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1397. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1398. return AVERROR(EINVAL);
  1399. }
  1400. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1401. uint8_t *side;
  1402. int side_size;
  1403. // copy to ensure we do not change avpkt
  1404. AVPacket tmp = *avpkt;
  1405. int did_split = av_packet_split_side_data(&tmp);
  1406. apply_param_change(avctx, &tmp);
  1407. avctx->pkt = &tmp;
  1408. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1409. if (ret >= 0 && *got_frame_ptr) {
  1410. avctx->frame_number++;
  1411. frame->pkt_dts = avpkt->dts;
  1412. frame->best_effort_timestamp = guess_correct_pts(avctx,
  1413. frame->pkt_pts,
  1414. frame->pkt_dts);
  1415. if (frame->format == AV_SAMPLE_FMT_NONE)
  1416. frame->format = avctx->sample_fmt;
  1417. if (!frame->channel_layout)
  1418. frame->channel_layout = avctx->channel_layout;
  1419. if (!frame->channels)
  1420. frame->channels = avctx->channels;
  1421. if (!frame->sample_rate)
  1422. frame->sample_rate = avctx->sample_rate;
  1423. }
  1424. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1425. if(side && side_size>=10) {
  1426. avctx->internal->skip_samples = AV_RL32(side);
  1427. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1428. avctx->internal->skip_samples);
  1429. }
  1430. if (avctx->internal->skip_samples) {
  1431. if(frame->nb_samples <= avctx->internal->skip_samples){
  1432. *got_frame_ptr = 0;
  1433. avctx->internal->skip_samples -= frame->nb_samples;
  1434. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1435. avctx->internal->skip_samples);
  1436. } else {
  1437. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1438. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1439. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1440. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1441. (AVRational){1, avctx->sample_rate},
  1442. avctx->pkt_timebase);
  1443. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1444. frame->pkt_pts += diff_ts;
  1445. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1446. frame->pkt_dts += diff_ts;
  1447. if (frame->pkt_duration >= diff_ts)
  1448. frame->pkt_duration -= diff_ts;
  1449. } else {
  1450. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1451. }
  1452. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1453. avctx->internal->skip_samples, frame->nb_samples);
  1454. frame->nb_samples -= avctx->internal->skip_samples;
  1455. avctx->internal->skip_samples = 0;
  1456. }
  1457. }
  1458. avctx->pkt = NULL;
  1459. if (did_split) {
  1460. ff_packet_free_side_data(&tmp);
  1461. if(ret == tmp.size)
  1462. ret = avpkt->size;
  1463. }
  1464. }
  1465. return ret;
  1466. }
  1467. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1468. int *got_sub_ptr,
  1469. AVPacket *avpkt)
  1470. {
  1471. int ret;
  1472. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1473. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1474. return AVERROR(EINVAL);
  1475. }
  1476. avctx->pkt = avpkt;
  1477. *got_sub_ptr = 0;
  1478. avcodec_get_subtitle_defaults(sub);
  1479. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1480. if (*got_sub_ptr)
  1481. avctx->frame_number++;
  1482. return ret;
  1483. }
  1484. void avsubtitle_free(AVSubtitle *sub)
  1485. {
  1486. int i;
  1487. for (i = 0; i < sub->num_rects; i++)
  1488. {
  1489. av_freep(&sub->rects[i]->pict.data[0]);
  1490. av_freep(&sub->rects[i]->pict.data[1]);
  1491. av_freep(&sub->rects[i]->pict.data[2]);
  1492. av_freep(&sub->rects[i]->pict.data[3]);
  1493. av_freep(&sub->rects[i]->text);
  1494. av_freep(&sub->rects[i]->ass);
  1495. av_freep(&sub->rects[i]);
  1496. }
  1497. av_freep(&sub->rects);
  1498. memset(sub, 0, sizeof(AVSubtitle));
  1499. }
  1500. av_cold int avcodec_close(AVCodecContext *avctx)
  1501. {
  1502. /* If there is a user-supplied mutex locking routine, call it. */
  1503. if (ff_lockmgr_cb) {
  1504. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1505. return -1;
  1506. }
  1507. entangled_thread_counter++;
  1508. if(entangled_thread_counter != 1){
  1509. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1510. entangled_thread_counter--;
  1511. return -1;
  1512. }
  1513. if (avcodec_is_open(avctx)) {
  1514. if (avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1515. entangled_thread_counter --;
  1516. ff_frame_thread_encoder_free(avctx);
  1517. entangled_thread_counter ++;
  1518. }
  1519. if (HAVE_THREADS && avctx->thread_opaque)
  1520. ff_thread_free(avctx);
  1521. if (avctx->codec && avctx->codec->close)
  1522. avctx->codec->close(avctx);
  1523. avcodec_default_free_buffers(avctx);
  1524. avctx->coded_frame = NULL;
  1525. avctx->internal->byte_buffer_size = 0;
  1526. av_freep(&avctx->internal->byte_buffer);
  1527. av_freep(&avctx->internal);
  1528. }
  1529. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1530. av_opt_free(avctx->priv_data);
  1531. av_opt_free(avctx);
  1532. av_freep(&avctx->priv_data);
  1533. if (av_codec_is_encoder(avctx->codec))
  1534. av_freep(&avctx->extradata);
  1535. avctx->codec = NULL;
  1536. avctx->active_thread_type = 0;
  1537. entangled_thread_counter--;
  1538. /* Release any user-supplied mutex. */
  1539. if (ff_lockmgr_cb) {
  1540. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1541. }
  1542. return 0;
  1543. }
  1544. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1545. {
  1546. switch(id){
  1547. //This is for future deprecatec codec ids, its empty since
  1548. //last major bump but will fill up again over time, please don't remove it
  1549. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1550. default : return id;
  1551. }
  1552. }
  1553. AVCodec *avcodec_find_encoder(enum CodecID id)
  1554. {
  1555. AVCodec *p, *experimental=NULL;
  1556. p = first_avcodec;
  1557. id= remap_deprecated_codec_id(id);
  1558. while (p) {
  1559. if (av_codec_is_encoder(p) && p->id == id) {
  1560. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1561. experimental = p;
  1562. } else
  1563. return p;
  1564. }
  1565. p = p->next;
  1566. }
  1567. return experimental;
  1568. }
  1569. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1570. {
  1571. AVCodec *p;
  1572. if (!name)
  1573. return NULL;
  1574. p = first_avcodec;
  1575. while (p) {
  1576. if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1577. return p;
  1578. p = p->next;
  1579. }
  1580. return NULL;
  1581. }
  1582. AVCodec *avcodec_find_decoder(enum CodecID id)
  1583. {
  1584. AVCodec *p, *experimental=NULL;
  1585. p = first_avcodec;
  1586. id= remap_deprecated_codec_id(id);
  1587. while (p) {
  1588. if (av_codec_is_decoder(p) && p->id == id) {
  1589. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1590. experimental = p;
  1591. } else
  1592. return p;
  1593. }
  1594. p = p->next;
  1595. }
  1596. return experimental;
  1597. }
  1598. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1599. {
  1600. AVCodec *p;
  1601. if (!name)
  1602. return NULL;
  1603. p = first_avcodec;
  1604. while (p) {
  1605. if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1606. return p;
  1607. p = p->next;
  1608. }
  1609. return NULL;
  1610. }
  1611. const char *avcodec_get_name(enum CodecID id)
  1612. {
  1613. AVCodec *codec;
  1614. #if !CONFIG_SMALL
  1615. switch (id) {
  1616. #include "libavcodec/codec_names.h"
  1617. }
  1618. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1619. #endif
  1620. codec = avcodec_find_decoder(id);
  1621. if (codec)
  1622. return codec->name;
  1623. codec = avcodec_find_encoder(id);
  1624. if (codec)
  1625. return codec->name;
  1626. return "unknown_codec";
  1627. }
  1628. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1629. {
  1630. int i, len, ret = 0;
  1631. #define IS_PRINT(x) \
  1632. (((x) >= '0' && (x) <= '9') || \
  1633. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1634. ((x) == '.' || (x) == ' ' || (x) == '-'))
  1635. for (i = 0; i < 4; i++) {
  1636. len = snprintf(buf, buf_size,
  1637. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1638. buf += len;
  1639. buf_size = buf_size > len ? buf_size - len : 0;
  1640. ret += len;
  1641. codec_tag>>=8;
  1642. }
  1643. return ret;
  1644. }
  1645. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1646. {
  1647. const char *codec_type;
  1648. const char *codec_name;
  1649. const char *profile = NULL;
  1650. AVCodec *p;
  1651. int bitrate;
  1652. AVRational display_aspect_ratio;
  1653. if (!buf || buf_size <= 0)
  1654. return;
  1655. codec_type = av_get_media_type_string(enc->codec_type);
  1656. codec_name = avcodec_get_name(enc->codec_id);
  1657. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1658. if (enc->codec)
  1659. p = enc->codec;
  1660. else
  1661. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1662. avcodec_find_decoder(enc->codec_id);
  1663. if (p)
  1664. profile = av_get_profile_name(p, enc->profile);
  1665. }
  1666. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1667. codec_name, enc->mb_decision ? " (hq)" : "");
  1668. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1669. if (profile)
  1670. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1671. if (enc->codec_tag) {
  1672. char tag_buf[32];
  1673. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1674. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1675. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1676. }
  1677. switch(enc->codec_type) {
  1678. case AVMEDIA_TYPE_VIDEO:
  1679. if (enc->pix_fmt != PIX_FMT_NONE) {
  1680. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1681. ", %s",
  1682. av_get_pix_fmt_name(enc->pix_fmt));
  1683. }
  1684. if (enc->width) {
  1685. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1686. ", %dx%d",
  1687. enc->width, enc->height);
  1688. if (enc->sample_aspect_ratio.num) {
  1689. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1690. enc->width*enc->sample_aspect_ratio.num,
  1691. enc->height*enc->sample_aspect_ratio.den,
  1692. 1024*1024);
  1693. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1694. " [SAR %d:%d DAR %d:%d]",
  1695. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1696. display_aspect_ratio.num, display_aspect_ratio.den);
  1697. }
  1698. if(av_log_get_level() >= AV_LOG_DEBUG){
  1699. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1700. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1701. ", %d/%d",
  1702. enc->time_base.num/g, enc->time_base.den/g);
  1703. }
  1704. }
  1705. if (encode) {
  1706. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1707. ", q=%d-%d", enc->qmin, enc->qmax);
  1708. }
  1709. break;
  1710. case AVMEDIA_TYPE_AUDIO:
  1711. if (enc->sample_rate) {
  1712. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1713. ", %d Hz", enc->sample_rate);
  1714. }
  1715. av_strlcat(buf, ", ", buf_size);
  1716. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1717. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1718. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1719. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1720. }
  1721. break;
  1722. default:
  1723. return;
  1724. }
  1725. if (encode) {
  1726. if (enc->flags & CODEC_FLAG_PASS1)
  1727. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1728. ", pass 1");
  1729. if (enc->flags & CODEC_FLAG_PASS2)
  1730. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1731. ", pass 2");
  1732. }
  1733. bitrate = get_bit_rate(enc);
  1734. if (bitrate != 0) {
  1735. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1736. ", %d kb/s", bitrate / 1000);
  1737. }
  1738. }
  1739. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1740. {
  1741. const AVProfile *p;
  1742. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1743. return NULL;
  1744. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1745. if (p->profile == profile)
  1746. return p->name;
  1747. return NULL;
  1748. }
  1749. unsigned avcodec_version( void )
  1750. {
  1751. // av_assert0(CODEC_ID_V410==164);
  1752. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1753. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1754. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1755. av_assert0(CODEC_ID_SRT==94216);
  1756. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1757. return LIBAVCODEC_VERSION_INT;
  1758. }
  1759. const char *avcodec_configuration(void)
  1760. {
  1761. return FFMPEG_CONFIGURATION;
  1762. }
  1763. const char *avcodec_license(void)
  1764. {
  1765. #define LICENSE_PREFIX "libavcodec license: "
  1766. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1767. }
  1768. void avcodec_flush_buffers(AVCodecContext *avctx)
  1769. {
  1770. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1771. ff_thread_flush(avctx);
  1772. else if(avctx->codec->flush)
  1773. avctx->codec->flush(avctx);
  1774. avctx->pts_correction_last_pts =
  1775. avctx->pts_correction_last_dts = INT64_MIN;
  1776. }
  1777. static void video_free_buffers(AVCodecContext *s)
  1778. {
  1779. AVCodecInternal *avci = s->internal;
  1780. int i, j;
  1781. if (!avci->buffer)
  1782. return;
  1783. if (avci->buffer_count)
  1784. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1785. avci->buffer_count);
  1786. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1787. InternalBuffer *buf = &avci->buffer[i];
  1788. for(j=0; j<4; j++){
  1789. av_freep(&buf->base[j]);
  1790. buf->data[j]= NULL;
  1791. }
  1792. }
  1793. av_freep(&avci->buffer);
  1794. avci->buffer_count=0;
  1795. }
  1796. static void audio_free_buffers(AVCodecContext *avctx)
  1797. {
  1798. AVCodecInternal *avci = avctx->internal;
  1799. InternalBuffer *buf;
  1800. if (!avci->buffer)
  1801. return;
  1802. buf = avci->buffer;
  1803. if (buf->extended_data) {
  1804. av_free(buf->extended_data[0]);
  1805. if (buf->extended_data != buf->data)
  1806. av_freep(&buf->extended_data);
  1807. }
  1808. av_freep(&avci->buffer);
  1809. }
  1810. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1811. {
  1812. switch (avctx->codec_type) {
  1813. case AVMEDIA_TYPE_VIDEO:
  1814. video_free_buffers(avctx);
  1815. break;
  1816. case AVMEDIA_TYPE_AUDIO:
  1817. audio_free_buffers(avctx);
  1818. break;
  1819. default:
  1820. break;
  1821. }
  1822. }
  1823. int av_get_exact_bits_per_sample(enum CodecID codec_id)
  1824. {
  1825. switch(codec_id){
  1826. case CODEC_ID_ADPCM_CT:
  1827. case CODEC_ID_ADPCM_IMA_APC:
  1828. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1829. case CODEC_ID_ADPCM_IMA_WS:
  1830. case CODEC_ID_ADPCM_G722:
  1831. case CODEC_ID_ADPCM_YAMAHA:
  1832. return 4;
  1833. case CODEC_ID_PCM_ALAW:
  1834. case CODEC_ID_PCM_MULAW:
  1835. case CODEC_ID_PCM_S8:
  1836. case CODEC_ID_PCM_U8:
  1837. case CODEC_ID_PCM_ZORK:
  1838. return 8;
  1839. case CODEC_ID_PCM_S16BE:
  1840. case CODEC_ID_PCM_S16LE:
  1841. case CODEC_ID_PCM_S16LE_PLANAR:
  1842. case CODEC_ID_PCM_U16BE:
  1843. case CODEC_ID_PCM_U16LE:
  1844. return 16;
  1845. case CODEC_ID_PCM_S24DAUD:
  1846. case CODEC_ID_PCM_S24BE:
  1847. case CODEC_ID_PCM_S24LE:
  1848. case CODEC_ID_PCM_U24BE:
  1849. case CODEC_ID_PCM_U24LE:
  1850. return 24;
  1851. case CODEC_ID_PCM_S32BE:
  1852. case CODEC_ID_PCM_S32LE:
  1853. case CODEC_ID_PCM_U32BE:
  1854. case CODEC_ID_PCM_U32LE:
  1855. case CODEC_ID_PCM_F32BE:
  1856. case CODEC_ID_PCM_F32LE:
  1857. return 32;
  1858. case CODEC_ID_PCM_F64BE:
  1859. case CODEC_ID_PCM_F64LE:
  1860. return 64;
  1861. default:
  1862. return 0;
  1863. }
  1864. }
  1865. enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1866. {
  1867. static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
  1868. [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1869. [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1870. [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1871. [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1872. [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1873. [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1874. [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1875. [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1876. [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1877. [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1878. };
  1879. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1880. return CODEC_ID_NONE;
  1881. if (be < 0 || be > 1)
  1882. be = AV_NE(1, 0);
  1883. return map[fmt][be];
  1884. }
  1885. int av_get_bits_per_sample(enum CodecID codec_id)
  1886. {
  1887. switch (codec_id) {
  1888. case CODEC_ID_ADPCM_SBPRO_2:
  1889. return 2;
  1890. case CODEC_ID_ADPCM_SBPRO_3:
  1891. return 3;
  1892. case CODEC_ID_ADPCM_SBPRO_4:
  1893. case CODEC_ID_ADPCM_IMA_WAV:
  1894. case CODEC_ID_ADPCM_IMA_QT:
  1895. case CODEC_ID_ADPCM_SWF:
  1896. case CODEC_ID_ADPCM_MS:
  1897. return 4;
  1898. default:
  1899. return av_get_exact_bits_per_sample(codec_id);
  1900. }
  1901. }
  1902. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1903. {
  1904. int id, sr, ch, ba, tag, bps;
  1905. id = avctx->codec_id;
  1906. sr = avctx->sample_rate;
  1907. ch = avctx->channels;
  1908. ba = avctx->block_align;
  1909. tag = avctx->codec_tag;
  1910. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1911. /* codecs with an exact constant bits per sample */
  1912. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1913. return (frame_bytes * 8LL) / (bps * ch);
  1914. bps = avctx->bits_per_coded_sample;
  1915. /* codecs with a fixed packet duration */
  1916. switch (id) {
  1917. case CODEC_ID_ADPCM_ADX: return 32;
  1918. case CODEC_ID_ADPCM_IMA_QT: return 64;
  1919. case CODEC_ID_ADPCM_EA_XAS: return 128;
  1920. case CODEC_ID_AMR_NB:
  1921. case CODEC_ID_GSM:
  1922. case CODEC_ID_QCELP:
  1923. case CODEC_ID_RA_144:
  1924. case CODEC_ID_RA_288: return 160;
  1925. case CODEC_ID_IMC: return 256;
  1926. case CODEC_ID_AMR_WB:
  1927. case CODEC_ID_GSM_MS: return 320;
  1928. case CODEC_ID_MP1: return 384;
  1929. case CODEC_ID_ATRAC1: return 512;
  1930. case CODEC_ID_ATRAC3: return 1024;
  1931. case CODEC_ID_MP2:
  1932. case CODEC_ID_MUSEPACK7: return 1152;
  1933. case CODEC_ID_AC3: return 1536;
  1934. }
  1935. if (sr > 0) {
  1936. /* calc from sample rate */
  1937. if (id == CODEC_ID_TTA)
  1938. return 256 * sr / 245;
  1939. if (ch > 0) {
  1940. /* calc from sample rate and channels */
  1941. if (id == CODEC_ID_BINKAUDIO_DCT)
  1942. return (480 << (sr / 22050)) / ch;
  1943. }
  1944. }
  1945. if (ba > 0) {
  1946. /* calc from block_align */
  1947. if (id == 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 == 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 == CODEC_ID_TRUESPEECH)
  1964. return 240 * (frame_bytes / 32);
  1965. if (id == CODEC_ID_NELLYMOSER)
  1966. return 256 * (frame_bytes / 64);
  1967. if (bps > 0) {
  1968. /* calc from frame_bytes and bits_per_coded_sample */
  1969. if (id == CODEC_ID_ADPCM_G726)
  1970. return frame_bytes * 8 / bps;
  1971. }
  1972. if (ch > 0) {
  1973. /* calc from frame_bytes and channels */
  1974. switch (id) {
  1975. case CODEC_ID_ADPCM_4XM:
  1976. case CODEC_ID_ADPCM_IMA_ISS:
  1977. return (frame_bytes - 4 * ch) * 2 / ch;
  1978. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1979. return (frame_bytes - 4) * 2 / ch;
  1980. case CODEC_ID_ADPCM_IMA_AMV:
  1981. return (frame_bytes - 8) * 2 / ch;
  1982. case CODEC_ID_ADPCM_XA:
  1983. return (frame_bytes / 128) * 224 / ch;
  1984. case CODEC_ID_INTERPLAY_DPCM:
  1985. return (frame_bytes - 6 - ch) / ch;
  1986. case CODEC_ID_ROQ_DPCM:
  1987. return (frame_bytes - 8) / ch;
  1988. case CODEC_ID_XAN_DPCM:
  1989. return (frame_bytes - 2 * ch) / ch;
  1990. case CODEC_ID_MACE3:
  1991. return 3 * frame_bytes / ch;
  1992. case CODEC_ID_MACE6:
  1993. return 6 * frame_bytes / ch;
  1994. case CODEC_ID_PCM_LXF:
  1995. return 2 * (frame_bytes / (5 * ch));
  1996. }
  1997. if (tag) {
  1998. /* calc from frame_bytes, channels, and codec_tag */
  1999. if (id == CODEC_ID_SOL_DPCM) {
  2000. if (tag == 3)
  2001. return frame_bytes / ch;
  2002. else
  2003. return frame_bytes * 2 / ch;
  2004. }
  2005. }
  2006. if (ba > 0) {
  2007. /* calc from frame_bytes, channels, and block_align */
  2008. int blocks = frame_bytes / ba;
  2009. switch (avctx->codec_id) {
  2010. case CODEC_ID_ADPCM_IMA_WAV:
  2011. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2012. case CODEC_ID_ADPCM_IMA_DK3:
  2013. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2014. case CODEC_ID_ADPCM_IMA_DK4:
  2015. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2016. case CODEC_ID_ADPCM_MS:
  2017. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2018. }
  2019. }
  2020. if (bps > 0) {
  2021. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2022. switch (avctx->codec_id) {
  2023. case CODEC_ID_PCM_DVD:
  2024. if(bps<4)
  2025. return 0;
  2026. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2027. case CODEC_ID_PCM_BLURAY:
  2028. if(bps<4)
  2029. return 0;
  2030. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2031. case CODEC_ID_S302M:
  2032. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2033. }
  2034. }
  2035. }
  2036. }
  2037. return 0;
  2038. }
  2039. #if !HAVE_THREADS
  2040. int ff_thread_init(AVCodecContext *s){
  2041. return -1;
  2042. }
  2043. #endif
  2044. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2045. {
  2046. unsigned int n = 0;
  2047. while(v >= 0xff) {
  2048. *s++ = 0xff;
  2049. v -= 0xff;
  2050. n++;
  2051. }
  2052. *s = v;
  2053. n++;
  2054. return n;
  2055. }
  2056. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  2057. int i;
  2058. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  2059. return i;
  2060. }
  2061. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2062. {
  2063. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  2064. "version to the newest one from Git. If the problem still "
  2065. "occurs, it means that your file has a feature which has not "
  2066. "been implemented.\n", feature);
  2067. if(want_sample)
  2068. av_log_ask_for_sample(avc, NULL);
  2069. }
  2070. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2071. {
  2072. va_list argument_list;
  2073. va_start(argument_list, msg);
  2074. if (msg)
  2075. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2076. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2077. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2078. "and contact the ffmpeg-devel mailing list.\n");
  2079. va_end(argument_list);
  2080. }
  2081. static AVHWAccel *first_hwaccel = NULL;
  2082. void av_register_hwaccel(AVHWAccel *hwaccel)
  2083. {
  2084. AVHWAccel **p = &first_hwaccel;
  2085. while (*p)
  2086. p = &(*p)->next;
  2087. *p = hwaccel;
  2088. hwaccel->next = NULL;
  2089. }
  2090. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2091. {
  2092. return hwaccel ? hwaccel->next : first_hwaccel;
  2093. }
  2094. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  2095. {
  2096. AVHWAccel *hwaccel=NULL;
  2097. while((hwaccel= av_hwaccel_next(hwaccel))){
  2098. if ( hwaccel->id == codec_id
  2099. && hwaccel->pix_fmt == pix_fmt)
  2100. return hwaccel;
  2101. }
  2102. return NULL;
  2103. }
  2104. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2105. {
  2106. if (ff_lockmgr_cb) {
  2107. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2108. return -1;
  2109. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2110. return -1;
  2111. }
  2112. ff_lockmgr_cb = cb;
  2113. if (ff_lockmgr_cb) {
  2114. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2115. return -1;
  2116. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2117. return -1;
  2118. }
  2119. return 0;
  2120. }
  2121. int avpriv_lock_avformat(void)
  2122. {
  2123. if (ff_lockmgr_cb) {
  2124. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2125. return -1;
  2126. }
  2127. return 0;
  2128. }
  2129. int avpriv_unlock_avformat(void)
  2130. {
  2131. if (ff_lockmgr_cb) {
  2132. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2133. return -1;
  2134. }
  2135. return 0;
  2136. }
  2137. unsigned int avpriv_toupper4(unsigned int x)
  2138. {
  2139. return toupper( x &0xFF)
  2140. + (toupper((x>>8 )&0xFF)<<8 )
  2141. + (toupper((x>>16)&0xFF)<<16)
  2142. + (toupper((x>>24)&0xFF)<<24);
  2143. }
  2144. #if !HAVE_THREADS
  2145. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2146. {
  2147. f->owner = avctx;
  2148. ff_init_buffer_info(avctx, f);
  2149. return avctx->get_buffer(avctx, f);
  2150. }
  2151. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2152. {
  2153. f->owner->release_buffer(f->owner, f);
  2154. }
  2155. void ff_thread_finish_setup(AVCodecContext *avctx)
  2156. {
  2157. }
  2158. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2159. {
  2160. }
  2161. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2162. {
  2163. }
  2164. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2165. {
  2166. return 1;
  2167. }
  2168. #endif
  2169. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  2170. {
  2171. AVCodec *c= avcodec_find_decoder(codec_id);
  2172. if(!c)
  2173. c= avcodec_find_encoder(codec_id);
  2174. if(c)
  2175. return c->type;
  2176. if (codec_id <= CODEC_ID_NONE)
  2177. return AVMEDIA_TYPE_UNKNOWN;
  2178. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  2179. return AVMEDIA_TYPE_VIDEO;
  2180. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  2181. return AVMEDIA_TYPE_AUDIO;
  2182. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  2183. return AVMEDIA_TYPE_SUBTITLE;
  2184. return AVMEDIA_TYPE_UNKNOWN;
  2185. }
  2186. int avcodec_is_open(AVCodecContext *s)
  2187. {
  2188. return !!s->internal;
  2189. }