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.

681 lines
17KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(unsigned int size)
  23. {
  24. void *ptr;
  25. if(size == 0) fprintf(stderr, "Warning, allocating 0 bytes\n");
  26. ptr = av_malloc(size);
  27. if (!ptr)
  28. return NULL;
  29. memset(ptr, 0, size);
  30. return ptr;
  31. }
  32. /* allocation of static arrays - do not use for normal allocation */
  33. static unsigned int last_static = 0;
  34. static char*** array_static = NULL;
  35. static const unsigned int grow_static = 64; // ^2
  36. void *__av_mallocz_static(void** location, unsigned int size)
  37. {
  38. int l = (last_static + grow_static) & ~(grow_static - 1);
  39. void *ptr = av_mallocz(size);
  40. if (!ptr)
  41. return NULL;
  42. if (location)
  43. {
  44. if (l > last_static)
  45. array_static = realloc(array_static, l);
  46. array_static[last_static++] = (char**) location;
  47. *location = ptr;
  48. }
  49. return ptr;
  50. }
  51. /* free all static arrays and reset pointers to 0 */
  52. void av_free_static()
  53. {
  54. if (array_static)
  55. {
  56. unsigned i;
  57. for (i = 0; i < last_static; i++)
  58. {
  59. free(*array_static[i]);
  60. *array_static[i] = NULL;
  61. }
  62. free(array_static);
  63. array_static = 0;
  64. }
  65. last_static = 0;
  66. }
  67. /* cannot call it directly because of 'void **' casting is not automatic */
  68. void __av_freep(void **ptr)
  69. {
  70. av_free(*ptr);
  71. *ptr = NULL;
  72. }
  73. /* encoder management */
  74. AVCodec *first_avcodec;
  75. void register_avcodec(AVCodec *format)
  76. {
  77. AVCodec **p;
  78. p = &first_avcodec;
  79. while (*p != NULL) p = &(*p)->next;
  80. *p = format;
  81. format->next = NULL;
  82. }
  83. void avcodec_get_chroma_sub_sample(int fmt, int *h_shift, int *v_shift){
  84. switch(fmt){
  85. case PIX_FMT_YUV410P:
  86. *h_shift=2;
  87. *v_shift=2;
  88. break;
  89. case PIX_FMT_YUV420P:
  90. *h_shift=1;
  91. *v_shift=1;
  92. break;
  93. case PIX_FMT_YUV411P:
  94. *h_shift=2;
  95. *v_shift=0;
  96. break;
  97. case PIX_FMT_YUV422P:
  98. case PIX_FMT_YUV422:
  99. *h_shift=1;
  100. *v_shift=0;
  101. break;
  102. default: //RGB/...
  103. *h_shift=0;
  104. *v_shift=0;
  105. break;
  106. }
  107. }
  108. typedef struct DefaultPicOpaque{
  109. int last_pic_num;
  110. uint8_t *data[4];
  111. }DefaultPicOpaque;
  112. int avcodec_default_get_buffer(AVCodecContext *s, AVVideoFrame *pic){
  113. int i;
  114. const int width = s->width;
  115. const int height= s->height;
  116. DefaultPicOpaque *opaque;
  117. if(pic->opaque){
  118. opaque= (DefaultPicOpaque *)pic->opaque;
  119. for(i=0; i<3; i++)
  120. pic->data[i]= opaque->data[i];
  121. // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
  122. pic->age= pic->coded_picture_number - opaque->last_pic_num;
  123. opaque->last_pic_num= pic->coded_picture_number;
  124. //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
  125. }else{
  126. int align, h_chroma_shift, v_chroma_shift;
  127. int w, h, pixel_size;
  128. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  129. switch(s->pix_fmt){
  130. case PIX_FMT_YUV422:
  131. pixel_size=2;
  132. break;
  133. case PIX_FMT_RGB24:
  134. case PIX_FMT_BGR24:
  135. pixel_size=3;
  136. break;
  137. case PIX_FMT_BGRA32:
  138. case PIX_FMT_RGBA32:
  139. pixel_size=4;
  140. break;
  141. default:
  142. pixel_size=1;
  143. }
  144. if(s->codec_id==CODEC_ID_SVQ1) align=63;
  145. else align=15;
  146. w= (width +align)&~align;
  147. h= (height+align)&~align;
  148. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  149. w+= EDGE_WIDTH*2;
  150. h+= EDGE_WIDTH*2;
  151. }
  152. opaque= av_mallocz(sizeof(DefaultPicOpaque));
  153. if(opaque==NULL) return -1;
  154. pic->opaque= opaque;
  155. opaque->last_pic_num= -256*256*256*64;
  156. for(i=0; i<3; i++){
  157. int h_shift= i==0 ? 0 : h_chroma_shift;
  158. int v_shift= i==0 ? 0 : v_chroma_shift;
  159. pic->linesize[i]= pixel_size*w>>h_shift;
  160. pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  161. if(pic->base[i]==NULL) return -1;
  162. memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
  163. if(s->flags&CODEC_FLAG_EMU_EDGE)
  164. pic->data[i] = pic->base[i];
  165. else
  166. pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift);
  167. opaque->data[i]= pic->data[i];
  168. }
  169. pic->age= 256*256*256*64;
  170. }
  171. return 0;
  172. }
  173. void avcodec_default_release_buffer(AVCodecContext *s, AVVideoFrame *pic){
  174. int i;
  175. for(i=0; i<3; i++)
  176. pic->data[i]=NULL;
  177. //printf("R%X\n", pic->opaque);
  178. }
  179. void avcodec_get_context_defaults(AVCodecContext *s){
  180. s->bit_rate= 800*1000;
  181. s->bit_rate_tolerance= s->bit_rate*10;
  182. s->qmin= 2;
  183. s->qmax= 31;
  184. s->rc_eq= "tex^qComp";
  185. s->qcompress= 0.5;
  186. s->max_qdiff= 3;
  187. s->b_quant_factor=1.25;
  188. s->b_quant_offset=1.25;
  189. s->i_quant_factor=-0.8;
  190. s->i_quant_offset=0.0;
  191. s->error_concealment= 3;
  192. s->error_resilience= 1;
  193. s->workaround_bugs= FF_BUG_AUTODETECT;
  194. s->frame_rate = 25 * FRAME_RATE_BASE;
  195. s->gop_size= 50;
  196. s->me_method= ME_EPZS;
  197. s->get_buffer= avcodec_default_get_buffer;
  198. s->release_buffer= avcodec_default_release_buffer;
  199. }
  200. /**
  201. * allocates a AVCodecContext and set it to defaults.
  202. * this can be deallocated by simply calling free()
  203. */
  204. AVCodecContext *avcodec_alloc_context(void){
  205. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  206. if(avctx==NULL) return NULL;
  207. avcodec_get_context_defaults(avctx);
  208. return avctx;
  209. }
  210. /**
  211. * allocates a AVPicture and set it to defaults.
  212. * this can be deallocated by simply calling free()
  213. */
  214. AVVideoFrame *avcodec_alloc_picture(void){
  215. AVVideoFrame *pic= av_mallocz(sizeof(AVVideoFrame));
  216. return pic;
  217. }
  218. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  219. {
  220. int ret;
  221. avctx->codec = codec;
  222. avctx->frame_number = 0;
  223. if (codec->priv_data_size > 0) {
  224. avctx->priv_data = av_mallocz(codec->priv_data_size);
  225. if (!avctx->priv_data)
  226. return -ENOMEM;
  227. } else {
  228. avctx->priv_data = NULL;
  229. }
  230. ret = avctx->codec->init(avctx);
  231. if (ret < 0) {
  232. av_freep(&avctx->priv_data);
  233. return ret;
  234. }
  235. return 0;
  236. }
  237. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  238. const short *samples)
  239. {
  240. int ret;
  241. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  242. avctx->frame_number++;
  243. return ret;
  244. }
  245. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  246. const AVVideoFrame *pict)
  247. {
  248. int ret;
  249. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  250. emms_c(); //needed to avoid a emms_c() call before every return;
  251. avctx->frame_number++;
  252. return ret;
  253. }
  254. /* decode a frame. return -1 if error, otherwise return the number of
  255. bytes used. If no frame could be decompressed, *got_picture_ptr is
  256. zero. Otherwise, it is non zero */
  257. int avcodec_decode_video(AVCodecContext *avctx, AVVideoFrame *picture,
  258. int *got_picture_ptr,
  259. UINT8 *buf, int buf_size)
  260. {
  261. int ret;
  262. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  263. buf, buf_size);
  264. emms_c(); //needed to avoid a emms_c() call before every return;
  265. if (*got_picture_ptr)
  266. avctx->frame_number++;
  267. return ret;
  268. }
  269. /* decode an audio frame. return -1 if error, otherwise return the
  270. *number of bytes used. If no frame could be decompressed,
  271. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  272. *size in BYTES. */
  273. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  274. int *frame_size_ptr,
  275. UINT8 *buf, int buf_size)
  276. {
  277. int ret;
  278. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  279. buf, buf_size);
  280. avctx->frame_number++;
  281. return ret;
  282. }
  283. int avcodec_close(AVCodecContext *avctx)
  284. {
  285. if (avctx->codec->close)
  286. avctx->codec->close(avctx);
  287. av_freep(&avctx->priv_data);
  288. avctx->codec = NULL;
  289. return 0;
  290. }
  291. AVCodec *avcodec_find_encoder(enum CodecID id)
  292. {
  293. AVCodec *p;
  294. p = first_avcodec;
  295. while (p) {
  296. if (p->encode != NULL && p->id == id)
  297. return p;
  298. p = p->next;
  299. }
  300. return NULL;
  301. }
  302. AVCodec *avcodec_find_encoder_by_name(const char *name)
  303. {
  304. AVCodec *p;
  305. p = first_avcodec;
  306. while (p) {
  307. if (p->encode != NULL && strcmp(name,p->name) == 0)
  308. return p;
  309. p = p->next;
  310. }
  311. return NULL;
  312. }
  313. AVCodec *avcodec_find_decoder(enum CodecID id)
  314. {
  315. AVCodec *p;
  316. p = first_avcodec;
  317. while (p) {
  318. if (p->decode != NULL && p->id == id)
  319. return p;
  320. p = p->next;
  321. }
  322. return NULL;
  323. }
  324. AVCodec *avcodec_find_decoder_by_name(const char *name)
  325. {
  326. AVCodec *p;
  327. p = first_avcodec;
  328. while (p) {
  329. if (p->decode != NULL && strcmp(name,p->name) == 0)
  330. return p;
  331. p = p->next;
  332. }
  333. return NULL;
  334. }
  335. AVCodec *avcodec_find(enum CodecID id)
  336. {
  337. AVCodec *p;
  338. p = first_avcodec;
  339. while (p) {
  340. if (p->id == id)
  341. return p;
  342. p = p->next;
  343. }
  344. return NULL;
  345. }
  346. const char *pix_fmt_str[] = {
  347. "yuv420p",
  348. "yuv422",
  349. "rgb24",
  350. "bgr24",
  351. "yuv422p",
  352. "yuv444p",
  353. "rgba32",
  354. "bgra32",
  355. "yuv410p",
  356. "yuv411p",
  357. };
  358. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  359. {
  360. const char *codec_name;
  361. AVCodec *p;
  362. char buf1[32];
  363. char channels_str[100];
  364. int bitrate;
  365. if (encode)
  366. p = avcodec_find_encoder(enc->codec_id);
  367. else
  368. p = avcodec_find_decoder(enc->codec_id);
  369. if (p) {
  370. codec_name = p->name;
  371. } else if (enc->codec_name[0] != '\0') {
  372. codec_name = enc->codec_name;
  373. } else {
  374. /* output avi tags */
  375. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  376. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  377. enc->codec_tag & 0xff,
  378. (enc->codec_tag >> 8) & 0xff,
  379. (enc->codec_tag >> 16) & 0xff,
  380. (enc->codec_tag >> 24) & 0xff);
  381. } else {
  382. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  383. }
  384. codec_name = buf1;
  385. }
  386. switch(enc->codec_type) {
  387. case CODEC_TYPE_VIDEO:
  388. snprintf(buf, buf_size,
  389. "Video: %s%s",
  390. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  391. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  392. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  393. ", %s",
  394. pix_fmt_str[enc->pix_fmt]);
  395. }
  396. if (enc->width) {
  397. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  398. ", %dx%d, %0.2f fps",
  399. enc->width, enc->height,
  400. (float)enc->frame_rate / FRAME_RATE_BASE);
  401. }
  402. if (encode) {
  403. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  404. ", q=%d-%d", enc->qmin, enc->qmax);
  405. }
  406. bitrate = enc->bit_rate;
  407. break;
  408. case CODEC_TYPE_AUDIO:
  409. snprintf(buf, buf_size,
  410. "Audio: %s",
  411. codec_name);
  412. switch (enc->channels) {
  413. case 1:
  414. strcpy(channels_str, "mono");
  415. break;
  416. case 2:
  417. strcpy(channels_str, "stereo");
  418. break;
  419. case 6:
  420. strcpy(channels_str, "5:1");
  421. break;
  422. default:
  423. sprintf(channels_str, "%d channels", enc->channels);
  424. break;
  425. }
  426. if (enc->sample_rate) {
  427. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  428. ", %d Hz, %s",
  429. enc->sample_rate,
  430. channels_str);
  431. }
  432. /* for PCM codecs, compute bitrate directly */
  433. switch(enc->codec_id) {
  434. case CODEC_ID_PCM_S16LE:
  435. case CODEC_ID_PCM_S16BE:
  436. case CODEC_ID_PCM_U16LE:
  437. case CODEC_ID_PCM_U16BE:
  438. bitrate = enc->sample_rate * enc->channels * 16;
  439. break;
  440. case CODEC_ID_PCM_S8:
  441. case CODEC_ID_PCM_U8:
  442. case CODEC_ID_PCM_ALAW:
  443. case CODEC_ID_PCM_MULAW:
  444. bitrate = enc->sample_rate * enc->channels * 8;
  445. break;
  446. default:
  447. bitrate = enc->bit_rate;
  448. break;
  449. }
  450. break;
  451. default:
  452. av_abort();
  453. }
  454. if (encode) {
  455. if (enc->flags & CODEC_FLAG_PASS1)
  456. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  457. ", pass 1");
  458. if (enc->flags & CODEC_FLAG_PASS2)
  459. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  460. ", pass 2");
  461. }
  462. if (bitrate != 0) {
  463. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  464. ", %d kb/s", bitrate / 1000);
  465. }
  466. }
  467. /* Picture field are filled with 'ptr' addresses */
  468. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  469. int pix_fmt, int width, int height)
  470. {
  471. int size;
  472. size = width * height;
  473. switch(pix_fmt) {
  474. case PIX_FMT_YUV420P:
  475. picture->data[0] = ptr;
  476. picture->data[1] = picture->data[0] + size;
  477. picture->data[2] = picture->data[1] + size / 4;
  478. picture->linesize[0] = width;
  479. picture->linesize[1] = width / 2;
  480. picture->linesize[2] = width / 2;
  481. break;
  482. case PIX_FMT_YUV422P:
  483. picture->data[0] = ptr;
  484. picture->data[1] = picture->data[0] + size;
  485. picture->data[2] = picture->data[1] + size / 2;
  486. picture->linesize[0] = width;
  487. picture->linesize[1] = width / 2;
  488. picture->linesize[2] = width / 2;
  489. break;
  490. case PIX_FMT_YUV444P:
  491. picture->data[0] = ptr;
  492. picture->data[1] = picture->data[0] + size;
  493. picture->data[2] = picture->data[1] + size;
  494. picture->linesize[0] = width;
  495. picture->linesize[1] = width;
  496. picture->linesize[2] = width;
  497. break;
  498. case PIX_FMT_RGB24:
  499. case PIX_FMT_BGR24:
  500. picture->data[0] = ptr;
  501. picture->data[1] = NULL;
  502. picture->data[2] = NULL;
  503. picture->linesize[0] = width * 3;
  504. break;
  505. case PIX_FMT_RGBA32:
  506. case PIX_FMT_BGRA32:
  507. picture->data[0] = ptr;
  508. picture->data[1] = NULL;
  509. picture->data[2] = NULL;
  510. picture->linesize[0] = width * 4;
  511. break;
  512. case PIX_FMT_YUV422:
  513. picture->data[0] = ptr;
  514. picture->data[1] = NULL;
  515. picture->data[2] = NULL;
  516. picture->linesize[0] = width * 2;
  517. break;
  518. default:
  519. picture->data[0] = NULL;
  520. picture->data[1] = NULL;
  521. picture->data[2] = NULL;
  522. break;
  523. }
  524. }
  525. int avpicture_get_size(int pix_fmt, int width, int height)
  526. {
  527. int size;
  528. size = width * height;
  529. switch(pix_fmt) {
  530. case PIX_FMT_YUV420P:
  531. size = (size * 3) / 2;
  532. break;
  533. case PIX_FMT_YUV422P:
  534. size = (size * 2);
  535. break;
  536. case PIX_FMT_YUV444P:
  537. size = (size * 3);
  538. break;
  539. case PIX_FMT_RGB24:
  540. case PIX_FMT_BGR24:
  541. size = (size * 3);
  542. break;
  543. case PIX_FMT_RGBA32:
  544. case PIX_FMT_BGRA32:
  545. size = (size * 4);
  546. break;
  547. case PIX_FMT_YUV422:
  548. size = (size * 2);
  549. break;
  550. default:
  551. size = -1;
  552. break;
  553. }
  554. return size;
  555. }
  556. unsigned avcodec_version( void )
  557. {
  558. return LIBAVCODEC_VERSION_INT;
  559. }
  560. unsigned avcodec_build( void )
  561. {
  562. return LIBAVCODEC_BUILD;
  563. }
  564. /* must be called before any other functions */
  565. void avcodec_init(void)
  566. {
  567. static int inited = 0;
  568. if (inited != 0)
  569. return;
  570. inited = 1;
  571. //dsputil_init();
  572. }
  573. /* this should be called after seeking and before trying to decode the next frame */
  574. void avcodec_flush_buffers(AVCodecContext *avctx)
  575. {
  576. MpegEncContext *s = avctx->priv_data;
  577. s->num_available_buffers=0;
  578. }
  579. static int raw_encode_init(AVCodecContext *s)
  580. {
  581. return 0;
  582. }
  583. static int raw_decode_frame(AVCodecContext *avctx,
  584. void *data, int *data_size,
  585. UINT8 *buf, int buf_size)
  586. {
  587. return -1;
  588. }
  589. static int raw_encode_frame(AVCodecContext *avctx,
  590. unsigned char *frame, int buf_size, void *data)
  591. {
  592. return -1;
  593. }
  594. AVCodec rawvideo_codec = {
  595. "rawvideo",
  596. CODEC_TYPE_VIDEO,
  597. CODEC_ID_RAWVIDEO,
  598. 0,
  599. raw_encode_init,
  600. raw_encode_frame,
  601. NULL,
  602. raw_decode_frame,
  603. };