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.

1185 lines
34KB

  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/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavcore/imgutils.h"
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "opt.h"
  34. #include "imgconvert.h"
  35. #include "audioconvert.h"
  36. #include "internal.h"
  37. #include <stdlib.h>
  38. #include <stdarg.h>
  39. #include <limits.h>
  40. #include <float.h>
  41. static int volatile entangled_thread_counter=0;
  42. int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  43. static void *codec_mutex;
  44. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  45. {
  46. if(min_size < *size)
  47. return ptr;
  48. *size= FFMAX(17*min_size/16 + 32, min_size);
  49. ptr= av_realloc(ptr, *size);
  50. 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
  51. *size= 0;
  52. return ptr;
  53. }
  54. void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size)
  55. {
  56. void **p = ptr;
  57. if (min_size < *size)
  58. return;
  59. *size= FFMAX(17*min_size/16 + 32, min_size);
  60. av_free(*p);
  61. *p = av_malloc(*size);
  62. if (!*p) *size = 0;
  63. }
  64. /* encoder management */
  65. static AVCodec *first_avcodec = NULL;
  66. AVCodec *av_codec_next(AVCodec *c){
  67. if(c) return c->next;
  68. else return first_avcodec;
  69. }
  70. void avcodec_register(AVCodec *codec)
  71. {
  72. AVCodec **p;
  73. avcodec_init();
  74. p = &first_avcodec;
  75. while (*p != NULL) p = &(*p)->next;
  76. *p = codec;
  77. codec->next = NULL;
  78. }
  79. #if LIBAVCODEC_VERSION_MAJOR < 53
  80. void register_avcodec(AVCodec *codec)
  81. {
  82. avcodec_register(codec);
  83. }
  84. #endif
  85. unsigned avcodec_get_edge_width(void)
  86. {
  87. return EDGE_WIDTH;
  88. }
  89. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  90. s->coded_width = width;
  91. s->coded_height= height;
  92. s->width = -((-width )>>s->lowres);
  93. s->height= -((-height)>>s->lowres);
  94. }
  95. typedef struct InternalBuffer{
  96. int last_pic_num;
  97. uint8_t *base[4];
  98. uint8_t *data[4];
  99. int linesize[4];
  100. int width, height;
  101. enum PixelFormat pix_fmt;
  102. }InternalBuffer;
  103. #define INTERNAL_BUFFER_SIZE 32
  104. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  105. int w_align= 1;
  106. int h_align= 1;
  107. switch(s->pix_fmt){
  108. case PIX_FMT_YUV420P:
  109. case PIX_FMT_YUYV422:
  110. case PIX_FMT_UYVY422:
  111. case PIX_FMT_YUV422P:
  112. case PIX_FMT_YUV440P:
  113. case PIX_FMT_YUV444P:
  114. case PIX_FMT_GRAY8:
  115. case PIX_FMT_GRAY16BE:
  116. case PIX_FMT_GRAY16LE:
  117. case PIX_FMT_YUVJ420P:
  118. case PIX_FMT_YUVJ422P:
  119. case PIX_FMT_YUVJ440P:
  120. case PIX_FMT_YUVJ444P:
  121. case PIX_FMT_YUVA420P:
  122. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  123. h_align= 16;
  124. 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)
  125. h_align= 32; // interlaced is rounded up to 2 MBs
  126. break;
  127. case PIX_FMT_YUV411P:
  128. case PIX_FMT_UYYVYY411:
  129. w_align=32;
  130. h_align=8;
  131. break;
  132. case PIX_FMT_YUV410P:
  133. if(s->codec_id == CODEC_ID_SVQ1){
  134. w_align=64;
  135. h_align=64;
  136. }
  137. case PIX_FMT_RGB555:
  138. if(s->codec_id == CODEC_ID_RPZA){
  139. w_align=4;
  140. h_align=4;
  141. }
  142. case PIX_FMT_PAL8:
  143. case PIX_FMT_BGR8:
  144. case PIX_FMT_RGB8:
  145. if(s->codec_id == CODEC_ID_SMC){
  146. w_align=4;
  147. h_align=4;
  148. }
  149. break;
  150. case PIX_FMT_BGR24:
  151. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  152. w_align=4;
  153. h_align=4;
  154. }
  155. break;
  156. default:
  157. w_align= 1;
  158. h_align= 1;
  159. break;
  160. }
  161. *width = FFALIGN(*width , w_align);
  162. *height= FFALIGN(*height, h_align);
  163. if(s->codec_id == CODEC_ID_H264)
  164. *height+=2; // some of the optimized chroma MC reads one line too much
  165. linesize_align[0] =
  166. linesize_align[1] =
  167. linesize_align[2] =
  168. linesize_align[3] = STRIDE_ALIGN;
  169. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  170. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  171. //picture size unneccessarily in some cases. The solution here is not
  172. //pretty and better ideas are welcome!
  173. #if HAVE_MMX
  174. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  175. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  176. s->codec_id == CODEC_ID_VP6A) {
  177. linesize_align[0] =
  178. linesize_align[1] =
  179. linesize_align[2] = 16;
  180. }
  181. #endif
  182. }
  183. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  184. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  185. int linesize_align[4];
  186. int align;
  187. avcodec_align_dimensions2(s, width, height, linesize_align);
  188. align = FFMAX(linesize_align[0], linesize_align[3]);
  189. linesize_align[1] <<= chroma_shift;
  190. linesize_align[2] <<= chroma_shift;
  191. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  192. *width=FFALIGN(*width, align);
  193. }
  194. #if LIBAVCODEC_VERSION_MAJOR < 53
  195. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  196. return av_image_check_size(w, h, 0, av_log_ctx);
  197. }
  198. #endif
  199. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  200. int i;
  201. int w= s->width;
  202. int h= s->height;
  203. InternalBuffer *buf;
  204. int *picture_number;
  205. if(pic->data[0]!=NULL) {
  206. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  207. return -1;
  208. }
  209. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  210. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  211. return -1;
  212. }
  213. if(av_image_check_size(w, h, 0, s))
  214. return -1;
  215. if(s->internal_buffer==NULL){
  216. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  217. }
  218. #if 0
  219. s->internal_buffer= av_fast_realloc(
  220. s->internal_buffer,
  221. &s->internal_buffer_size,
  222. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  223. );
  224. #endif
  225. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  226. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  227. (*picture_number)++;
  228. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  229. for(i=0; i<4; i++){
  230. av_freep(&buf->base[i]);
  231. buf->data[i]= NULL;
  232. }
  233. }
  234. if(buf->base[0]){
  235. pic->age= *picture_number - buf->last_pic_num;
  236. buf->last_pic_num= *picture_number;
  237. }else{
  238. int h_chroma_shift, v_chroma_shift;
  239. int size[4] = {0};
  240. int tmpsize;
  241. int unaligned;
  242. AVPicture picture;
  243. int stride_align[4];
  244. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  245. avcodec_align_dimensions2(s, &w, &h, stride_align);
  246. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  247. w+= EDGE_WIDTH*2;
  248. h+= EDGE_WIDTH*2;
  249. }
  250. do {
  251. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  252. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  253. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  254. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  255. w += w & ~(w-1);
  256. unaligned = 0;
  257. for (i=0; i<4; i++){
  258. unaligned |= picture.linesize[i] % stride_align[i];
  259. }
  260. } while (unaligned);
  261. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  262. if (tmpsize < 0)
  263. return -1;
  264. for (i=0; i<3 && picture.data[i+1]; i++)
  265. size[i] = picture.data[i+1] - picture.data[i];
  266. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  267. buf->last_pic_num= -256*256*256*64;
  268. memset(buf->base, 0, sizeof(buf->base));
  269. memset(buf->data, 0, sizeof(buf->data));
  270. for(i=0; i<4 && size[i]; i++){
  271. const int h_shift= i==0 ? 0 : h_chroma_shift;
  272. const int v_shift= i==0 ? 0 : v_chroma_shift;
  273. buf->linesize[i]= picture.linesize[i];
  274. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  275. if(buf->base[i]==NULL) return -1;
  276. memset(buf->base[i], 128, size[i]);
  277. // no edge if EDGE EMU or not planar YUV
  278. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  279. buf->data[i] = buf->base[i];
  280. else
  281. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  282. }
  283. if(size[1] && !size[2])
  284. ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
  285. buf->width = s->width;
  286. buf->height = s->height;
  287. buf->pix_fmt= s->pix_fmt;
  288. pic->age= 256*256*256*64;
  289. }
  290. pic->type= FF_BUFFER_TYPE_INTERNAL;
  291. for(i=0; i<4; i++){
  292. pic->base[i]= buf->base[i];
  293. pic->data[i]= buf->data[i];
  294. pic->linesize[i]= buf->linesize[i];
  295. }
  296. s->internal_buffer_count++;
  297. pic->reordered_opaque= s->reordered_opaque;
  298. if(s->debug&FF_DEBUG_BUFFERS)
  299. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  300. return 0;
  301. }
  302. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  303. int i;
  304. InternalBuffer *buf, *last;
  305. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  306. assert(s->internal_buffer_count);
  307. buf = NULL; /* avoids warning */
  308. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  309. buf= &((InternalBuffer*)s->internal_buffer)[i];
  310. if(buf->data[0] == pic->data[0])
  311. break;
  312. }
  313. assert(i < s->internal_buffer_count);
  314. s->internal_buffer_count--;
  315. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  316. FFSWAP(InternalBuffer, *buf, *last);
  317. for(i=0; i<4; i++){
  318. pic->data[i]=NULL;
  319. // pic->base[i]=NULL;
  320. }
  321. //printf("R%X\n", pic->opaque);
  322. if(s->debug&FF_DEBUG_BUFFERS)
  323. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  324. }
  325. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  326. AVFrame temp_pic;
  327. int i;
  328. /* If no picture return a new buffer */
  329. if(pic->data[0] == NULL) {
  330. /* We will copy from buffer, so must be readable */
  331. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  332. return s->get_buffer(s, pic);
  333. }
  334. /* If internal buffer type return the same buffer */
  335. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  336. pic->reordered_opaque= s->reordered_opaque;
  337. return 0;
  338. }
  339. /*
  340. * Not internal type and reget_buffer not overridden, emulate cr buffer
  341. */
  342. temp_pic = *pic;
  343. for(i = 0; i < 4; i++)
  344. pic->data[i] = pic->base[i] = NULL;
  345. pic->opaque = NULL;
  346. /* Allocate new frame */
  347. if (s->get_buffer(s, pic))
  348. return -1;
  349. /* Copy image data from old buffer to new buffer */
  350. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  351. s->height);
  352. s->release_buffer(s, &temp_pic); // Release old frame
  353. return 0;
  354. }
  355. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  356. int i;
  357. for(i=0; i<count; i++){
  358. int r= func(c, (char*)arg + i*size);
  359. if(ret) ret[i]= r;
  360. }
  361. return 0;
  362. }
  363. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  364. int i;
  365. for(i=0; i<count; i++){
  366. int r= func(c, arg, i, 0);
  367. if(ret) ret[i]= r;
  368. }
  369. return 0;
  370. }
  371. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  372. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  373. ++fmt;
  374. return fmt[0];
  375. }
  376. void avcodec_get_frame_defaults(AVFrame *pic){
  377. memset(pic, 0, sizeof(AVFrame));
  378. pic->pts= AV_NOPTS_VALUE;
  379. pic->key_frame= 1;
  380. }
  381. AVFrame *avcodec_alloc_frame(void){
  382. AVFrame *pic= av_malloc(sizeof(AVFrame));
  383. if(pic==NULL) return NULL;
  384. avcodec_get_frame_defaults(pic);
  385. return pic;
  386. }
  387. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  388. {
  389. int ret= -1;
  390. /* If there is a user-supplied mutex locking routine, call it. */
  391. if (ff_lockmgr_cb) {
  392. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  393. return -1;
  394. }
  395. entangled_thread_counter++;
  396. if(entangled_thread_counter != 1){
  397. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  398. goto end;
  399. }
  400. if(avctx->codec || !codec)
  401. goto end;
  402. if (codec->priv_data_size > 0) {
  403. avctx->priv_data = av_mallocz(codec->priv_data_size);
  404. if (!avctx->priv_data) {
  405. ret = AVERROR(ENOMEM);
  406. goto end;
  407. }
  408. } else {
  409. avctx->priv_data = NULL;
  410. }
  411. if(avctx->coded_width && avctx->coded_height)
  412. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  413. else if(avctx->width && avctx->height)
  414. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  415. #define SANE_NB_CHANNELS 128U
  416. if (((avctx->coded_width || avctx->coded_height)
  417. && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  418. || avctx->channels > SANE_NB_CHANNELS) {
  419. ret = AVERROR(EINVAL);
  420. goto free_and_end;
  421. }
  422. avctx->codec = codec;
  423. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  424. avctx->codec_id == CODEC_ID_NONE) {
  425. avctx->codec_type = codec->type;
  426. avctx->codec_id = codec->id;
  427. }
  428. if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
  429. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  430. goto free_and_end;
  431. }
  432. avctx->frame_number = 0;
  433. if(avctx->codec->init){
  434. if (avctx->codec->max_lowres < avctx->lowres) {
  435. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  436. avctx->codec->max_lowres);
  437. goto free_and_end;
  438. }
  439. ret = avctx->codec->init(avctx);
  440. if (ret < 0) {
  441. goto free_and_end;
  442. }
  443. }
  444. ret=0;
  445. end:
  446. entangled_thread_counter--;
  447. /* Release any user-supplied mutex. */
  448. if (ff_lockmgr_cb) {
  449. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  450. }
  451. return ret;
  452. free_and_end:
  453. av_freep(&avctx->priv_data);
  454. avctx->codec= NULL;
  455. goto end;
  456. }
  457. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  458. const short *samples)
  459. {
  460. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  461. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  462. return -1;
  463. }
  464. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  465. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  466. avctx->frame_number++;
  467. return ret;
  468. }else
  469. return 0;
  470. }
  471. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  472. const AVFrame *pict)
  473. {
  474. if(buf_size < FF_MIN_BUFFER_SIZE){
  475. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  476. return -1;
  477. }
  478. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  479. return -1;
  480. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  481. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  482. avctx->frame_number++;
  483. emms_c(); //needed to avoid an emms_c() call before every return;
  484. return ret;
  485. }else
  486. return 0;
  487. }
  488. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  489. const AVSubtitle *sub)
  490. {
  491. int ret;
  492. if(sub->start_display_time) {
  493. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  494. return -1;
  495. }
  496. if(sub->num_rects == 0 || !sub->rects)
  497. return -1;
  498. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  499. avctx->frame_number++;
  500. return ret;
  501. }
  502. #if LIBAVCODEC_VERSION_MAJOR < 53
  503. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  504. int *got_picture_ptr,
  505. const uint8_t *buf, int buf_size)
  506. {
  507. AVPacket avpkt;
  508. av_init_packet(&avpkt);
  509. avpkt.data = buf;
  510. avpkt.size = buf_size;
  511. // HACK for CorePNG to decode as normal PNG by default
  512. avpkt.flags = AV_PKT_FLAG_KEY;
  513. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  514. }
  515. #endif
  516. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  517. int *got_picture_ptr,
  518. AVPacket *avpkt)
  519. {
  520. int ret;
  521. *got_picture_ptr= 0;
  522. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  523. return -1;
  524. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  525. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  526. avpkt);
  527. emms_c(); //needed to avoid an emms_c() call before every return;
  528. if (*got_picture_ptr)
  529. avctx->frame_number++;
  530. }else
  531. ret= 0;
  532. return ret;
  533. }
  534. #if LIBAVCODEC_VERSION_MAJOR < 53
  535. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  536. int *frame_size_ptr,
  537. const uint8_t *buf, int buf_size)
  538. {
  539. AVPacket avpkt;
  540. av_init_packet(&avpkt);
  541. avpkt.data = buf;
  542. avpkt.size = buf_size;
  543. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  544. }
  545. #endif
  546. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  547. int *frame_size_ptr,
  548. AVPacket *avpkt)
  549. {
  550. int ret;
  551. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  552. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  553. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  554. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  555. return -1;
  556. }
  557. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  558. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  559. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  560. return -1;
  561. }
  562. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  563. avctx->frame_number++;
  564. }else{
  565. ret= 0;
  566. *frame_size_ptr=0;
  567. }
  568. return ret;
  569. }
  570. #if LIBAVCODEC_VERSION_MAJOR < 53
  571. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  572. int *got_sub_ptr,
  573. const uint8_t *buf, int buf_size)
  574. {
  575. AVPacket avpkt;
  576. av_init_packet(&avpkt);
  577. avpkt.data = buf;
  578. avpkt.size = buf_size;
  579. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  580. }
  581. #endif
  582. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  583. int *got_sub_ptr,
  584. AVPacket *avpkt)
  585. {
  586. int ret;
  587. *got_sub_ptr = 0;
  588. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  589. if (*got_sub_ptr)
  590. avctx->frame_number++;
  591. return ret;
  592. }
  593. void avsubtitle_free(AVSubtitle *sub)
  594. {
  595. int i;
  596. for (i = 0; i < sub->num_rects; i++)
  597. {
  598. av_freep(&sub->rects[i]->pict.data[0]);
  599. av_freep(&sub->rects[i]->pict.data[1]);
  600. av_freep(&sub->rects[i]->pict.data[2]);
  601. av_freep(&sub->rects[i]->pict.data[3]);
  602. av_freep(&sub->rects[i]->text);
  603. av_freep(&sub->rects[i]->ass);
  604. av_freep(&sub->rects[i]);
  605. }
  606. av_freep(&sub->rects);
  607. memset(sub, 0, sizeof(AVSubtitle));
  608. }
  609. av_cold int avcodec_close(AVCodecContext *avctx)
  610. {
  611. /* If there is a user-supplied mutex locking routine, call it. */
  612. if (ff_lockmgr_cb) {
  613. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  614. return -1;
  615. }
  616. entangled_thread_counter++;
  617. if(entangled_thread_counter != 1){
  618. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  619. entangled_thread_counter--;
  620. return -1;
  621. }
  622. if (HAVE_THREADS && avctx->thread_opaque)
  623. avcodec_thread_free(avctx);
  624. if (avctx->codec && avctx->codec->close)
  625. avctx->codec->close(avctx);
  626. avcodec_default_free_buffers(avctx);
  627. avctx->coded_frame = NULL;
  628. av_freep(&avctx->priv_data);
  629. if(avctx->codec && avctx->codec->encode)
  630. av_freep(&avctx->extradata);
  631. avctx->codec = NULL;
  632. entangled_thread_counter--;
  633. /* Release any user-supplied mutex. */
  634. if (ff_lockmgr_cb) {
  635. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  636. }
  637. return 0;
  638. }
  639. AVCodec *avcodec_find_encoder(enum CodecID id)
  640. {
  641. AVCodec *p, *experimental=NULL;
  642. p = first_avcodec;
  643. while (p) {
  644. if (p->encode != NULL && p->id == id) {
  645. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  646. experimental = p;
  647. } else
  648. return p;
  649. }
  650. p = p->next;
  651. }
  652. return experimental;
  653. }
  654. AVCodec *avcodec_find_encoder_by_name(const char *name)
  655. {
  656. AVCodec *p;
  657. if (!name)
  658. return NULL;
  659. p = first_avcodec;
  660. while (p) {
  661. if (p->encode != NULL && strcmp(name,p->name) == 0)
  662. return p;
  663. p = p->next;
  664. }
  665. return NULL;
  666. }
  667. AVCodec *avcodec_find_decoder(enum CodecID id)
  668. {
  669. AVCodec *p;
  670. p = first_avcodec;
  671. while (p) {
  672. if (p->decode != NULL && p->id == id)
  673. return p;
  674. p = p->next;
  675. }
  676. return NULL;
  677. }
  678. AVCodec *avcodec_find_decoder_by_name(const char *name)
  679. {
  680. AVCodec *p;
  681. if (!name)
  682. return NULL;
  683. p = first_avcodec;
  684. while (p) {
  685. if (p->decode != NULL && strcmp(name,p->name) == 0)
  686. return p;
  687. p = p->next;
  688. }
  689. return NULL;
  690. }
  691. static int get_bit_rate(AVCodecContext *ctx)
  692. {
  693. int bit_rate;
  694. int bits_per_sample;
  695. switch(ctx->codec_type) {
  696. case AVMEDIA_TYPE_VIDEO:
  697. case AVMEDIA_TYPE_DATA:
  698. case AVMEDIA_TYPE_SUBTITLE:
  699. case AVMEDIA_TYPE_ATTACHMENT:
  700. bit_rate = ctx->bit_rate;
  701. break;
  702. case AVMEDIA_TYPE_AUDIO:
  703. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  704. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  705. break;
  706. default:
  707. bit_rate = 0;
  708. break;
  709. }
  710. return bit_rate;
  711. }
  712. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  713. {
  714. int i, len, ret = 0;
  715. for (i = 0; i < 4; i++) {
  716. len = snprintf(buf, buf_size,
  717. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  718. buf += len;
  719. buf_size = buf_size > len ? buf_size - len : 0;
  720. ret += len;
  721. codec_tag>>=8;
  722. }
  723. return ret;
  724. }
  725. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  726. {
  727. const char *codec_name;
  728. AVCodec *p;
  729. char buf1[32];
  730. int bitrate;
  731. AVRational display_aspect_ratio;
  732. if (encode)
  733. p = avcodec_find_encoder(enc->codec_id);
  734. else
  735. p = avcodec_find_decoder(enc->codec_id);
  736. if (p) {
  737. codec_name = p->name;
  738. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  739. /* fake mpeg2 transport stream codec (currently not
  740. registered) */
  741. codec_name = "mpeg2ts";
  742. } else if (enc->codec_name[0] != '\0') {
  743. codec_name = enc->codec_name;
  744. } else {
  745. /* output avi tags */
  746. char tag_buf[32];
  747. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  748. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  749. codec_name = buf1;
  750. }
  751. switch(enc->codec_type) {
  752. case AVMEDIA_TYPE_VIDEO:
  753. snprintf(buf, buf_size,
  754. "Video: %s%s",
  755. codec_name, enc->mb_decision ? " (hq)" : "");
  756. if (enc->pix_fmt != PIX_FMT_NONE) {
  757. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  758. ", %s",
  759. avcodec_get_pix_fmt_name(enc->pix_fmt));
  760. }
  761. if (enc->width) {
  762. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  763. ", %dx%d",
  764. enc->width, enc->height);
  765. if (enc->sample_aspect_ratio.num) {
  766. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  767. enc->width*enc->sample_aspect_ratio.num,
  768. enc->height*enc->sample_aspect_ratio.den,
  769. 1024*1024);
  770. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  771. " [PAR %d:%d DAR %d:%d]",
  772. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  773. display_aspect_ratio.num, display_aspect_ratio.den);
  774. }
  775. if(av_log_get_level() >= AV_LOG_DEBUG){
  776. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  777. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  778. ", %d/%d",
  779. enc->time_base.num/g, enc->time_base.den/g);
  780. }
  781. }
  782. if (encode) {
  783. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  784. ", q=%d-%d", enc->qmin, enc->qmax);
  785. }
  786. break;
  787. case AVMEDIA_TYPE_AUDIO:
  788. snprintf(buf, buf_size,
  789. "Audio: %s",
  790. codec_name);
  791. if (enc->sample_rate) {
  792. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  793. ", %d Hz", enc->sample_rate);
  794. }
  795. av_strlcat(buf, ", ", buf_size);
  796. avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  797. if (enc->sample_fmt != SAMPLE_FMT_NONE) {
  798. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  799. ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
  800. }
  801. break;
  802. case AVMEDIA_TYPE_DATA:
  803. snprintf(buf, buf_size, "Data: %s", codec_name);
  804. break;
  805. case AVMEDIA_TYPE_SUBTITLE:
  806. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  807. break;
  808. case AVMEDIA_TYPE_ATTACHMENT:
  809. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  810. break;
  811. default:
  812. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  813. return;
  814. }
  815. if (encode) {
  816. if (enc->flags & CODEC_FLAG_PASS1)
  817. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  818. ", pass 1");
  819. if (enc->flags & CODEC_FLAG_PASS2)
  820. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  821. ", pass 2");
  822. }
  823. bitrate = get_bit_rate(enc);
  824. if (bitrate != 0) {
  825. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  826. ", %d kb/s", bitrate / 1000);
  827. }
  828. }
  829. unsigned avcodec_version( void )
  830. {
  831. return LIBAVCODEC_VERSION_INT;
  832. }
  833. const char *avcodec_configuration(void)
  834. {
  835. return FFMPEG_CONFIGURATION;
  836. }
  837. const char *avcodec_license(void)
  838. {
  839. #define LICENSE_PREFIX "libavcodec license: "
  840. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  841. }
  842. void avcodec_init(void)
  843. {
  844. static int initialized = 0;
  845. if (initialized != 0)
  846. return;
  847. initialized = 1;
  848. dsputil_static_init();
  849. }
  850. void avcodec_flush_buffers(AVCodecContext *avctx)
  851. {
  852. if(avctx->codec->flush)
  853. avctx->codec->flush(avctx);
  854. }
  855. void avcodec_default_free_buffers(AVCodecContext *s){
  856. int i, j;
  857. if(s->internal_buffer==NULL) return;
  858. if (s->internal_buffer_count)
  859. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  860. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  861. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  862. for(j=0; j<4; j++){
  863. av_freep(&buf->base[j]);
  864. buf->data[j]= NULL;
  865. }
  866. }
  867. av_freep(&s->internal_buffer);
  868. s->internal_buffer_count=0;
  869. }
  870. char av_get_pict_type_char(int pict_type){
  871. switch(pict_type){
  872. case FF_I_TYPE: return 'I';
  873. case FF_P_TYPE: return 'P';
  874. case FF_B_TYPE: return 'B';
  875. case FF_S_TYPE: return 'S';
  876. case FF_SI_TYPE:return 'i';
  877. case FF_SP_TYPE:return 'p';
  878. case FF_BI_TYPE:return 'b';
  879. default: return '?';
  880. }
  881. }
  882. int av_get_bits_per_sample(enum CodecID codec_id){
  883. switch(codec_id){
  884. case CODEC_ID_ADPCM_SBPRO_2:
  885. return 2;
  886. case CODEC_ID_ADPCM_SBPRO_3:
  887. return 3;
  888. case CODEC_ID_ADPCM_SBPRO_4:
  889. case CODEC_ID_ADPCM_CT:
  890. case CODEC_ID_ADPCM_IMA_WAV:
  891. case CODEC_ID_ADPCM_MS:
  892. case CODEC_ID_ADPCM_YAMAHA:
  893. return 4;
  894. case CODEC_ID_PCM_ALAW:
  895. case CODEC_ID_PCM_MULAW:
  896. case CODEC_ID_PCM_S8:
  897. case CODEC_ID_PCM_U8:
  898. case CODEC_ID_PCM_ZORK:
  899. return 8;
  900. case CODEC_ID_PCM_S16BE:
  901. case CODEC_ID_PCM_S16LE:
  902. case CODEC_ID_PCM_S16LE_PLANAR:
  903. case CODEC_ID_PCM_U16BE:
  904. case CODEC_ID_PCM_U16LE:
  905. return 16;
  906. case CODEC_ID_PCM_S24DAUD:
  907. case CODEC_ID_PCM_S24BE:
  908. case CODEC_ID_PCM_S24LE:
  909. case CODEC_ID_PCM_U24BE:
  910. case CODEC_ID_PCM_U24LE:
  911. return 24;
  912. case CODEC_ID_PCM_S32BE:
  913. case CODEC_ID_PCM_S32LE:
  914. case CODEC_ID_PCM_U32BE:
  915. case CODEC_ID_PCM_U32LE:
  916. case CODEC_ID_PCM_F32BE:
  917. case CODEC_ID_PCM_F32LE:
  918. return 32;
  919. case CODEC_ID_PCM_F64BE:
  920. case CODEC_ID_PCM_F64LE:
  921. return 64;
  922. default:
  923. return 0;
  924. }
  925. }
  926. int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
  927. switch (sample_fmt) {
  928. case SAMPLE_FMT_U8:
  929. return 8;
  930. case SAMPLE_FMT_S16:
  931. return 16;
  932. case SAMPLE_FMT_S32:
  933. case SAMPLE_FMT_FLT:
  934. return 32;
  935. case SAMPLE_FMT_DBL:
  936. return 64;
  937. default:
  938. return 0;
  939. }
  940. }
  941. #if !HAVE_THREADS
  942. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  943. s->thread_count = thread_count;
  944. return -1;
  945. }
  946. #endif
  947. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  948. {
  949. unsigned int n = 0;
  950. while(v >= 0xff) {
  951. *s++ = 0xff;
  952. v -= 0xff;
  953. n++;
  954. }
  955. *s = v;
  956. n++;
  957. return n;
  958. }
  959. #if LIBAVCODEC_VERSION_MAJOR < 53
  960. #include "libavcore/parseutils.h"
  961. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  962. {
  963. return av_parse_video_size(width_ptr, height_ptr, str);
  964. }
  965. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  966. {
  967. return av_parse_video_rate(frame_rate, arg);
  968. }
  969. #endif
  970. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  971. int i;
  972. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  973. return i;
  974. }
  975. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  976. {
  977. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  978. "version to the newest one from SVN. If the problem still "
  979. "occurs, it means that your file has a feature which has not "
  980. "been implemented.", feature);
  981. if(want_sample)
  982. av_log_ask_for_sample(avc, NULL);
  983. else
  984. av_log(avc, AV_LOG_WARNING, "\n");
  985. }
  986. void av_log_ask_for_sample(void *avc, const char *msg)
  987. {
  988. if (msg)
  989. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  990. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  991. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  992. "and contact the ffmpeg-devel mailing list.\n");
  993. }
  994. static AVHWAccel *first_hwaccel = NULL;
  995. void av_register_hwaccel(AVHWAccel *hwaccel)
  996. {
  997. AVHWAccel **p = &first_hwaccel;
  998. while (*p)
  999. p = &(*p)->next;
  1000. *p = hwaccel;
  1001. hwaccel->next = NULL;
  1002. }
  1003. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1004. {
  1005. return hwaccel ? hwaccel->next : first_hwaccel;
  1006. }
  1007. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1008. {
  1009. AVHWAccel *hwaccel=NULL;
  1010. while((hwaccel= av_hwaccel_next(hwaccel))){
  1011. if ( hwaccel->id == codec_id
  1012. && hwaccel->pix_fmt == pix_fmt)
  1013. return hwaccel;
  1014. }
  1015. return NULL;
  1016. }
  1017. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1018. {
  1019. if (ff_lockmgr_cb) {
  1020. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1021. return -1;
  1022. }
  1023. ff_lockmgr_cb = cb;
  1024. if (ff_lockmgr_cb) {
  1025. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1026. return -1;
  1027. }
  1028. return 0;
  1029. }
  1030. unsigned int ff_toupper4(unsigned int x)
  1031. {
  1032. return toupper( x &0xFF)
  1033. + (toupper((x>>8 )&0xFF)<<8 )
  1034. + (toupper((x>>16)&0xFF)<<16)
  1035. + (toupper((x>>24)&0xFF)<<24);
  1036. }