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.

1609 lines
48KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/avstring.h"
  27. #include "libavutil/crc.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/dict.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include "bytestream.h"
  42. #include <stdlib.h>
  43. #include <stdarg.h>
  44. #include <limits.h>
  45. #include <float.h>
  46. static int volatile entangled_thread_counter=0;
  47. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  48. static void *codec_mutex;
  49. static void *avformat_mutex;
  50. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  51. {
  52. if(min_size < *size)
  53. return ptr;
  54. min_size= FFMAX(17*min_size/16 + 32, min_size);
  55. ptr= av_realloc(ptr, min_size);
  56. 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
  57. min_size= 0;
  58. *size= min_size;
  59. return ptr;
  60. }
  61. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  62. {
  63. void **p = ptr;
  64. if (min_size < *size)
  65. return;
  66. min_size= FFMAX(17*min_size/16 + 32, min_size);
  67. av_free(*p);
  68. *p = av_malloc(min_size);
  69. if (!*p) min_size = 0;
  70. *size= min_size;
  71. }
  72. /* encoder management */
  73. static AVCodec *first_avcodec = NULL;
  74. AVCodec *av_codec_next(AVCodec *c){
  75. if(c) return c->next;
  76. else return first_avcodec;
  77. }
  78. #if !FF_API_AVCODEC_INIT
  79. static
  80. #endif
  81. void avcodec_init(void)
  82. {
  83. static int initialized = 0;
  84. if (initialized != 0)
  85. return;
  86. initialized = 1;
  87. dsputil_static_init();
  88. }
  89. void avcodec_register(AVCodec *codec)
  90. {
  91. AVCodec **p;
  92. avcodec_init();
  93. p = &first_avcodec;
  94. while (*p != NULL) p = &(*p)->next;
  95. *p = codec;
  96. codec->next = NULL;
  97. if (codec->init_static_data)
  98. codec->init_static_data(codec);
  99. }
  100. unsigned avcodec_get_edge_width(void)
  101. {
  102. return EDGE_WIDTH;
  103. }
  104. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  105. s->coded_width = width;
  106. s->coded_height= height;
  107. s->width = -((-width )>>s->lowres);
  108. s->height= -((-height)>>s->lowres);
  109. }
  110. #define INTERNAL_BUFFER_SIZE (32+1)
  111. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  112. int linesize_align[AV_NUM_DATA_POINTERS])
  113. {
  114. int i;
  115. int w_align= 1;
  116. int h_align= 1;
  117. switch(s->pix_fmt){
  118. case PIX_FMT_YUV420P:
  119. case PIX_FMT_YUYV422:
  120. case PIX_FMT_UYVY422:
  121. case PIX_FMT_YUV422P:
  122. case PIX_FMT_YUV440P:
  123. case PIX_FMT_YUV444P:
  124. case PIX_FMT_GBRP:
  125. case PIX_FMT_GRAY8:
  126. case PIX_FMT_GRAY16BE:
  127. case PIX_FMT_GRAY16LE:
  128. case PIX_FMT_YUVJ420P:
  129. case PIX_FMT_YUVJ422P:
  130. case PIX_FMT_YUVJ440P:
  131. case PIX_FMT_YUVJ444P:
  132. case PIX_FMT_YUVA420P:
  133. case PIX_FMT_YUV420P9LE:
  134. case PIX_FMT_YUV420P9BE:
  135. case PIX_FMT_YUV420P10LE:
  136. case PIX_FMT_YUV420P10BE:
  137. case PIX_FMT_YUV422P9LE:
  138. case PIX_FMT_YUV422P9BE:
  139. case PIX_FMT_YUV422P10LE:
  140. case PIX_FMT_YUV422P10BE:
  141. case PIX_FMT_YUV444P9LE:
  142. case PIX_FMT_YUV444P9BE:
  143. case PIX_FMT_YUV444P10LE:
  144. case PIX_FMT_YUV444P10BE:
  145. case PIX_FMT_GBRP9LE:
  146. case PIX_FMT_GBRP9BE:
  147. case PIX_FMT_GBRP10LE:
  148. case PIX_FMT_GBRP10BE:
  149. w_align = 16; //FIXME assume 16 pixel per macroblock
  150. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  151. break;
  152. case PIX_FMT_YUV411P:
  153. case PIX_FMT_UYYVYY411:
  154. w_align=32;
  155. h_align=8;
  156. break;
  157. case PIX_FMT_YUV410P:
  158. if(s->codec_id == CODEC_ID_SVQ1){
  159. w_align=64;
  160. h_align=64;
  161. }
  162. case PIX_FMT_RGB555:
  163. if(s->codec_id == CODEC_ID_RPZA){
  164. w_align=4;
  165. h_align=4;
  166. }
  167. case PIX_FMT_PAL8:
  168. case PIX_FMT_BGR8:
  169. case PIX_FMT_RGB8:
  170. if(s->codec_id == CODEC_ID_SMC){
  171. w_align=4;
  172. h_align=4;
  173. }
  174. break;
  175. case PIX_FMT_BGR24:
  176. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  177. w_align=4;
  178. h_align=4;
  179. }
  180. break;
  181. default:
  182. w_align= 1;
  183. h_align= 1;
  184. break;
  185. }
  186. *width = FFALIGN(*width , w_align);
  187. *height= FFALIGN(*height, h_align);
  188. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  189. *height+=2; // some of the optimized chroma MC reads one line too much
  190. // which is also done in mpeg decoders with lowres > 0
  191. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  192. linesize_align[i] = STRIDE_ALIGN;
  193. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  194. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  195. //picture size unneccessarily in some cases. The solution here is not
  196. //pretty and better ideas are welcome!
  197. #if HAVE_MMX
  198. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  199. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  200. s->codec_id == CODEC_ID_VP6A) {
  201. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  202. linesize_align[i] = 16;
  203. }
  204. #endif
  205. }
  206. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  207. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  208. int linesize_align[AV_NUM_DATA_POINTERS];
  209. int align;
  210. avcodec_align_dimensions2(s, width, height, linesize_align);
  211. align = FFMAX(linesize_align[0], linesize_align[3]);
  212. linesize_align[1] <<= chroma_shift;
  213. linesize_align[2] <<= chroma_shift;
  214. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  215. *width=FFALIGN(*width, align);
  216. }
  217. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  218. {
  219. AVCodecInternal *avci = avctx->internal;
  220. InternalBuffer *buf;
  221. int buf_size, ret, i, needs_extended_data;
  222. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  223. frame->nb_samples, avctx->sample_fmt,
  224. 32);
  225. if (buf_size < 0)
  226. return AVERROR(EINVAL);
  227. needs_extended_data = av_sample_fmt_is_planar(avctx->sample_fmt) &&
  228. avctx->channels > AV_NUM_DATA_POINTERS;
  229. /* allocate InternalBuffer if needed */
  230. if (!avci->buffer) {
  231. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  232. if (!avci->buffer)
  233. return AVERROR(ENOMEM);
  234. }
  235. buf = avci->buffer;
  236. /* if there is a previously-used internal buffer, check its size and
  237. channel count to see if we can reuse it */
  238. if (buf->extended_data) {
  239. /* if current buffer is too small, free it */
  240. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  241. av_free(buf->extended_data[0]);
  242. if (buf->extended_data != buf->data)
  243. av_free(&buf->extended_data);
  244. buf->extended_data = NULL;
  245. buf->data[0] = NULL;
  246. }
  247. /* if number of channels has changed, reset and/or free extended data
  248. pointers but leave data buffer in buf->data[0] for reuse */
  249. if (buf->nb_channels != avctx->channels) {
  250. if (buf->extended_data != buf->data)
  251. av_free(buf->extended_data);
  252. buf->extended_data = NULL;
  253. }
  254. }
  255. /* if there is no previous buffer or the previous buffer cannot be used
  256. as-is, allocate a new buffer and/or rearrange the channel pointers */
  257. if (!buf->extended_data) {
  258. /* if the channel pointers will fit, just set extended_data to data,
  259. otherwise allocate the extended_data channel pointers */
  260. if (needs_extended_data) {
  261. buf->extended_data = av_mallocz(avctx->channels *
  262. sizeof(*buf->extended_data));
  263. if (!buf->extended_data)
  264. return AVERROR(ENOMEM);
  265. } else {
  266. buf->extended_data = buf->data;
  267. }
  268. /* if there is a previous buffer and it is large enough, reuse it and
  269. just fill-in new channel pointers and linesize, otherwise allocate
  270. a new buffer */
  271. if (buf->extended_data[0]) {
  272. ret = av_samples_fill_arrays(buf->extended_data, &buf->linesize[0],
  273. buf->extended_data[0], avctx->channels,
  274. frame->nb_samples, avctx->sample_fmt,
  275. 32);
  276. } else {
  277. ret = av_samples_alloc(buf->extended_data, &buf->linesize[0],
  278. avctx->channels, frame->nb_samples,
  279. avctx->sample_fmt, 32);
  280. }
  281. if (ret)
  282. return ret;
  283. /* if data was not used for extended_data, we need to copy as many of
  284. the extended_data channel pointers as will fit */
  285. if (needs_extended_data) {
  286. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  287. buf->data[i] = buf->extended_data[i];
  288. }
  289. buf->audio_data_size = buf_size;
  290. buf->nb_channels = avctx->channels;
  291. }
  292. /* copy InternalBuffer info to the AVFrame */
  293. frame->type = FF_BUFFER_TYPE_INTERNAL;
  294. frame->extended_data = buf->extended_data;
  295. frame->linesize[0] = buf->linesize[0];
  296. memcpy(frame->data, buf->data, sizeof(frame->data));
  297. if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
  298. else frame->pkt_pts = AV_NOPTS_VALUE;
  299. frame->reordered_opaque = avctx->reordered_opaque;
  300. if (avctx->debug & FF_DEBUG_BUFFERS)
  301. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  302. "internal audio buffer used\n", frame);
  303. return 0;
  304. }
  305. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  306. {
  307. int i;
  308. int w= s->width;
  309. int h= s->height;
  310. InternalBuffer *buf;
  311. AVCodecInternal *avci = s->internal;
  312. if(pic->data[0]!=NULL) {
  313. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  314. return -1;
  315. }
  316. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  317. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  318. return -1;
  319. }
  320. if(av_image_check_size(w, h, 0, s))
  321. return -1;
  322. if (!avci->buffer) {
  323. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  324. sizeof(InternalBuffer));
  325. }
  326. buf = &avci->buffer[avci->buffer_count];
  327. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  328. if(s->active_thread_type&FF_THREAD_FRAME) {
  329. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  330. return -1;
  331. }
  332. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  333. av_freep(&buf->base[i]);
  334. buf->data[i]= NULL;
  335. }
  336. }
  337. if (!buf->base[0]) {
  338. int h_chroma_shift, v_chroma_shift;
  339. int size[4] = {0};
  340. int tmpsize;
  341. int unaligned;
  342. AVPicture picture;
  343. int stride_align[AV_NUM_DATA_POINTERS];
  344. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  345. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  346. avcodec_align_dimensions2(s, &w, &h, stride_align);
  347. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  348. w+= EDGE_WIDTH*2;
  349. h+= EDGE_WIDTH*2;
  350. }
  351. do {
  352. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  353. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  354. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  355. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  356. w += w & ~(w-1);
  357. unaligned = 0;
  358. for (i=0; i<4; i++){
  359. unaligned |= picture.linesize[i] % stride_align[i];
  360. }
  361. } while (unaligned);
  362. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  363. if (tmpsize < 0)
  364. return -1;
  365. for (i=0; i<3 && picture.data[i+1]; i++)
  366. size[i] = picture.data[i+1] - picture.data[i];
  367. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  368. memset(buf->base, 0, sizeof(buf->base));
  369. memset(buf->data, 0, sizeof(buf->data));
  370. for(i=0; i<4 && size[i]; i++){
  371. const int h_shift= i==0 ? 0 : h_chroma_shift;
  372. const int v_shift= i==0 ? 0 : v_chroma_shift;
  373. buf->linesize[i]= picture.linesize[i];
  374. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  375. if(buf->base[i]==NULL) return -1;
  376. memset(buf->base[i], 128, size[i]);
  377. // no edge if EDGE EMU or not planar YUV
  378. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  379. buf->data[i] = buf->base[i];
  380. else
  381. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  382. }
  383. for (; i < AV_NUM_DATA_POINTERS; i++) {
  384. buf->base[i] = buf->data[i] = NULL;
  385. buf->linesize[i] = 0;
  386. }
  387. if(size[1] && !size[2])
  388. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  389. buf->width = s->width;
  390. buf->height = s->height;
  391. buf->pix_fmt= s->pix_fmt;
  392. }
  393. pic->type= FF_BUFFER_TYPE_INTERNAL;
  394. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  395. pic->base[i]= buf->base[i];
  396. pic->data[i]= buf->data[i];
  397. pic->linesize[i]= buf->linesize[i];
  398. }
  399. pic->extended_data = pic->data;
  400. avci->buffer_count++;
  401. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  402. else pic->pkt_pts= AV_NOPTS_VALUE;
  403. pic->reordered_opaque= s->reordered_opaque;
  404. if(s->debug&FF_DEBUG_BUFFERS)
  405. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  406. "buffers used\n", pic, avci->buffer_count);
  407. return 0;
  408. }
  409. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  410. {
  411. switch (avctx->codec_type) {
  412. case AVMEDIA_TYPE_VIDEO:
  413. return video_get_buffer(avctx, frame);
  414. case AVMEDIA_TYPE_AUDIO:
  415. return audio_get_buffer(avctx, frame);
  416. default:
  417. return -1;
  418. }
  419. }
  420. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  421. int i;
  422. InternalBuffer *buf, *last;
  423. AVCodecInternal *avci = s->internal;
  424. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  425. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  426. assert(avci->buffer_count);
  427. if (avci->buffer) {
  428. buf = NULL; /* avoids warning */
  429. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  430. buf = &avci->buffer[i];
  431. if (buf->data[0] == pic->data[0])
  432. break;
  433. }
  434. assert(i < avci->buffer_count);
  435. avci->buffer_count--;
  436. last = &avci->buffer[avci->buffer_count];
  437. if (buf != last)
  438. FFSWAP(InternalBuffer, *buf, *last);
  439. }
  440. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  441. pic->data[i]=NULL;
  442. // pic->base[i]=NULL;
  443. }
  444. //printf("R%X\n", pic->opaque);
  445. if(s->debug&FF_DEBUG_BUFFERS)
  446. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  447. "buffers used\n", pic, avci->buffer_count);
  448. }
  449. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  450. AVFrame temp_pic;
  451. int i;
  452. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  453. /* If no picture return a new buffer */
  454. if(pic->data[0] == NULL) {
  455. /* We will copy from buffer, so must be readable */
  456. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  457. return s->get_buffer(s, pic);
  458. }
  459. /* If internal buffer type return the same buffer */
  460. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  461. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  462. else pic->pkt_pts= AV_NOPTS_VALUE;
  463. pic->reordered_opaque= s->reordered_opaque;
  464. return 0;
  465. }
  466. /*
  467. * Not internal type and reget_buffer not overridden, emulate cr buffer
  468. */
  469. temp_pic = *pic;
  470. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  471. pic->data[i] = pic->base[i] = NULL;
  472. pic->opaque = NULL;
  473. /* Allocate new frame */
  474. if (s->get_buffer(s, pic))
  475. return -1;
  476. /* Copy image data from old buffer to new buffer */
  477. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  478. s->height);
  479. s->release_buffer(s, &temp_pic); // Release old frame
  480. return 0;
  481. }
  482. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  483. int i;
  484. for(i=0; i<count; i++){
  485. int r= func(c, (char*)arg + i*size);
  486. if(ret) ret[i]= r;
  487. }
  488. return 0;
  489. }
  490. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  491. int i;
  492. for(i=0; i<count; i++){
  493. int r= func(c, arg, i, 0);
  494. if(ret) ret[i]= r;
  495. }
  496. return 0;
  497. }
  498. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  499. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  500. ++fmt;
  501. return fmt[0];
  502. }
  503. void avcodec_get_frame_defaults(AVFrame *pic){
  504. memset(pic, 0, sizeof(AVFrame));
  505. pic->pts= AV_NOPTS_VALUE;
  506. pic->key_frame= 1;
  507. pic->sample_aspect_ratio = (AVRational){0, 1};
  508. pic->format = -1; /* unknown */
  509. }
  510. AVFrame *avcodec_alloc_frame(void){
  511. AVFrame *pic= av_malloc(sizeof(AVFrame));
  512. if(pic==NULL) return NULL;
  513. avcodec_get_frame_defaults(pic);
  514. return pic;
  515. }
  516. #if FF_API_AVCODEC_OPEN
  517. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  518. {
  519. return avcodec_open2(avctx, codec, NULL);
  520. }
  521. #endif
  522. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  523. {
  524. int ret = 0;
  525. AVDictionary *tmp = NULL;
  526. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  527. return AVERROR(EINVAL);
  528. if (options)
  529. av_dict_copy(&tmp, *options, 0);
  530. /* If there is a user-supplied mutex locking routine, call it. */
  531. if (ff_lockmgr_cb) {
  532. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  533. return -1;
  534. }
  535. entangled_thread_counter++;
  536. if(entangled_thread_counter != 1){
  537. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  538. ret = -1;
  539. goto end;
  540. }
  541. if(avctx->codec || !codec) {
  542. ret = AVERROR(EINVAL);
  543. goto end;
  544. }
  545. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  546. if (!avctx->internal) {
  547. ret = AVERROR(ENOMEM);
  548. goto end;
  549. }
  550. if (codec->priv_data_size > 0) {
  551. if(!avctx->priv_data){
  552. avctx->priv_data = av_mallocz(codec->priv_data_size);
  553. if (!avctx->priv_data) {
  554. ret = AVERROR(ENOMEM);
  555. goto end;
  556. }
  557. if (codec->priv_class) {
  558. *(AVClass**)avctx->priv_data= codec->priv_class;
  559. av_opt_set_defaults(avctx->priv_data);
  560. }
  561. }
  562. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  563. goto free_and_end;
  564. } else {
  565. avctx->priv_data = NULL;
  566. }
  567. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  568. goto free_and_end;
  569. if(avctx->coded_width && avctx->coded_height)
  570. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  571. else if(avctx->width && avctx->height)
  572. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  573. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  574. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  575. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  576. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  577. avcodec_set_dimensions(avctx, 0, 0);
  578. }
  579. /* if the decoder init function was already called previously,
  580. free the already allocated subtitle_header before overwriting it */
  581. if (codec->decode)
  582. av_freep(&avctx->subtitle_header);
  583. #define SANE_NB_CHANNELS 128U
  584. if (avctx->channels > SANE_NB_CHANNELS) {
  585. ret = AVERROR(EINVAL);
  586. goto free_and_end;
  587. }
  588. avctx->codec = codec;
  589. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  590. avctx->codec_id == CODEC_ID_NONE) {
  591. avctx->codec_type = codec->type;
  592. avctx->codec_id = codec->id;
  593. }
  594. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  595. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  596. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  597. ret = AVERROR(EINVAL);
  598. goto free_and_end;
  599. }
  600. avctx->frame_number = 0;
  601. #if FF_API_ER
  602. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
  603. avctx->error_recognition, avctx->err_recognition);
  604. /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
  605. FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
  606. avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
  607. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
  608. avctx->error_recognition, avctx->err_recognition);
  609. #endif
  610. if (HAVE_THREADS && !avctx->thread_opaque) {
  611. ret = ff_thread_init(avctx);
  612. if (ret < 0) {
  613. goto free_and_end;
  614. }
  615. }
  616. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  617. avctx->thread_count = 1;
  618. if (avctx->codec->max_lowres < avctx->lowres) {
  619. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  620. avctx->codec->max_lowres);
  621. ret = AVERROR(EINVAL);
  622. goto free_and_end;
  623. }
  624. if (avctx->codec->encode) {
  625. int i;
  626. if (avctx->codec->sample_fmts) {
  627. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  628. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  629. break;
  630. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  631. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  632. ret = AVERROR(EINVAL);
  633. goto free_and_end;
  634. }
  635. }
  636. if (avctx->codec->supported_samplerates) {
  637. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  638. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  639. break;
  640. if (avctx->codec->supported_samplerates[i] == 0) {
  641. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  642. ret = AVERROR(EINVAL);
  643. goto free_and_end;
  644. }
  645. }
  646. if (avctx->codec->channel_layouts) {
  647. if (!avctx->channel_layout) {
  648. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  649. } else {
  650. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  651. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  652. break;
  653. if (avctx->codec->channel_layouts[i] == 0) {
  654. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  655. ret = AVERROR(EINVAL);
  656. goto free_and_end;
  657. }
  658. }
  659. }
  660. if (avctx->channel_layout && avctx->channels) {
  661. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  662. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  663. ret = AVERROR(EINVAL);
  664. goto free_and_end;
  665. }
  666. } else if (avctx->channel_layout) {
  667. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  668. }
  669. }
  670. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  671. ret = avctx->codec->init(avctx);
  672. if (ret < 0) {
  673. goto free_and_end;
  674. }
  675. }
  676. end:
  677. entangled_thread_counter--;
  678. /* Release any user-supplied mutex. */
  679. if (ff_lockmgr_cb) {
  680. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  681. }
  682. if (options) {
  683. av_dict_free(options);
  684. *options = tmp;
  685. }
  686. return ret;
  687. free_and_end:
  688. av_dict_free(&tmp);
  689. av_freep(&avctx->priv_data);
  690. av_freep(&avctx->internal);
  691. avctx->codec= NULL;
  692. goto end;
  693. }
  694. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  695. const short *samples)
  696. {
  697. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  698. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  699. return -1;
  700. }
  701. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  702. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  703. avctx->frame_number++;
  704. return ret;
  705. }else
  706. return 0;
  707. }
  708. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  709. const AVFrame *pict)
  710. {
  711. if(buf_size < FF_MIN_BUFFER_SIZE){
  712. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  713. return -1;
  714. }
  715. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  716. return -1;
  717. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  718. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  719. avctx->frame_number++;
  720. emms_c(); //needed to avoid an emms_c() call before every return;
  721. return ret;
  722. }else
  723. return 0;
  724. }
  725. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  726. const AVSubtitle *sub)
  727. {
  728. int ret;
  729. if(sub->start_display_time) {
  730. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  731. return -1;
  732. }
  733. if(sub->num_rects == 0 || !sub->rects)
  734. return -1;
  735. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  736. avctx->frame_number++;
  737. return ret;
  738. }
  739. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  740. {
  741. int size = 0;
  742. const uint8_t *data;
  743. uint32_t flags;
  744. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  745. return;
  746. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  747. if (!data || size < 4)
  748. return;
  749. flags = bytestream_get_le32(&data);
  750. size -= 4;
  751. if (size < 4) /* Required for any of the changes */
  752. return;
  753. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  754. avctx->channels = bytestream_get_le32(&data);
  755. size -= 4;
  756. }
  757. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  758. if (size < 8)
  759. return;
  760. avctx->channel_layout = bytestream_get_le64(&data);
  761. size -= 8;
  762. }
  763. if (size < 4)
  764. return;
  765. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  766. avctx->sample_rate = bytestream_get_le32(&data);
  767. size -= 4;
  768. }
  769. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  770. if (size < 8)
  771. return;
  772. avctx->width = bytestream_get_le32(&data);
  773. avctx->height = bytestream_get_le32(&data);
  774. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  775. size -= 8;
  776. }
  777. }
  778. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  779. int *got_picture_ptr,
  780. AVPacket *avpkt)
  781. {
  782. int ret;
  783. *got_picture_ptr= 0;
  784. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  785. return -1;
  786. avctx->pkt = avpkt;
  787. apply_param_change(avctx, avpkt);
  788. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  789. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  790. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  791. avpkt);
  792. else {
  793. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  794. avpkt);
  795. picture->pkt_dts= avpkt->dts;
  796. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  797. picture->width = avctx->width;
  798. picture->height = avctx->height;
  799. picture->format = avctx->pix_fmt;
  800. }
  801. emms_c(); //needed to avoid an emms_c() call before every return;
  802. if (*got_picture_ptr)
  803. avctx->frame_number++;
  804. }else
  805. ret= 0;
  806. return ret;
  807. }
  808. #if FF_API_OLD_DECODE_AUDIO
  809. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  810. int *frame_size_ptr,
  811. AVPacket *avpkt)
  812. {
  813. AVFrame frame;
  814. int ret, got_frame = 0;
  815. if (avctx->get_buffer != avcodec_default_get_buffer) {
  816. av_log(avctx, AV_LOG_ERROR, "A custom get_buffer() cannot be used with "
  817. "avcodec_decode_audio3()\n");
  818. return AVERROR(EINVAL);
  819. }
  820. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  821. if (ret >= 0 && got_frame) {
  822. int ch, plane_size;
  823. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  824. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  825. frame.nb_samples,
  826. avctx->sample_fmt, 1);
  827. if (*frame_size_ptr < data_size) {
  828. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  829. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  830. return AVERROR(EINVAL);
  831. }
  832. memcpy(samples, frame.extended_data[0], plane_size);
  833. if (planar && avctx->channels > 1) {
  834. uint8_t *out = ((uint8_t *)samples) + plane_size;
  835. for (ch = 1; ch < avctx->channels; ch++) {
  836. memcpy(out, frame.extended_data[ch], plane_size);
  837. out += plane_size;
  838. }
  839. }
  840. *frame_size_ptr = data_size;
  841. } else {
  842. *frame_size_ptr = 0;
  843. }
  844. return ret;
  845. }
  846. #endif
  847. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  848. AVFrame *frame,
  849. int *got_frame_ptr,
  850. AVPacket *avpkt)
  851. {
  852. int ret = 0;
  853. *got_frame_ptr = 0;
  854. avctx->pkt = avpkt;
  855. if (!avpkt->data && avpkt->size) {
  856. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  857. return AVERROR(EINVAL);
  858. }
  859. apply_param_change(avctx, avpkt);
  860. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  861. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  862. if (ret >= 0 && *got_frame_ptr) {
  863. avctx->frame_number++;
  864. frame->pkt_dts = avpkt->dts;
  865. if (frame->format == AV_SAMPLE_FMT_NONE)
  866. frame->format = avctx->sample_fmt;
  867. }
  868. }
  869. return ret;
  870. }
  871. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  872. int *got_sub_ptr,
  873. AVPacket *avpkt)
  874. {
  875. int ret;
  876. avctx->pkt = avpkt;
  877. *got_sub_ptr = 0;
  878. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  879. if (*got_sub_ptr)
  880. avctx->frame_number++;
  881. return ret;
  882. }
  883. void avsubtitle_free(AVSubtitle *sub)
  884. {
  885. int i;
  886. for (i = 0; i < sub->num_rects; i++)
  887. {
  888. av_freep(&sub->rects[i]->pict.data[0]);
  889. av_freep(&sub->rects[i]->pict.data[1]);
  890. av_freep(&sub->rects[i]->pict.data[2]);
  891. av_freep(&sub->rects[i]->pict.data[3]);
  892. av_freep(&sub->rects[i]->text);
  893. av_freep(&sub->rects[i]->ass);
  894. av_freep(&sub->rects[i]);
  895. }
  896. av_freep(&sub->rects);
  897. memset(sub, 0, sizeof(AVSubtitle));
  898. }
  899. av_cold int avcodec_close(AVCodecContext *avctx)
  900. {
  901. /* If there is a user-supplied mutex locking routine, call it. */
  902. if (ff_lockmgr_cb) {
  903. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  904. return -1;
  905. }
  906. entangled_thread_counter++;
  907. if(entangled_thread_counter != 1){
  908. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  909. entangled_thread_counter--;
  910. return -1;
  911. }
  912. if (HAVE_THREADS && avctx->thread_opaque)
  913. ff_thread_free(avctx);
  914. if (avctx->codec && avctx->codec->close)
  915. avctx->codec->close(avctx);
  916. avcodec_default_free_buffers(avctx);
  917. avctx->coded_frame = NULL;
  918. av_freep(&avctx->internal);
  919. if (avctx->codec && avctx->codec->priv_class)
  920. av_opt_free(avctx->priv_data);
  921. av_opt_free(avctx);
  922. av_freep(&avctx->priv_data);
  923. if(avctx->codec && avctx->codec->encode)
  924. av_freep(&avctx->extradata);
  925. avctx->codec = NULL;
  926. avctx->active_thread_type = 0;
  927. entangled_thread_counter--;
  928. /* Release any user-supplied mutex. */
  929. if (ff_lockmgr_cb) {
  930. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  931. }
  932. return 0;
  933. }
  934. AVCodec *avcodec_find_encoder(enum CodecID id)
  935. {
  936. AVCodec *p, *experimental=NULL;
  937. p = first_avcodec;
  938. while (p) {
  939. if (p->encode != NULL && p->id == id) {
  940. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  941. experimental = p;
  942. } else
  943. return p;
  944. }
  945. p = p->next;
  946. }
  947. return experimental;
  948. }
  949. AVCodec *avcodec_find_encoder_by_name(const char *name)
  950. {
  951. AVCodec *p;
  952. if (!name)
  953. return NULL;
  954. p = first_avcodec;
  955. while (p) {
  956. if (p->encode != NULL && strcmp(name,p->name) == 0)
  957. return p;
  958. p = p->next;
  959. }
  960. return NULL;
  961. }
  962. AVCodec *avcodec_find_decoder(enum CodecID id)
  963. {
  964. AVCodec *p;
  965. p = first_avcodec;
  966. while (p) {
  967. if (p->decode != NULL && p->id == id)
  968. return p;
  969. p = p->next;
  970. }
  971. return NULL;
  972. }
  973. AVCodec *avcodec_find_decoder_by_name(const char *name)
  974. {
  975. AVCodec *p;
  976. if (!name)
  977. return NULL;
  978. p = first_avcodec;
  979. while (p) {
  980. if (p->decode != NULL && strcmp(name,p->name) == 0)
  981. return p;
  982. p = p->next;
  983. }
  984. return NULL;
  985. }
  986. static int get_bit_rate(AVCodecContext *ctx)
  987. {
  988. int bit_rate;
  989. int bits_per_sample;
  990. switch(ctx->codec_type) {
  991. case AVMEDIA_TYPE_VIDEO:
  992. case AVMEDIA_TYPE_DATA:
  993. case AVMEDIA_TYPE_SUBTITLE:
  994. case AVMEDIA_TYPE_ATTACHMENT:
  995. bit_rate = ctx->bit_rate;
  996. break;
  997. case AVMEDIA_TYPE_AUDIO:
  998. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  999. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1000. break;
  1001. default:
  1002. bit_rate = 0;
  1003. break;
  1004. }
  1005. return bit_rate;
  1006. }
  1007. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1008. {
  1009. int i, len, ret = 0;
  1010. for (i = 0; i < 4; i++) {
  1011. len = snprintf(buf, buf_size,
  1012. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1013. buf += len;
  1014. buf_size = buf_size > len ? buf_size - len : 0;
  1015. ret += len;
  1016. codec_tag>>=8;
  1017. }
  1018. return ret;
  1019. }
  1020. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1021. {
  1022. const char *codec_name;
  1023. const char *profile = NULL;
  1024. AVCodec *p;
  1025. char buf1[32];
  1026. int bitrate;
  1027. AVRational display_aspect_ratio;
  1028. if (encode)
  1029. p = avcodec_find_encoder(enc->codec_id);
  1030. else
  1031. p = avcodec_find_decoder(enc->codec_id);
  1032. if (p) {
  1033. codec_name = p->name;
  1034. profile = av_get_profile_name(p, enc->profile);
  1035. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  1036. /* fake mpeg2 transport stream codec (currently not
  1037. registered) */
  1038. codec_name = "mpeg2ts";
  1039. } else if (enc->codec_name[0] != '\0') {
  1040. codec_name = enc->codec_name;
  1041. } else {
  1042. /* output avi tags */
  1043. char tag_buf[32];
  1044. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1045. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1046. codec_name = buf1;
  1047. }
  1048. switch(enc->codec_type) {
  1049. case AVMEDIA_TYPE_VIDEO:
  1050. snprintf(buf, buf_size,
  1051. "Video: %s%s",
  1052. codec_name, enc->mb_decision ? " (hq)" : "");
  1053. if (profile)
  1054. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1055. " (%s)", profile);
  1056. if (enc->pix_fmt != PIX_FMT_NONE) {
  1057. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1058. ", %s",
  1059. av_get_pix_fmt_name(enc->pix_fmt));
  1060. }
  1061. if (enc->width) {
  1062. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1063. ", %dx%d",
  1064. enc->width, enc->height);
  1065. if (enc->sample_aspect_ratio.num) {
  1066. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1067. enc->width*enc->sample_aspect_ratio.num,
  1068. enc->height*enc->sample_aspect_ratio.den,
  1069. 1024*1024);
  1070. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1071. " [PAR %d:%d DAR %d:%d]",
  1072. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1073. display_aspect_ratio.num, display_aspect_ratio.den);
  1074. }
  1075. if(av_log_get_level() >= AV_LOG_DEBUG){
  1076. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1077. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1078. ", %d/%d",
  1079. enc->time_base.num/g, enc->time_base.den/g);
  1080. }
  1081. }
  1082. if (encode) {
  1083. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1084. ", q=%d-%d", enc->qmin, enc->qmax);
  1085. }
  1086. break;
  1087. case AVMEDIA_TYPE_AUDIO:
  1088. snprintf(buf, buf_size,
  1089. "Audio: %s",
  1090. codec_name);
  1091. if (profile)
  1092. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1093. " (%s)", profile);
  1094. if (enc->sample_rate) {
  1095. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1096. ", %d Hz", enc->sample_rate);
  1097. }
  1098. av_strlcat(buf, ", ", buf_size);
  1099. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1100. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1101. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1102. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1103. }
  1104. break;
  1105. case AVMEDIA_TYPE_DATA:
  1106. snprintf(buf, buf_size, "Data: %s", codec_name);
  1107. break;
  1108. case AVMEDIA_TYPE_SUBTITLE:
  1109. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1110. break;
  1111. case AVMEDIA_TYPE_ATTACHMENT:
  1112. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1113. break;
  1114. default:
  1115. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1116. return;
  1117. }
  1118. if (encode) {
  1119. if (enc->flags & CODEC_FLAG_PASS1)
  1120. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1121. ", pass 1");
  1122. if (enc->flags & CODEC_FLAG_PASS2)
  1123. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1124. ", pass 2");
  1125. }
  1126. bitrate = get_bit_rate(enc);
  1127. if (bitrate != 0) {
  1128. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1129. ", %d kb/s", bitrate / 1000);
  1130. }
  1131. }
  1132. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1133. {
  1134. const AVProfile *p;
  1135. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1136. return NULL;
  1137. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1138. if (p->profile == profile)
  1139. return p->name;
  1140. return NULL;
  1141. }
  1142. unsigned avcodec_version( void )
  1143. {
  1144. return LIBAVCODEC_VERSION_INT;
  1145. }
  1146. const char *avcodec_configuration(void)
  1147. {
  1148. return LIBAV_CONFIGURATION;
  1149. }
  1150. const char *avcodec_license(void)
  1151. {
  1152. #define LICENSE_PREFIX "libavcodec license: "
  1153. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1154. }
  1155. void avcodec_flush_buffers(AVCodecContext *avctx)
  1156. {
  1157. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1158. ff_thread_flush(avctx);
  1159. else if(avctx->codec->flush)
  1160. avctx->codec->flush(avctx);
  1161. }
  1162. static void video_free_buffers(AVCodecContext *s)
  1163. {
  1164. AVCodecInternal *avci = s->internal;
  1165. int i, j;
  1166. if (!avci->buffer)
  1167. return;
  1168. if (avci->buffer_count)
  1169. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1170. avci->buffer_count);
  1171. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1172. InternalBuffer *buf = &avci->buffer[i];
  1173. for(j=0; j<4; j++){
  1174. av_freep(&buf->base[j]);
  1175. buf->data[j]= NULL;
  1176. }
  1177. }
  1178. av_freep(&avci->buffer);
  1179. avci->buffer_count=0;
  1180. }
  1181. static void audio_free_buffers(AVCodecContext *avctx)
  1182. {
  1183. AVCodecInternal *avci = avctx->internal;
  1184. InternalBuffer *buf;
  1185. if (!avci->buffer)
  1186. return;
  1187. buf = avci->buffer;
  1188. if (buf->extended_data) {
  1189. av_free(buf->extended_data[0]);
  1190. if (buf->extended_data != buf->data)
  1191. av_free(buf->extended_data);
  1192. }
  1193. av_freep(&avci->buffer);
  1194. }
  1195. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1196. {
  1197. switch (avctx->codec_type) {
  1198. case AVMEDIA_TYPE_VIDEO:
  1199. video_free_buffers(avctx);
  1200. break;
  1201. case AVMEDIA_TYPE_AUDIO:
  1202. audio_free_buffers(avctx);
  1203. break;
  1204. default:
  1205. break;
  1206. }
  1207. }
  1208. #if FF_API_OLD_FF_PICT_TYPES
  1209. char av_get_pict_type_char(int pict_type){
  1210. return av_get_picture_type_char(pict_type);
  1211. }
  1212. #endif
  1213. int av_get_bits_per_sample(enum CodecID codec_id){
  1214. switch(codec_id){
  1215. case CODEC_ID_ADPCM_SBPRO_2:
  1216. return 2;
  1217. case CODEC_ID_ADPCM_SBPRO_3:
  1218. return 3;
  1219. case CODEC_ID_ADPCM_SBPRO_4:
  1220. case CODEC_ID_ADPCM_CT:
  1221. case CODEC_ID_ADPCM_IMA_WAV:
  1222. case CODEC_ID_ADPCM_IMA_QT:
  1223. case CODEC_ID_ADPCM_SWF:
  1224. case CODEC_ID_ADPCM_MS:
  1225. case CODEC_ID_ADPCM_YAMAHA:
  1226. case CODEC_ID_ADPCM_G722:
  1227. return 4;
  1228. case CODEC_ID_PCM_ALAW:
  1229. case CODEC_ID_PCM_MULAW:
  1230. case CODEC_ID_PCM_S8:
  1231. case CODEC_ID_PCM_U8:
  1232. case CODEC_ID_PCM_ZORK:
  1233. return 8;
  1234. case CODEC_ID_PCM_S16BE:
  1235. case CODEC_ID_PCM_S16LE:
  1236. case CODEC_ID_PCM_S16LE_PLANAR:
  1237. case CODEC_ID_PCM_U16BE:
  1238. case CODEC_ID_PCM_U16LE:
  1239. return 16;
  1240. case CODEC_ID_PCM_S24DAUD:
  1241. case CODEC_ID_PCM_S24BE:
  1242. case CODEC_ID_PCM_S24LE:
  1243. case CODEC_ID_PCM_U24BE:
  1244. case CODEC_ID_PCM_U24LE:
  1245. return 24;
  1246. case CODEC_ID_PCM_S32BE:
  1247. case CODEC_ID_PCM_S32LE:
  1248. case CODEC_ID_PCM_U32BE:
  1249. case CODEC_ID_PCM_U32LE:
  1250. case CODEC_ID_PCM_F32BE:
  1251. case CODEC_ID_PCM_F32LE:
  1252. return 32;
  1253. case CODEC_ID_PCM_F64BE:
  1254. case CODEC_ID_PCM_F64LE:
  1255. return 64;
  1256. default:
  1257. return 0;
  1258. }
  1259. }
  1260. #if FF_API_OLD_SAMPLE_FMT
  1261. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1262. return av_get_bytes_per_sample(sample_fmt) << 3;
  1263. }
  1264. #endif
  1265. #if !HAVE_THREADS
  1266. int ff_thread_init(AVCodecContext *s){
  1267. return -1;
  1268. }
  1269. #endif
  1270. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1271. {
  1272. unsigned int n = 0;
  1273. while(v >= 0xff) {
  1274. *s++ = 0xff;
  1275. v -= 0xff;
  1276. n++;
  1277. }
  1278. *s = v;
  1279. n++;
  1280. return n;
  1281. }
  1282. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1283. int i;
  1284. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1285. return i;
  1286. }
  1287. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1288. {
  1289. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1290. "version to the newest one from Git. If the problem still "
  1291. "occurs, it means that your file has a feature which has not "
  1292. "been implemented.\n", feature);
  1293. if(want_sample)
  1294. av_log_ask_for_sample(avc, NULL);
  1295. }
  1296. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1297. {
  1298. va_list argument_list;
  1299. va_start(argument_list, msg);
  1300. if (msg)
  1301. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1302. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1303. "of this file to ftp://upload.libav.org/incoming/ "
  1304. "and contact the libav-devel mailing list.\n");
  1305. va_end(argument_list);
  1306. }
  1307. static AVHWAccel *first_hwaccel = NULL;
  1308. void av_register_hwaccel(AVHWAccel *hwaccel)
  1309. {
  1310. AVHWAccel **p = &first_hwaccel;
  1311. while (*p)
  1312. p = &(*p)->next;
  1313. *p = hwaccel;
  1314. hwaccel->next = NULL;
  1315. }
  1316. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1317. {
  1318. return hwaccel ? hwaccel->next : first_hwaccel;
  1319. }
  1320. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1321. {
  1322. AVHWAccel *hwaccel=NULL;
  1323. while((hwaccel= av_hwaccel_next(hwaccel))){
  1324. if ( hwaccel->id == codec_id
  1325. && hwaccel->pix_fmt == pix_fmt)
  1326. return hwaccel;
  1327. }
  1328. return NULL;
  1329. }
  1330. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1331. {
  1332. if (ff_lockmgr_cb) {
  1333. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1334. return -1;
  1335. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1336. return -1;
  1337. }
  1338. ff_lockmgr_cb = cb;
  1339. if (ff_lockmgr_cb) {
  1340. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1341. return -1;
  1342. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1343. return -1;
  1344. }
  1345. return 0;
  1346. }
  1347. int avpriv_lock_avformat(void)
  1348. {
  1349. if (ff_lockmgr_cb) {
  1350. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1351. return -1;
  1352. }
  1353. return 0;
  1354. }
  1355. int avpriv_unlock_avformat(void)
  1356. {
  1357. if (ff_lockmgr_cb) {
  1358. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1359. return -1;
  1360. }
  1361. return 0;
  1362. }
  1363. unsigned int avpriv_toupper4(unsigned int x)
  1364. {
  1365. return toupper( x &0xFF)
  1366. + (toupper((x>>8 )&0xFF)<<8 )
  1367. + (toupper((x>>16)&0xFF)<<16)
  1368. + (toupper((x>>24)&0xFF)<<24);
  1369. }
  1370. #if !HAVE_THREADS
  1371. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1372. {
  1373. f->owner = avctx;
  1374. return avctx->get_buffer(avctx, f);
  1375. }
  1376. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1377. {
  1378. f->owner->release_buffer(f->owner, f);
  1379. }
  1380. void ff_thread_finish_setup(AVCodecContext *avctx)
  1381. {
  1382. }
  1383. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1384. {
  1385. }
  1386. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1387. {
  1388. }
  1389. #endif
  1390. #if FF_API_THREAD_INIT
  1391. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1392. {
  1393. s->thread_count = thread_count;
  1394. return ff_thread_init(s);
  1395. }
  1396. #endif
  1397. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1398. {
  1399. if (codec_id <= CODEC_ID_NONE)
  1400. return AVMEDIA_TYPE_UNKNOWN;
  1401. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1402. return AVMEDIA_TYPE_VIDEO;
  1403. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1404. return AVMEDIA_TYPE_AUDIO;
  1405. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1406. return AVMEDIA_TYPE_SUBTITLE;
  1407. return AVMEDIA_TYPE_UNKNOWN;
  1408. }