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.

402 lines
12KB

  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. char *colorspace = "";
  31. st = s->streams[0];
  32. width = st->codec->width;
  33. height = st->codec->height;
  34. av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
  35. aspectn = st->codec->sample_aspect_ratio.num;
  36. aspectd = st->codec->sample_aspect_ratio.den;
  37. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  38. inter = 'p'; /* progressive is the default */
  39. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
  40. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  41. }
  42. switch(st->codec->pix_fmt) {
  43. case PIX_FMT_GRAY8:
  44. colorspace = " Cmono";
  45. break;
  46. case PIX_FMT_YUV411P:
  47. colorspace = " C411 XYSCSS=411";
  48. break;
  49. case PIX_FMT_YUV420P:
  50. colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
  51. break;
  52. case PIX_FMT_YUV422P:
  53. colorspace = " C422 XYSCSS=422";
  54. break;
  55. case PIX_FMT_YUV444P:
  56. colorspace = " C444 XYSCSS=444";
  57. break;
  58. }
  59. /* construct stream header, if this is the first frame */
  60. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  61. Y4M_MAGIC,
  62. width,
  63. height,
  64. raten, rated,
  65. inter,
  66. aspectn, aspectd,
  67. colorspace);
  68. return n;
  69. }
  70. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  71. {
  72. AVStream *st = s->streams[pkt->stream_index];
  73. ByteIOContext *pb = &s->pb;
  74. AVPicture *picture;
  75. int* first_pkt = s->priv_data;
  76. int width, height, h_chroma_shift, v_chroma_shift;
  77. int i, m;
  78. char buf2[Y4M_LINE_MAX+1];
  79. char buf1[20];
  80. uint8_t *ptr, *ptr1, *ptr2;
  81. picture = (AVPicture *)pkt->data;
  82. /* for the first packet we have to output the header as well */
  83. if (*first_pkt) {
  84. *first_pkt = 0;
  85. if (yuv4_generate_header(s, buf2) < 0) {
  86. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  87. return AVERROR_IO;
  88. } else {
  89. put_buffer(pb, buf2, strlen(buf2));
  90. }
  91. }
  92. /* construct frame header */
  93. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  94. put_buffer(pb, buf1, strlen(buf1));
  95. width = st->codec->width;
  96. height = st->codec->height;
  97. ptr = picture->data[0];
  98. for(i=0;i<height;i++) {
  99. put_buffer(pb, ptr, width);
  100. ptr += picture->linesize[0];
  101. }
  102. if (st->codec->pix_fmt != PIX_FMT_GRAY8){
  103. // Adjust for smaller Cb and Cr planes
  104. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  105. width >>= h_chroma_shift;
  106. height >>= v_chroma_shift;
  107. ptr1 = picture->data[1];
  108. ptr2 = picture->data[2];
  109. for(i=0;i<height;i++) { /* Cb */
  110. put_buffer(pb, ptr1, width);
  111. ptr1 += picture->linesize[1];
  112. }
  113. for(i=0;i<height;i++) { /* Cr */
  114. put_buffer(pb, ptr2, width);
  115. ptr2 += picture->linesize[2];
  116. }
  117. }
  118. put_flush_packet(pb);
  119. return 0;
  120. }
  121. static int yuv4_write_header(AVFormatContext *s)
  122. {
  123. int* first_pkt = s->priv_data;
  124. if (s->nb_streams != 1)
  125. return AVERROR_IO;
  126. if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
  127. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  128. }
  129. else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
  130. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  131. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  132. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
  133. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
  134. return AVERROR_IO;
  135. }
  136. *first_pkt = 1;
  137. return 0;
  138. }
  139. static int yuv4_write_trailer(AVFormatContext *s)
  140. {
  141. return 0;
  142. }
  143. AVOutputFormat yuv4mpegpipe_oformat = {
  144. "yuv4mpegpipe",
  145. "YUV4MPEG pipe format",
  146. "",
  147. "y4m",
  148. sizeof(int),
  149. CODEC_ID_NONE,
  150. CODEC_ID_RAWVIDEO,
  151. yuv4_write_header,
  152. yuv4_write_packet,
  153. yuv4_write_trailer,
  154. .flags = AVFMT_RAWPICTURE,
  155. };
  156. #endif //CONFIG_ENCODERS
  157. /* Header size increased to allow room for optional flags */
  158. #define MAX_YUV4_HEADER 80
  159. #define MAX_FRAME_HEADER 80
  160. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  161. {
  162. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  163. char *tokstart,*tokend,*header_end;
  164. int i;
  165. ByteIOContext *pb = &s->pb;
  166. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0,interlaced_frame=0,top_field_first=0;
  167. enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
  168. AVStream *st;
  169. for (i=0; i<MAX_YUV4_HEADER; i++) {
  170. header[i] = get_byte(pb);
  171. if (header[i] == '\n') {
  172. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  173. header[i+2] = 0;
  174. break;
  175. }
  176. }
  177. if (i == MAX_YUV4_HEADER) return -1;
  178. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  179. header_end = &header[i+1]; // Include space
  180. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  181. if (*tokstart==0x20) continue;
  182. switch (*tokstart++) {
  183. case 'W': // Width. Required.
  184. width = strtol(tokstart, &tokend, 10);
  185. tokstart=tokend;
  186. break;
  187. case 'H': // Height. Required.
  188. height = strtol(tokstart, &tokend, 10);
  189. tokstart=tokend;
  190. break;
  191. case 'C': // Color space
  192. if (strncmp("420jpeg",tokstart,7)==0)
  193. pix_fmt = PIX_FMT_YUV420P;
  194. else if (strncmp("420mpeg2",tokstart,8)==0)
  195. pix_fmt = PIX_FMT_YUV420P;
  196. else if (strncmp("420paldv", tokstart, 8)==0)
  197. pix_fmt = PIX_FMT_YUV420P;
  198. else if (strncmp("411", tokstart, 3)==0)
  199. pix_fmt = PIX_FMT_YUV411P;
  200. else if (strncmp("422", tokstart, 3)==0)
  201. pix_fmt = PIX_FMT_YUV422P;
  202. else if (strncmp("444alpha", tokstart, 8)==0) {
  203. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  204. return -1;
  205. } else if (strncmp("444", tokstart, 3)==0)
  206. pix_fmt = PIX_FMT_YUV444P;
  207. else if (strncmp("mono",tokstart, 4)==0) {
  208. pix_fmt = PIX_FMT_GRAY8;
  209. } else {
  210. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  211. return -1;
  212. }
  213. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  214. break;
  215. case 'I': // Interlace type
  216. switch (*tokstart++){
  217. case '?':
  218. break;
  219. case 'p':
  220. interlaced_frame=0;
  221. break;
  222. case 't':
  223. interlaced_frame=1;
  224. top_field_first=1;
  225. break;
  226. case 'b':
  227. interlaced_frame=1;
  228. top_field_first=0;
  229. break;
  230. case 'm':
  231. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  232. return -1;
  233. default:
  234. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  235. return -1;
  236. }
  237. break;
  238. case 'F': // Frame rate
  239. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  240. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  241. break;
  242. case 'A': // Pixel aspect
  243. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  244. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  245. break;
  246. case 'X': // Vendor extensions
  247. if (strncmp("YSCSS=",tokstart,6)==0) {
  248. // Older nonstandard pixel format representation
  249. tokstart+=6;
  250. if (strncmp("420JPEG",tokstart,7)==0)
  251. alt_pix_fmt=PIX_FMT_YUV420P;
  252. else if (strncmp("420MPEG2",tokstart,8)==0)
  253. alt_pix_fmt=PIX_FMT_YUV420P;
  254. else if (strncmp("420PALDV",tokstart,8)==0)
  255. alt_pix_fmt=PIX_FMT_YUV420P;
  256. else if (strncmp("411",tokstart,3)==0)
  257. alt_pix_fmt=PIX_FMT_YUV411P;
  258. else if (strncmp("422",tokstart,3)==0)
  259. alt_pix_fmt=PIX_FMT_YUV422P;
  260. else if (strncmp("444",tokstart,3)==0)
  261. alt_pix_fmt=PIX_FMT_YUV444P;
  262. }
  263. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  264. break;
  265. }
  266. }
  267. if ((width == -1) || (height == -1)) {
  268. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  269. return -1;
  270. }
  271. if (pix_fmt == PIX_FMT_NONE) {
  272. if (alt_pix_fmt == PIX_FMT_NONE)
  273. pix_fmt = PIX_FMT_YUV420P;
  274. else
  275. pix_fmt = alt_pix_fmt;
  276. }
  277. if (raten == 0 && rated == 0) {
  278. // Frame rate unknown
  279. raten = 25;
  280. rated = 1;
  281. }
  282. if (aspectn == 0 && aspectd == 0) {
  283. // Pixel aspect unknown
  284. aspectd = 1;
  285. }
  286. st = av_new_stream(s, 0);
  287. st = s->streams[0];
  288. st->codec->width = width;
  289. st->codec->height = height;
  290. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  291. av_set_pts_info(st, 64, rated, raten);
  292. st->codec->pix_fmt = pix_fmt;
  293. st->codec->codec_type = CODEC_TYPE_VIDEO;
  294. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  295. st->codec->sample_aspect_ratio= (AVRational){aspectn, aspectd};
  296. return 0;
  297. }
  298. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  299. {
  300. int i;
  301. char header[MAX_FRAME_HEADER+1];
  302. int packet_size, width, height;
  303. AVStream *st = s->streams[0];
  304. for (i=0; i<MAX_FRAME_HEADER; i++) {
  305. header[i] = get_byte(&s->pb);
  306. if (header[i] == '\n') {
  307. header[i+1] = 0;
  308. break;
  309. }
  310. }
  311. if (i == MAX_FRAME_HEADER) return -1;
  312. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  313. width = st->codec->width;
  314. height = st->codec->height;
  315. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  316. if (packet_size < 0)
  317. return -1;
  318. if (av_get_packet(&s->pb, pkt, packet_size) != packet_size)
  319. return AVERROR_IO;
  320. pkt->stream_index = 0;
  321. return 0;
  322. }
  323. static int yuv4_read_close(AVFormatContext *s)
  324. {
  325. return 0;
  326. }
  327. static int yuv4_probe(AVProbeData *pd)
  328. {
  329. /* check file header */
  330. if (pd->buf_size <= sizeof(Y4M_MAGIC))
  331. return 0;
  332. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
  333. return AVPROBE_SCORE_MAX;
  334. else
  335. return 0;
  336. }
  337. AVInputFormat yuv4mpegpipe_iformat = {
  338. "yuv4mpegpipe",
  339. "YUV4MPEG pipe format",
  340. 0,
  341. yuv4_probe,
  342. yuv4_read_header,
  343. yuv4_read_packet,
  344. yuv4_read_close,
  345. .extensions = "y4m"
  346. };
  347. int yuv4mpeg_init(void)
  348. {
  349. av_register_input_format(&yuv4mpegpipe_iformat);
  350. #ifdef CONFIG_ENCODERS
  351. av_register_output_format(&yuv4mpegpipe_oformat);
  352. #endif //CONFIG_ENCODERS
  353. return 0;
  354. }