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.

1422 lines
42KB

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