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.

258 lines
6.9KB

  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 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 "avformat.h"
  20. #define Y4M_MAGIC "YUV4MPEG2"
  21. #define Y4M_FRAME_MAGIC "FRAME"
  22. #define Y4M_LINE_MAX 256
  23. #ifdef CONFIG_ENCODERS
  24. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  25. {
  26. AVStream *st;
  27. int width, height;
  28. int raten, rated, aspectn, aspectd, n;
  29. char inter;
  30. st = s->streams[0];
  31. width = st->codec.width;
  32. height = st->codec.height;
  33. av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
  34. aspectn = st->codec.sample_aspect_ratio.num;
  35. aspectd = st->codec.sample_aspect_ratio.den;
  36. inter = 'p'; /* progressive is the default */
  37. if (st->codec.coded_frame && st->codec.coded_frame->interlaced_frame) {
  38. inter = st->codec.coded_frame->top_field_first ? 't' : 'b';
  39. }
  40. /* construct stream header, if this is the first frame */
  41. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  42. Y4M_MAGIC,
  43. width,
  44. height,
  45. raten, rated,
  46. inter,
  47. aspectn, aspectd,
  48. (st->codec.pix_fmt == PIX_FMT_YUV411P) ? " C411 XYSCSS=411" : " C420mpeg2 XYSCSS=420MPEG2");
  49. return n;
  50. }
  51. static int yuv4_write_packet(AVFormatContext *s, int stream_index,
  52. const uint8_t *buf, int size, int64_t pts)
  53. {
  54. AVStream *st = s->streams[stream_index];
  55. ByteIOContext *pb = &s->pb;
  56. AVPicture *picture;
  57. int* first_pkt = s->priv_data;
  58. int width, height;
  59. int i, m;
  60. char buf2[Y4M_LINE_MAX+1];
  61. char buf1[20];
  62. uint8_t *ptr, *ptr1, *ptr2;
  63. picture = (AVPicture *)buf;
  64. /* for the first packet we have to output the header as well */
  65. if (*first_pkt) {
  66. *first_pkt = 0;
  67. if (yuv4_generate_header(s, buf2) < 0) {
  68. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  69. return -EIO;
  70. } else {
  71. put_buffer(pb, buf2, strlen(buf2));
  72. }
  73. }
  74. /* construct frame header */
  75. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  76. put_buffer(pb, buf1, strlen(buf1));
  77. width = st->codec.width;
  78. height = st->codec.height;
  79. ptr = picture->data[0];
  80. for(i=0;i<height;i++) {
  81. put_buffer(pb, ptr, width);
  82. ptr += picture->linesize[0];
  83. }
  84. height >>= 1;
  85. width >>= 1;
  86. ptr1 = picture->data[1];
  87. ptr2 = picture->data[2];
  88. for(i=0;i<height;i++) { /* Cb */
  89. put_buffer(pb, ptr1, width);
  90. ptr1 += picture->linesize[1];
  91. }
  92. for(i=0;i<height;i++) { /* Cr */
  93. put_buffer(pb, ptr2, width);
  94. ptr2 += picture->linesize[2];
  95. }
  96. put_flush_packet(pb);
  97. return 0;
  98. }
  99. static int yuv4_write_header(AVFormatContext *s)
  100. {
  101. int* first_pkt = s->priv_data;
  102. if (s->nb_streams != 1)
  103. return -EIO;
  104. if (s->streams[0]->codec.pix_fmt == PIX_FMT_YUV411P) {
  105. av_log(s, AV_LOG_ERROR, "Warning: generating non-standard 4:1:1 YUV stream, some mjpegtools might not work.\n");
  106. }
  107. else if (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV420P) {
  108. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles 4:2:0, 4:1:1 YUV data. Use -pix_fmt to select one.\n");
  109. return -EIO;
  110. }
  111. *first_pkt = 1;
  112. return 0;
  113. }
  114. static int yuv4_write_trailer(AVFormatContext *s)
  115. {
  116. return 0;
  117. }
  118. AVOutputFormat yuv4mpegpipe_oformat = {
  119. "yuv4mpegpipe",
  120. "YUV4MPEG pipe format",
  121. "",
  122. "yuv4mpeg",
  123. sizeof(int),
  124. CODEC_ID_NONE,
  125. CODEC_ID_RAWVIDEO,
  126. yuv4_write_header,
  127. yuv4_write_packet,
  128. yuv4_write_trailer,
  129. .flags = AVFMT_RAWPICTURE,
  130. };
  131. #endif //CONFIG_ENCODERS
  132. /* Header size increased to allow room for optional flags */
  133. #define MAX_YUV4_HEADER 80
  134. #define MAX_FRAME_HEADER 10
  135. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  136. {
  137. char header[MAX_YUV4_HEADER+1];
  138. int i;
  139. ByteIOContext *pb = &s->pb;
  140. int width, height, raten, rated, aspectn, aspectd;
  141. char lacing;
  142. AVStream *st;
  143. for (i=0; i<MAX_YUV4_HEADER; i++) {
  144. header[i] = get_byte(pb);
  145. if (header[i] == '\n') {
  146. header[i+1] = 0;
  147. break;
  148. }
  149. }
  150. if (i == MAX_YUV4_HEADER) return -1;
  151. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  152. sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
  153. &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
  154. st = av_new_stream(s, 0);
  155. st = s->streams[0];
  156. st->codec.width = width;
  157. st->codec.height = height;
  158. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  159. st->codec.frame_rate = raten;
  160. st->codec.frame_rate_base = rated;
  161. st->codec.pix_fmt = PIX_FMT_YUV420P;
  162. st->codec.codec_type = CODEC_TYPE_VIDEO;
  163. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  164. st->codec.sample_aspect_ratio= (AVRational){aspectn, aspectd};
  165. return 0;
  166. }
  167. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  168. {
  169. int i;
  170. char header[MAX_FRAME_HEADER+1];
  171. int packet_size, ret, width, height;
  172. AVStream *st = s->streams[0];
  173. for (i=0; i<MAX_FRAME_HEADER; i++) {
  174. header[i] = get_byte(&s->pb);
  175. if (header[i] == '\n') {
  176. header[i+1] = 0;
  177. break;
  178. }
  179. }
  180. if (i == MAX_FRAME_HEADER) return -1;
  181. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  182. width = st->codec.width;
  183. height = st->codec.height;
  184. packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
  185. if (packet_size < 0)
  186. av_abort();
  187. if (av_new_packet(pkt, packet_size) < 0)
  188. return -EIO;
  189. pkt->stream_index = 0;
  190. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  191. if (ret != pkt->size) {
  192. av_free_packet(pkt);
  193. return -EIO;
  194. } else {
  195. return 0;
  196. }
  197. }
  198. static int yuv4_read_close(AVFormatContext *s)
  199. {
  200. return 0;
  201. }
  202. AVInputFormat yuv4mpegpipe_iformat = {
  203. "yuv4mpegpipe",
  204. "YUV4MPEG pipe format",
  205. 0,
  206. NULL,
  207. yuv4_read_header,
  208. yuv4_read_packet,
  209. yuv4_read_close,
  210. .extensions = "yuv4mpeg"
  211. };
  212. int yuv4mpeg_init(void)
  213. {
  214. av_register_input_format(&yuv4mpegpipe_iformat);
  215. #ifdef CONFIG_ENCODERS
  216. av_register_output_format(&yuv4mpegpipe_oformat);
  217. #endif //CONFIG_ENCODERS
  218. return 0;
  219. }